🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
>[info] git checkout 功能: - 撤销修改 - 切换(检出)分支 ### 撤销修改: git checkout -- file可以丢弃工作区的修改: ``` $ git checkout -- readme.txt ``` 命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况: 1. 一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态; 2. 一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。 >[info] 总之,就是让这个文件回到最近一次git commit或git add时的状态。 ### 切换分支 我们创建`dev`分支,然后切换到`dev`分支: ~~~ $ git checkout -b dev Switched to a new branch 'dev' ~~~ `git checkout`命令加上`-b`参数表示创建并切换,相当于以下两条命令: ~~~ $ git branch dev $ git checkout dev Switched to branch 'dev' ~~~ 从下面的代码可以得出,切换分支并没有改变工作区哦 ~~~ Administrator@USER-20160512VQ MINGW64 ~/desktop/github-note (master) $ git status On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean Administrator@USER-20160512VQ MINGW64 ~/desktop/github-note (master) $ git checkout dev Switched to branch 'dev' Your branch is up-to-date with 'origin/dev'. Administrator@USER-20160512VQ MINGW64 ~/desktop/github-note (dev) $ git status On branch dev Your branch is up-to-date with 'origin/dev'. nothing to commit, working tree clean Administrator@USER-20160512VQ MINGW64 ~/desktop/github-note (dev) $ ls node.txt README.md test test2 test3 Administrator@USER-20160512VQ MINGW64 ~/desktop/github-note (dev) $ vi test Administrator@USER-20160512VQ MINGW64 ~/desktop/github-note (dev) $ git status On branch dev Your branch is up-to-date with 'origin/dev'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: test no changes added to commit (use "git add" and/or "git commit -a") Administrator@USER-20160512VQ MINGW64 ~/desktop/github-note (dev) $ git checkout master Switched to branch 'master' Your branch is up-to-date with 'origin/master'. Administrator@USER-20160512VQ MINGW64 ~/desktop/github-note (master) $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: test no changes added to commit (use "git add" and/or "git commit -a") Administrator@USER-20160512VQ MINGW64 ~/desktop/github-note (master) $ ~~~ 但是当在dev分支修改提交后,再回到master: ~~~ Administrator@USER-20160512VQ MINGW64 ~/desktop/github-note (master) $ vi test Administrator@USER-20160512VQ MINGW64 ~/desktop/github-note (master) $ git checkout dev error: Your local changes to the following files would be overwritten by checkout: test Please commit your changes or stash them before you switch branches. Aborting ~~~ 此时提示: ~~~   错误:您的本地更改以下文件将被检出覆盖:   测试   请提交您的更改或隐藏他们之前切换分支。   流产 ~~~ 结论:当检出分支时发现本地更改会被检出覆盖时就不允许切换分支,除非按照提示解决问题。(即使git add test也还是会报错哦) 咦,怎么感觉这个结论怪怪的呢。 至于上面你用那么长的代码证明的结论已经不攻自破了,现在可以解释了你那么结论的情况了,HEAD->master->commit,HEAD->dev->commit它们指向同一个提交的时,那时dev分支刚从master建立出来,它们都还没有任何提交,它们的指针都同时指向了一个提交,也就是它们的版本库是一模一样的,或者说“它们就是一个人”,所以此时检出分支当然内容相同,让你感觉切换分支好像并没有改变工作区一样,其实这种情况当git发现两个分支是指向同一个引用时根本就没有做“检出(工作区进行任何变化)”操作哦,只是切换一下当前分支而已,即改变/.git/HEAD里面的指向而已哈。 总结:当前工作是属于分支的,如果你有未做完的工作(工作区不是干净的:有未添加到暂存区的修改或者暂存区有内容),那么切换分支会失败,除非是那种情况(“它们就是一个人”)。请做完工作(保证工作区是干净的)或者隐藏现场在切换分支。