当前位置:   article > 正文

git 常用的几剂后悔药_no changes added to commit (use "git add" and/or "

no changes added to commit (use "git add" and/or "git commit -a")

在 git add 之前,放弃对文件的所有修改

就算你用 rm 删除了也无所谓,照样还原回来。(如果是没有用 git 进行跟踪的文件,可不要轻易这么尝试哟!)

git checkout --

当你对一个文件进行了修改,还没有 add 进暂存区,你想要放弃所有的修改,将文件恢复到没有修改之前的样子,这时候可以使用 git checkout -- ,这条命令会将文件恢复到上一次 commit 的时候这个文件的样子。

  1. [root@master GitTest]# vim hello.txt
  2. [root@master GitTest]# git status
  3. On branch master
  4. Your branch is up to date with 'origin/master'.
  5. Changes not staged for commit:
  6. (use "git add <file>..." to update what will be committed)
  7. (use "git restore <file>..." to discard changes in working directory)
  8. modified: hello.txt
  9. no changes added to commit (use "git add" and/or "git commit -a")
  10. [root@master GitTest]# git checkout -- hello.txt
  11. [root@master GitTest]# git status
  12. On branch master
  13. Your branch is up to date with 'origin/master'.
  14. nothing to commit, working tree clean

git restore  (推荐使用)

貌似新版本的 git restore  和 git checkout -- 差不多,以后可能用 git restore 更多一些了,毕竟见文知意,而且命令还短。

  1. [root@master GitTest]# vim hello.txt
  2. [root@master GitTest]# git status
  3. On branch master
  4. Your branch is up to date with 'origin/master'.
  5. Changes not staged for commit:
  6. (use "git add <file>..." to update what will be committed)
  7. (use "git restore <file>..." to discard changes in working directory)
  8. modified: hello.txt
  9. no changes added to commit (use "git add" and/or "git commit -a")
  10. [root@master GitTest]# git restore hello.txt
  11. [root@master GitTest]# git status
  12. On branch master
  13. Your branch is up to date with 'origin/master'.
  14. nothing to commit, working tree clean

将 git add 的文件变成 git add 之前的状态

git restore --staged <file>

  1. [looking@master GitTest]$ vim b.txt
  2. [looking@master GitTest]$ git add b.txt
  3. [looking@master GitTest]$ git status
  4. On branch master
  5. Your branch is up to date with 'origin/master'.
  6. Changes to be committed:
  7. (use "git restore --staged <file>..." to unstage)
  8. modified: b.txt
  9. [looking@master GitTest]$ git restore --staged b.txt
  10. [looking@master GitTest]$ git status
  11. On branch master
  12. Your branch is up to date with 'origin/master'.
  13. Changes not staged for commit:
  14. (use "git add <file>..." to update what will be committed)
  15. (use "git restore <file>..." to discard changes in working directory)
  16. modified: b.txt
  17. no changes added to commit (use "git add" and/or "git commit -a")

git reset HEAD <file>

  1. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
  2. $ vi myGit.txt # 添加新行 hello world
  3. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
  4. $ git add myGit.txt
  5. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
  6. $ git status
  7. On branch master
  8. Your branch is ahead of 'origin/master' by 2 commits.
  9. (use "git push" to publish your local commits)
  10. Changes to be committed:
  11. (use "git reset HEAD <file>..." to unstage)
  12. modified: myGit.txt
  13. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
  14. $ git reset HEAD myGit.txt
  15. Unstaged changes after reset:
  16. M myGit.txt
  17. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
  18. $ git status
  19. On branch master
  20. Your branch is ahead of 'origin/master' by 2 commits.
  21. (use "git push" to publish your local commits)
  22. Changes not staged for commit:
  23. (use "git add <file>..." to update what will be committed)
  24. (use "git checkout -- <file>..." to discard changes in working directory)
  25. modified: myGit.txt
  26. no changes added to commit (use "git add" and/or "git commit -a")

将最近 commit 中的某个文件重新修改提交

git restore -s HEAD~1 <file>

执行以后,git commit 的 log 信息并没有丢失,对 file 的修改会 rollback 回滚,你可以重新修改文件,然后 git add , git commit 对文件的修改重新进行提交。(适用场景:最近的 commit 修改了多个文件,但是想对其中一个文件的提交撤销并重新修改)

  1. [looking@master GitTest]$ git status
  2. On branch master
  3. Your branch is ahead of 'origin/master' by 1 commit.
  4. (use "git push" to publish your local commits)
  5. nothing to commit, working tree clean
  6. [looking@master GitTest]$ git restore -s HEAD~1 hello.txt
  7. [looking@master GitTest]$ git status
  8. On branch master
  9. Your branch is ahead of 'origin/master' by 1 commit.
  10. (use "git push" to publish your local commits)
  11. Changes not staged for commit:
  12. (use "git add <file>..." to update what will be committed)
  13. (use "git restore <file>..." to discard changes in working directory)
  14. modified: hello.txt
  15. no changes added to commit (use "git add" and/or "git commit -a")

重新填写 git commit 信息

git commit --amend

有时候我们发现提交信息填写错了,或者在提交完成之后才发现有几个文件没有提交,这时候可以使用 git commit --amend 尝试重新进行提交。

  1. $ git commit --amend
  2. - mv test.txt to test directory
  3. + mv test.txt to test dir
  4. # Please enter the commit message for your changes. Lines starting
  5. # with '#' will be ignored, and an empty message aborts the commit.
  6. #
  7. # Date: Wed Jul 29 22:28:31 2020 +0800
  8. #
  9. # On branch master
  10. # Your branch is ahead of 'origin/master' by 2 commits.
  11. # (use "git push" to publish your local commits)
  12. #
  13. # Changes to be committed:
  14. # renamed: test.txt -> test/test.txt
  15. #
  16. $ git commit --amend
  17. [master db41223] mv test.txt to test dir
  18. Date: Wed Jul 29 22:28:31 2020 +0800
  19. 1 file changed, 1 insertion(+)
  20. rename test.txt => test/test.txt (71%)

不想让 git 继续跟踪某个文件

git rm --cached

如果你只是想从暂存区删除文件,但是工作区的文件保持不变(将文件保存在磁盘),也就是说将文件保存在磁盘但是不想让 Git 进行跟踪,使用如下命令即可 git rm --cached :

  1. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
  2. $ vi test.txt # 新行添加 hello world
  3. git
  4. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
  5. $ git status
  6. On branch master
  7. Your branch is ahead of 'origin/master' by 1 commit.
  8. (use "git push" to publish your local commits)
  9. Changes not staged for commit:
  10. (use "git add <file>..." to update what will be committed)
  11. (use "git checkout -- <file>..." to discard changes in working directory)
  12. modified: test.txt
  13. no changes added to commit (use "git add" and/or "git commit -a")
  14. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
  15. $ git rm --cached test.txt
  16. rm 'test.txt'
  17. Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)
  18. $ git status # 显示文件 test.txt 为 Untracked files
  19. On branch master
  20. Your branch is ahead of 'origin/master' by 1 commit.
  21. (use "git push" to publish your local commits)
  22. Changes to be committed:
  23. (use "git reset HEAD <file>..." to unstage)
  24. deleted: test.txt
  25. Untracked files:
  26. (use "git add <file>..." to include in what will be committed)
  27. test.txt

