>[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里面的指向而已哈。
總結:當前工作是屬于分支的,如果你有未做完的工作(工作區不是干凈的:有未添加到暫存區的修改或者暫存區有內容),那么切換分支會失敗,除非是那種情況(“它們就是一個人”)。請做完工作(保證工作區是干凈的)或者隱藏現場在切換分支。
- 說明
- git配置
- git與github的關系
- 基礎概念
- git命令
- git init
- git status
- git diff
- git log
- git reflog
- git add
- git commit
- git reset
- git checkout
- git rm
- git stash
- git remote
- git push
- git clone
- git branch
- git fetch
- git merge
- git rebase
- git pull
- git tag
- 建立版本庫
- 分支合并
- 遠程庫別名
- Pull requests
- 擴展知識
- 功能文件
- 差異看法
- 注意細節
- github移動端
- git工作系統理解
- 倉庫嵌套問題
- 倉庫的使用問題
- 常用命令
- 學習資料
- 學習總結
- 示例文件
- README.md
- CONTRIBUTING.md
- .gitignore
- coding
- 大小寫問題
- 如何貢獻
- 使用賬號密碼clone
- git目錄分析
- HEAD
- 代碼部署問題
- 開發流程
- 指定公鑰文件