..." to discard changes in working directory)">
当前位置:   article > 正文

Git之git checkout再理解及用法汇总_(use "git checkout -- ..." to discard change

(use "git checkout -- ..." to discard changes in working directory)

目录

前言

切换分支

放弃修改


前言

checkoutGit最常用的命令之一,我们常常使用它来切换分支,但是切换分支的时候总会遇到一些莫名其妙的问题导致切换失败,查询资料会发现它还有一些其他用法,虽然能够解决当前的问题,但是总会感觉有些用不明白,用不明白的原因应该是没有深度地、全面地理解它,所以要对它好好整理一下。

checkoutCVSSVN中都是检出的意思,从版本库检出一个版本,在Git中就不是这么简单了。手册上是这样介绍的:

git-checkout - Switch branches or restore working tree files

翻译这句话意思就是,checkout用于切换分支或者恢复工作树的文件。

那我们看下它的详细用法:

  1. $ git checkout -h
  2. usage: git checkout [<options>] <branch>
  3. or: git checkout [<options>] [<branch>] -- <file>...
  4. -q, --quiet suppress progress reporting
  5. -b <branch> create and checkout a new branch
  6. -B <branch> create/reset and checkout a branch
  7. -l create reflog for new branch
  8. --detach detach HEAD at named commit
  9. -t, --track set upstream info for new branch
  10. --orphan <new-branch>
  11. new unparented branch
  12. -2, --ours checkout our version for unmerged files
  13. -3, --theirs checkout their version for unmerged files
  14. -f, --force force checkout (throw away local modifications)
  15. -m, --merge perform a 3-way merge with the new branch
  16. --overwrite-ignore update ignored files (default)
  17. --conflict <style> conflict style (merge or diff3)
  18. -p, --patch select hunks interactively
  19. --ignore-skip-worktree-bits
  20. do not limit pathspecs to sparse entries only
  21. --ignore-other-worktrees
  22. do not check if another worktree is holding the given ref
  23. --recurse-submodules[=<checkout>]
  24. control recursive updating of submodules
  25. --progress force progress reporting

usage后面的or表示这个命令有两种用法,第一种是切换分支,第二种是撤销修改。

checkout 本意是检出的意思,也就是将某次commit的状态检出到工作区;所以它的过程是先将HEAD指向某个分支的最近一次commit,然后从commit恢复index,最后从index恢复工作区。
 

切换分支

创建并切换到新的分支,参考语法:

git checkout -b|-B <new_branch> [<start point>] 

git checkout branchname

如果不指定切换到哪个分支,那就是切换到当前分支

git checkout -b branchname 创建并切换到新的分支.
这个命令是将git branch newbranchgit checkout newbranch合在一起的结果。

这个时候分支是本地分支,并没有提交到服务器上去,如果这个分支已经被创建,这个命令会失败,这个时候,如果想要重置这个分支,需要使用-B参数。

  1. (1)创造 index 和 工作区 都有变动的场景
  2. $ git status
  3. On branch master
  4. Changes to be committed:
  5. (use "git reset HEAD <file>..." to unstage)
  6. modified: t5.txt
  7. Changes not staged for commit:
  8. (use "git add <file>..." to update what will be committed)
  9. (use "git checkout -- <file>..." to discard changes in working directory)
  10. modified: t2.txt
  11. 查看分支
  12. $ git branch
  13. b1
  14. * master
  15. 从 master 切换到 b1
  16. $ git checkout b1
  17. error: Your local changes to the following files would be overwritten by checkout:
  18. t5.txt
  19. Please commit your changes or stash them before you switch branches.
  20. Aborting
  21. 只提到了 index 中的文件 t5.txt,那是不是我把他提交了就可以了呢?
  22. $ git commit -m 'add'
  23. $ git checkout b1
  24. Switched to branch 'b1'
  25. M t2.txt
  26. $ git branch
  27. * b1
  28. master
  29. $ git status
  30. On branch b1
  31. Changes not staged for commit:
  32. (use "git add <file>..." to update what will be committed)
  33. (use "git checkout -- <file>..." to discard changes in working directory)
  34. modified: t2.txt
  35. no changes added to commit (use "git add" and/or "git commit -a")
  36. 由此可见 index 中的变动会阻止切换分支,那是因为一旦切换了,index 的变动就会消失;而工作区的变动会体现在切换后的分支上。
  37. (2)重新创造 index 和 工作区 都有变动的场景,然后使用 -f 来强制切换分支呢?
  38. $ git status
  39. On branch master
  40. Changes to be committed:
  41. (use "git reset HEAD <file>..." to unstage)
  42. modified: t2.txt
  43. Changes not staged for commit:
  44. (use "git add <file>..." to update what will be committed)
  45. (use "git checkout -- <file>..." to discard changes in working directory)
  46. modified: a/b/c/t3.txt
  47. $ git checkout -f b1
  48. Switched to branch 'b1'
  49. $ git status
  50. On branch b1
  51. nothing to commit, working tree clean
  52. 切回 master 看一看
  53. $ git checkout master
  54. Switched to branch 'master'
  55. $ git status
  56. On branch master
  57. nothing to commit, working tree clean
  58. 可见 -f 参数会强制切换分支,并清除掉 index 和 工作区的全部变动!!
  59. 那么,如果既要切换分支,又希望全部修改在切换后的分支中体现,那么只需要将 index 中的变动撤销即可(git reset HEAD filename),这样之前add进来的文件就成了Unstaged,然后切换分支即可。

放弃修改

如果不指定切换到哪个分支,那就是切换到当前分支,虽然HEAD的指向没有变化,但是后面的两个恢复过程依然会执行,于是就可以理解为放弃index和工作区的变动。但是出于安全考虑 git 会保持 index 的变动不被覆盖。

1、只放弃工作区的改动,index 保持不变,其实就是从当前 index 恢复 工作区:

放弃工作区中全部的修改

git checkout .

放弃工作区中某个文件的修改:

git checkout -- filename

先使用 git status 列出文件,然后 git checkout -- app/Http/Controllers/Read/Read3Controller.php

2、强制放弃 index 和 工作区 的改动:

git checkout -f

这是不可逆的操作,会直接覆盖,但是还是很有用的,有时候想放弃这些改动,使用svn的时候可以直接把文件删掉再update,但是使用git就不能这么操作,使用 git checkout 可以满足这一点。
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/458084
推荐阅读
相关标签
  

闽ICP备14008679号