将 git 仓库变成普通目录

rm -rf .git

这个要稍微简单一些了,毕竟 git 能进行版本控制最主要的就是因为这个隐藏的 .git 目录了,删了就可以了(请谨慎操作)。

  1. [root@master GitTest]# git status
  2. On branch master
  3. Your branch is up to date with 'origin/master'.
  4. nothing to commit, working tree clean
  5. [root@master GitTest]# rm -rf .git
  6. [root@master GitTest]# git status
  7. fatal: not a git repository (or any of the parent directories): .git

撤销最近的 commit,但是不撤销对应 commit 所做的文件修改

git reset HEAD~1

注:使用此命令,你原来提交的代码都在,不会被撤销,也即只会撤销  commit 记录(不对文件内容进行任何修改)。

  1. [root@master GitTest]# git status
  2. # On branch master
  3. nothing to commit, working directory clean
  4. [root@master GitTest]# vim b.txt # 删除了文件最后一行
  5. # 下面 commit 失败,因为修改的文件还没添加到暂存区
  6. [root@master GitTest]# git commit -m "delete last line in b.txt"
  7. # On branch master
  8. # Changes not staged for commit:
  9. # (use "git add <file>..." to update what will be committed)
  10. # (use "git checkout -- <file>..." to discard changes in working directory)
  11. #
  12. # modified: b.txt
  13. #
  14. no changes added to commit (use "git add" and/or "git commit -a")
  15. ------------------------------------------------------------
  16. [root@master GitTest]# git add b.txt
  17. [root@master GitTest]# git commit -m "delete last line in b.txt"
  18. [master 131c2e9] delete last line in b.txt
  19. 1 file changed, 1 deletion(-)
  20. [root@master GitTest]# git log
  21. commit 131c2e9e676726168a87db678c17e8ec404b8c4e
  22. Author: looking <looking@qq.com>
  23. Date: Sat Aug 8 01:35:09 2020 +0800
  24. delete last line in b.txt
  25. commit f3b4d4abd65a59c8a03df6caccf28f33422b4614
  26. Author: looking <looking@qq.com>
  27. Date: Tue Aug 4 21:42:27 2020 +0800
  28. This is a combination of 2 commits.
  29. add hello in new line of looking.txt
  30. add world in new line of looking.txt
  31. ------------------------------------------------------------
  32. # 注意啦,放大招啦。
  33. [root@master GitTest]# git reset HEAD~
  34. Unstaged changes after reset:
  35. M b.txt
  36. # 这个时候回到了 addcommit 提交之前,但是在文件 b.txt 修改之后的那个状态
  37. [root@master GitTest]# git status
  38. # On branch master
  39. # Changes not staged for commit:
  40. # (use "git add <file>..." to update what will be committed)
  41. # (use "git checkout -- <file>..." to discard changes in working directory)
  42. #
  43. # modified: b.txt
  44. #
  45. no changes added to commit (use "git add" and/or "git commit -a")
  46. ------------------------------------------------------------
  47. # 因为 commit 被撤销了,所以我们再重新提交一次
  48. [root@master GitTest]# git add b.txt
  49. [root@master GitTest]# git commit -m "delete last line in b.txt"
  50. [master 33796e9] delete last line in b.txt
  51. 1 file changed, 1 deletion(-)
  52. [root@master GitTest]# git log
  53. commit 33796e9844af4b6e1ee23c2d98ec5be4c5fef3e6
  54. Author: looking <looking@qq.com>
  55. Date: Sat Aug 8 01:44:27 2020 +0800
  56. delete last line in b.txt
  57. commit f3b4d4abd65a59c8a03df6caccf28f33422b4614
  58. Author: looking <looking@qq.com>
  59. Date: Tue Aug 4 21:42:27 2020 +0800
  60. This is a combination of 2 commits.
  61. add hello in new line of looking.txt
  62. add world in new line of looking.txt
  63. commit cd3e33625578c3aaf6f19b69c878559a1dcf552a
  64. Merge: 640fd3c 3fceb8a
  65. [root@master GitTest]#

撤销最近的 commit,同时撤销对应 commit 所作的文件修改

git reset --hard HEAD^1

注:使用之后,你最新的 commit 命令下修改的内容将完全被撤销,最近一次的 commit 记录也没了。

  1. [root@master GitTest]# git add b.txt
  2. [root@master GitTest]# git commit -m "modify file b.txt"
  3. [master be7abc1] modify file b.txt
  4. 1 file changed, 2 deletions(-)
  5. [root@master GitTest]# git log
  6. commit be7abc12a1a94e9390bd6c7f4bc747f9bbe86ab9
  7. Author: looking <looking@qq.com>
  8. Date: Sat Aug 8 02:01:32 2020 +0800
  9. modify file b.txt
  10. commit f3b4d4abd65a59c8a03df6caccf28f33422b4614
  11. Author: looking <looking@qq.com>
  12. Date: Tue Aug 4 21:42:27 2020 +0800
  13. This is a combination of 2 commits.
  14. add hello in new line of looking.txt
  15. add world in new line of looking.txt
  16. ------------------------------------------------------------
  17. [root@master GitTest]# cat b.txt
  18. hahahahaha
  19. hohohohoho
  20. rebase test
  21. add one line in b.txt by user B
  22. add one line in b.txt by user B again
  23. ------------------------------------------------------------
  24. [root@master GitTest]# git reset --hard HEAD^1
  25. HEAD is now at f3b4d4a This is a combination of 2 commits.
  26. ------------------------------------------------------------
  27. # 注意看,git reset --hard HEAD^1 执行前后, b.txt 文件内容发生变化了哟!
  28. # git log 的最近一次 commit 没有了!
  29. ------------------------------------------------------------
  30. [root@master GitTest]# cat b.txt
  31. hahahahaha
  32. hohohohoho
  33. hehehehehe
  34. rebase test
  35. add one line in b.txt by user B
  36. add one line in b.txt by user B again
  37. hello world
  38. ------------------------------------------------------------
  39. [root@master GitTest]# git log
  40. commit f3b4d4abd65a59c8a03df6caccf28f33422b4614
  41. Author: looking <looking@qq.com>
  42. Date: Tue Aug 4 21:42:27 2020 +0800
  43. This is a combination of 2 commits.
  44. add hello in new line of looking.txt
  45. add world in new line of looking.txt

回滚之前某个 commit 提交的内容

git revert <commit-id>

git revert 是用于“反做”某一个版本,以达到撤销该版本的修改的目的。 git reset 的作用是修改HEAD的位置,且那个版本之后提交的版本都会丢失。

git revert 可以回滚某个 commit 对应的修改,所以在这点上面 revert 和 reset 是不一样的。

  1. root@master ~/GitTest# vim hello.txt
  2. root@master ~/GitTest# git add hello.txt
  3. root@master ~/GitTest# git commit -m "add hello in hello.txt"
  4. root@master ~/GitTest# git show
  5. commit 72bfd90b8194b1d3537fc62a22d6776d32ae5b4b (HEAD -> master)
  6. Author: looking <looking@qq.com>
  7. Date: Mon Jun 6 17:11:10 2022 +0800
  8. add hello in hello.txt
  9. diff --git a/hello.txt b/hello.txt
  10. index 83585f1..25f7272 100644
  11. --- a/hello.txt
  12. +++ b/hello.txt
  13. @@ -1,3 +1,4 @@
  14. +hello
  15. nice
  16. hello world
  17. hello world. I am Looking

revert 是新建一个 commit 来回滚之前 commit-id 的操作,可以看到 git revert <commit-id> 执行的操作是 commit-id 操作的逆操作,也就是 commit-id 做的动作,它都会反向操作一遍。

  1. root@master ~/GitTest# git revert 72bfd90b8194b1d3537fc62a22d6776d32ae5b4b
  2. [master 780b3e0] Revert "add hello in hello.txt"
  3. 1 file changed, 1 deletion(-)
  4. root@master ~/GitTest# git show
  5. commit 780b3e0044b10aa62a6459a44d4b657da76a501d (HEAD -> master)
  6. Author: looking <looking@qq.com>
  7. Date: Mon Jun 6 17:26:12 2022 +0800
  8. Revert "add hello in hello.txt"
  9. This reverts commit 72bfd90b8194b1d3537fc62a22d6776d32ae5b4b.
  10. diff --git a/hello.txt b/hello.txt
  11. index 25f7272..83585f1 100644
  12. --- a/hello.txt
  13. +++ b/hello.txt
  14. @@ -1,4 +1,3 @@
  15. -hello
  16. nice
  17. hello world
  18. hello world. I am Looking

 reset 的话,则 reset 对应 commit-id 之后的修改就丢失了(这也是 reset 比 revert 更危险的原因之一)。

  1. root@master ~/GitTest# git reset --hard 1912baa1b9457add393f48c41ffd35957aa8b569
  2. HEAD is now at 1912baa deal merge conflict in hello.txt

当然,回滚也是可能会造成冲突的,稍微注意一下就行。比如有人在你之前 commit-id 修改的地方又重新做了新的修改,你 revert 这个 commit-id 的话肯定就不可避免要 conflict 了。 

修改倒数二个 commit 相关内容

git rebase -i HEAD~2 和 git rebase --continue。git 修改倒数二个 commit_TomorrowAndTuture的博客-CSDN博客

git rebase -i HEAD~2

git rebase -i HEAD~2 以后,git 会自动给你切换到下面这个界面,你将你需要修改的那个 commit 前边的 pick 修改为 edit。

  1. [root@master GitTest]# git rebase -i HEAD~2
  2. edit 3d87922 nice to meet you
  3. pick df5f952 nice to meet you too
  4. # Rebase 3f20612..df5f952 onto 3f20612 (2 commands)
  5. #
  6. # Commands:
  7. # p, pick <commit> = use commit
  8. # r, reword <commit> = use commit, but edit the commit message
  9. # e, edit <commit> = use commit, but stop for amending
  10. # s, squash <commit> = use commit, but meld into previous commit
  11. # f, fixup <commit> = like "squash", but discard this commit's log message
  12. # x, exec <command> = run command (the rest of the line) using shell
  13. # b, break = stop here (continue rebase later with 'git rebase --continue')
  14. # d, drop <commit> = remove commit
  15. # l, label <label> = label current HEAD with a name
  16. # t, reset <label> = reset HEAD to a label
  17. # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
  18. # . create a merge commit using the original merge commit's
  19. # . message (or the oneline, if no original merge commit was
  20. # . specified). Use -c <commit> to reword the commit message.

然后保存退出,而且 git 也已经提醒你版本已经 stopped 在这个 commit 节点了,你可以开始你的表演(修改)了:

  1. [root@master GitTest]# git rebase -i HEAD~2
  2. Stopped at 3d87922... nice to meet you
  3. You can amend the commit now, with
  4. git commit --amend
  5. Once you are satisfied with your changes, run
  6. git rebase --continue

假如我想把原来插入的 nice to meet you 修改为 nice。用 vim 修改后用 git commit --amend(不会产生新的 commit 提交) 重新提交 commit。

  1. [root@master GitTest]# vim hello.txt
  2. [root@master GitTest]# git add hello.txt
  3. [root@master GitTest]# git commit --amend
  4. nice
  5. # Please enter the commit message for your changes. Lines starting
  6. # with '#' will be ignored, and an empty message aborts the commit.
  7. #
  8. # Date: Mon Aug 31 21:16:51 2020 +0800
  9. #
  10. # interactive rebase in progress; onto 3f20612
  11. [detached HEAD bcbfd91] nice
  12. Date: Mon Aug 31 21:16:51 2020 +0800
  13. 1 file changed, 1 insertion(+)

git rebase --continue

然后继续 git rebase --continue 就好了(如果没有冲突,直接 git rebase --continue 也是极好的)。

  1. [root@master GitTest]# git add hello.txt
  2. [root@master GitTest]# git rebase --continue
  3. [detached HEAD 72017cd] nice to meet you too
  4. 1 file changed, 1 insertion(+)
  5. Successfully rebased and updated refs/heads/master.

git rebase --abort

这个是你在 git rebase 过程当中的后悔药,如果你在 rebase 的任何过程中想退出 rebase 过程,直接执行 git rebase --abort 就直接退出回到 rebase 之前的状态啦。

将最近多个commit合并成一个(以最近两个为例)

git rebase -i HEAD~2 (下面列表中,越靠下的 commit 离 HEAD 越近)

  1. pick b6fdaf6 add nice in hello.txt
  2. squash 63bb8ef add hello world in world.txt
  3. # Rebase eced426..63bb8ef onto eced426 (2 commands)
  4. #
  5. # Commands:
  6. # p, pick <commit> = use commit
  7. # r, reword <commit> = use commit, but edit the commit message
  8. # e, edit <commit> = use commit, but stop for amending
  9. # s, squash <commit> = use commit, but meld into previous commit
  10. # f, fixup <commit> = like "squash", but discard this commit's log message
  11. # x, exec <command> = run command (the rest of the line) using shell
  12. # b, break = stop here (continue rebase later with 'git rebase --continue')
  13. # d, drop <commit> = remove commit
  14. # l, label <label> = label current HEAD with a name
  15. # t, reset <label> = reset HEAD to a label
  16. # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
  17. # . create a merge commit using the original merge commit's
  18. # . message (or the oneline, if no original merge commit was
  19. # . specified). Use -c <commit> to reword the commit message.
  20. #
  21. # These lines can be re-ordered; they are executed from top to bottom.
  22. #
  23. # If you remove a line here THAT COMMIT WILL BE LOST.
  24. #
  25. # However, if you remove everything, the rebase will be aborted.

使用 squash 将最近一个提交 squash 合并到上一个提交(也就是倒数第二个 commit)产生新的 commit,然后:wq 保存。

然后继续编辑提交详情信息,保存退出即可。

  1. # This is a combination of 2 commits.
  2. # This is the 1st commit message:
  3. add nice in hello.txt
  4. # This is the commit message #2:
  5. add hello world in world.txt
  1. commit bf00e63e180caa680584920c08f396818c2fcf02 (HEAD -> master)
  2. Author: looking <looking@qq.com>
  3. Date: Tue Dec 22 09:51:15 2020 +0800
  4. add nice in hello.txt and add hello world in world.txt
  5. commit eced426098acd286cb45cb7191f83ddee9cc228c
  6. Author: Looking <38554375+2392863668@users.noreply.github.com>
  7. Date: Tue Dec 22 10:25:57 2020 +0800
  8. clear hello.txt

将多个不连续commit合并成一个

git rebase -i HEAD~6(下面列表中,越靠下的 commit 离 HEAD 越近)

这个和上面的操作类似,还是使用 git rebase 命令

  1. root@master ~/GitTest# git rebase -i HEAD~6
  2. pick b6fdaf6 add nice in hello.txt
  3. pick 63bb8ef add hello world in world.txt
  4. pick ba8b2b1 delete world in first line of hello.txt
  5. pick 5983ee6 modify b.txt
  6. pick 5eaae9c modify hello.txt
  7. pick 5c528bb modify c.txt

比如说我想将倒数第2个,第4个,第6个 commit (和操作 hello.txt 相关的几个不连续 commit)都合并到一个 commit,就这么调整顺序和操作如下:

  1. pick 63bb8ef add hello world in world.txt
  2. pick b68f98f modify b.txt
  3. pick b6fdaf6 add nice in hello.txt
  4. s ba8b2b1 delete world in first line of hello.txt
  5. s 2820a56 modify hello.txt
  6. pick 824c246 modify c.txt

 然后保存 commit 就好了(这块强调一下,针对有相互依赖关系的 commit 提交,千万不要轻易改变 commit 的相对顺序,否者可能会出现冲突——如果你不嫌麻烦要去处理冲突的忽略。比如我这块对 hello.txt 操作的三个 commit ,相对顺序并没有发生变化;虽然我其他 commit 和 这三个 commit 的相对顺序发生了变化,但是其他 commit 和这三个关联不大,不会出现冲突,所以也就没太大影响)

  1. root@master ~/GitTest# git log
  2. commit d6566c3000afe7a057d5b54946f828b3b9bcffa0 (HEAD -> master)
  3. Author: looking <looking@qq.com>
  4. Date: Fri Feb 24 16:12:04 2023 +0800
  5. modify c.txt
  6. commit a9f43a7acc4830b50df48d4a0187154fe3f644c0
  7. Author: looking <looking@qq.com>
  8. Date: Tue Dec 22 09:51:15 2020 +0800
  9. add nice in hello.txt
  10. delete world in first line of hello.txt
  11. modify hello.txt
  12. commit 6bc4f93f821b4946a725bf20c2fd6643e244a380
  13. Author: looking <looking@qq.com>
  14. Date: Fri Feb 24 16:11:24 2023 +0800
  15. modify b.txt
  16. commit c18dc0314ab997f061350f89d7f983bf5687a27a
  17. Author: looking <looking@qq.com>
  18. Date: Tue Dec 22 09:54:39 2020 +0800
  19. add hello world in world.txt

将代码临时切换回之前的某个稳定版本(不乐意的话还可以切换回来)

git checkout  和 git checkout master。

  1. [root@master GitTest]# git checkout 3f2061298d921378da14f4003753f1f277e67243
  2. Note: switching to '3f2061298d921378da14f4003753f1f277e67243'.
  3. You are in 'detached HEAD' state. You can look around, make experimental
  4. changes and commit them, and you can discard any commits you make in this
  5. state without impacting any branches by switching back to a branch.
  6. If you want to create a new branch to retain commits you create, you may
  7. do so (now or later) by using -c with the switch command. Example:
  8. git switch -c <new-branch-name>
  9. Or undo this operation with:
  10. git switch -
  11. Turn off this advice by setting config variable advice.detachedHead to false
  12. HEAD is now at 3f20612 I am Looking
  13. [root@master GitTest]# git checkout master
  14. Previous HEAD position was 3f20612 I am Looking
  15. Switched to branch 'master'
  16. Your branch is up to date with 'origin/master'.

将代码永久切换回之前的某个稳定版本(对应分支的指针也一同切换过去了)

git reset --hard commitId(谨慎使用)

如果想恢复到之前某个提交的版本,且那个版本之后提交的版本我们都不要了,就可以用这种方法(这个和 git checkout 最大的不同在于它把你的 master(分支指针所引用的 commit-id ——正常情况下指向当前分支的最新提交点) 也切换过去了,你要再想用 git checkout master 可就回不去 origin/master 了哟,如果你还记得 commitId 的话,还是可以切换回去的)。

master:本地主分支。origin/master:远端主分支。

  1. [root@master GitTest]# git reset --hard 3f2061298d921378da14f4003753f1f277e67243
  2. HEAD is now at 3f20612 I am Looking
  3. [root@master GitTest]# git checkout master
  4. Already on 'master'
  5. Your branch is behind 'origin/master' by 12 commits, and can be fast-forwarded.
  6. (use "git pull" to update your local branch)
  7. [root@master GitTest]# git reset --hard 1912baa1b9457add393f48c41ffd35957aa8b569
  8. HEAD is now at 1912baa deal merge conflict in hello.txt
  9. [root@master GitTest]# git checkout master
  10. Already on 'master'
  11. Your branch is up to date with 'origin/master'.

删除远端分支

git push origin --delete <branch-name>

快速保存和恢复临时工作现场

git stash 和 git stash pop

git stash 结合 git stash pop 适用于快速保存和和恢复临时工作现场(比如你在 branch1 正在安安心心写代码,突然老板让你赶紧去 branch2 去修一个紧急 bug,可以使用 git stash 将当前未提交的工作快速暂存到 git 堆栈,到 biranch2 修改 bug 以后再切换回 branch1,再次 git stash pop 快速恢复现场),还可以适用于快速对比程序修改前后的运行差异,而且可以在现有修改的基础上继续修改。 

丢弃本地所有未提交的修改

git stash 同时 git stash clear

针对本地所有修改了但是还没进行 git commit 提交的部分,即使已经使用了 git add,也仍然可以丢弃。

  1. [root@master GitTest]# git status
  2. On branch master
  3. Your branch is ahead of 'origin/master' by 1 commit.
  4. (use "git push" to publish your local commits)
  5. Changes to be committed:
  6. (use "git restore --staged <file>..." to unstage)
  7. deleted: b.txt
  8. modified: hello.txt
  9. Changes not staged for commit:
  10. (use "git add <file>..." to update what will be committed)
  11. (use "git restore <file>..." to discard changes in working directory)
  12. modified: c.txt
  13. [root@master GitTest]# git stash
  14. Saved working directory and index state WIP on master: 2017dee add newline in c.txt
  15. [root@master GitTest]# git stash clear
  16. [root@master GitTest]# git status
  17. On branch master
  18. Your branch is ahead of 'origin/master' by 1 commit.
  19. (use "git push" to publish your local commits)
  20. nothing to commit, working tree clean

丢弃本地所有修改,强制远端拉取覆盖本地

git fetch --all ; git reset --hard origin/master

如果你想以远程仓库的版本内容为准,丢弃本地所有修改(重置了暂存区的内容,而且还修改重置了本地工作区的内容,即使是已提交 commit 的部分也会重置),但是又不想删除本地仓库以后再次 git clone,那么就按下边这么做吧(这里的 origin/master 可以认为是一个指针,初次拉取仓库时,他指向了远端 origin 仓库 master 分支的最新提交点,只有当本地 commit 被 push 到远端仓库的时候,指针的指向才会发生改变)。

git fetch --all; git reset --hard origin/master
  1. commit 44892cd7d4c8c36267f6af9f8568fb5369eaaddc (HEAD -> master)
  2. Author: lukaiyi <lukaiyi@zdns.cn>
  3. Date: Fri Mar 10 15:42:49 2023 +0800
  4. test
  5. commit 1912baa1b9457add393f48c41ffd35957aa8b569 (origin/master, origin/HEAD)
  6. Merge: ba8b2b1 5f7be5d
  7. Author: looking <looking@qq.com>
  8. Date: Wed Dec 23 15:34:02 2020 +0800
  9. deal merge conflict in hello.txt
  1. root@localhost ~/GitTest (master)# git push origin master
  2. Username for 'https://gitee.com': 15249089066
  3. Password for 'https://15249089066@gitee.com':
  4. Enumerating objects: 5, done.
  5. Counting objects: 100% (5/5), done.
  6. Delta compression using up to 4 threads
  7. Compressing objects: 100% (3/3), done.
  8. Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done.
  9. Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
  10. remote: Powered by GITEE.COM [GNK-6.4]
  11. To https://gitee.com/L2392863668/GitTest.git
  12. 1912baa..44892cd master -> master
  13. root@localhost ~/GitTest (master)# git log
  14. commit 44892cd7d4c8c36267f6af9f8568fb5369eaaddc (HEAD -> master, origin/master, origin/HEAD)
  15. Author: lukaiyi <lukaiyi@zdns.cn>
  16. Date: Fri Mar 10 15:42:49 2023 +0800
  17. test
  18. commit 1912baa1b9457add393f48c41ffd35957aa8b569
  19. Merge: ba8b2b1 5f7be5d
  20. Author: looking <looking@qq.com>
  21. Date: Wed Dec 23 15:34:02 2020 +0800
  22. deal merge conflict in hello.txt

git fetch --all ; git reset --hard HEAD

如果你只想重置当前分支未提交的内容(已提交的内容不被重置),那就直接用  HEAD 就可以了,像上面这样即可。

丢弃远端所有修改,强制推送覆盖远端 

git push -f <repo> <branch>

这个好像没啥多说的,只要你考虑好了,就强制推送就好了啦。默认是 git push -f origin master。

删除本地仓库,重新克隆(终极解决方案)

rm -rf GitTest

git clone https://github.com/2392863668/GitTest

如果你想以远程仓库的版本内容为准,丢弃本地所有修改(包括仓库里边 untracked file 文件的改动),那就重新克隆一个吧。

  1. [root@master workspace]# rm -rf GitTest
  2. [root@master workspace]# git clone https://github.com/2392863668/GitTest
  3. Cloning into 'GitTest'...
  4. remote: Enumerating objects: 26, done.
  5. remote: Counting objects: 100% (26/26), done.
  6. remote: Compressing objects: 100% (19/19), done.
  7. remote: Total 247 (delta 11), reused 15 (delta 5), pack-reused 221
  8. Receiving objects: 100% (247/247), 1.27 MiB | 869.00 KiB/s, done.
  9. Resolving deltas: 100% (95/95), done.

重要知识点总结

  1. “^”代表父提交,当一个提交有多个父提交时,可以通过在”^”后面跟上一个数字,表示第几个父提交,”^”相当于”^1”.

  2. ~<n>相当于连续的<n>个”^”.

  3. checkout只会移动HEAD指针,reset会改变HEAD的引用值。

  4. HEAD、<branch>、origin/<branch> 都属于指针,HEAD 一般指向当前位置最新提交点,<branch> 一般指向对应分支的最新提交点、origin/<branch> 一般指向对应分支远端origin仓库的最新提交点,本地的 commit 提交,在 push 到远端之前,origin/<branch> 的引用指向一般不会发生变化。

  5. git commit -am "xxx" 和 git add . ; git commit -m "xxx" 操作其实不等价,-am 参数只会将已追踪的文件进行 add 和 commit,而先 git add . 会将未追踪的文件也添加到暂存区
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/264713
推荐阅读
  

闽ICP备14008679号