git checkout


git checkout 相關命令總結

1.git checkout       

  表示核查工作區相對於版本庫修改過的文件

2. git checkout  + 分支名 

  表示切換分支

3. git checkout  -b  分支名

  表示以當前分支的當前狀態創建新分支並切換到新分支    -b 表示創建新分支

4. git checkout -b 分支名  commitID

  表示以當前分支的commitID提交節點創建新的分支並切換到新分支。此時工作區的內容和切換分之前commitID提交節點的內容一樣

5. git checkout  commitID

  是以指定的提交節點創建了一個臨時性分支,此臨時性分支可用於做實驗性修改

6.git checkout  filename 

  當沒有提交版本號時將工作區的指定文件的內容恢復到暫存區的狀態

     git checkout  . 

  將工作區的所有文件的內容恢復到暫存區的狀態

 7.    git checkout <commit> filename 

  當有提交版本號時,表示將工作區和暫存區都恢復到版本庫指定提交版本的指定文件的狀態,此時HEAD指針不變,此時的狀態相當於把工作區的內容修改到指定版本的文件內容后,再把修改的內容添加到暫存區。因此git checkout <commit> filename后,可以直接執行git commit而不需要先執行git add

 

 

操作詳細如下

1.git checkout 后面不加任何參數。表示核查工作區相對於版本庫修改過的文件

 

2. git checkout  + 分支名       表示切換分支

 

 

3. git checkout  -b  分支名   表示以當前分支的當前狀態創建新分支並切換到新分支    -b 表示創建新分支

 

 4. git checkout -b 分支名  commitID   表示以當前分支的commitID提交節點創建新的分支並切換到新分支。此時工作區的內容和切換分之前commitID提交節點的內容一樣

 

5. git checkout  commitID   此命令有些特殊,此命令執行后,工作區的內容會變成commitID提交節點的內容,但時HEAD不位於任何分支上,處於游離狀態。

更准確的說,此命令是以指定的提交節點創建了一個臨時性分支,被別HEAD指向了這個臨時分支,你可以在這個臨時分支上修改內容並且提交內容。

但是,一但你從臨時分支切換到其他分支,這個臨時分支就會消失。

這種臨時性分支主要用來做一些實驗性的修改,實驗結束后,只要切換回原分支即可,原分支不會有任何改變。臨時性分支上的改動並不會反映到原分支。

但是你如果想把這臨時性分支上的改動反映到原分支上,可以通過git checkout  -b  新分支名  命令  以臨時性分支的當前狀態創建一個永久性分支,再把這個分支合並到原先的分支,然后再刪除這個分支即可。

執行git checkout  commitID命令

 

 將臨時性分支反映到原分支master上

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (master)
$ git log --pretty=oneline
515bbaac44e128a4e3c1f956bb962359090b7dc6 (HEAD -> master, newbranch) AA
cc19754ef1ea7f9e0414eed31592af952094116e (branch2, branch1) Merge branch 'tem'
065be4955a8e3e2e1d7bdf8b181f58a2066b5289 CC
5da296c5bf7d7f674f4a128d1ff77a79f4ee0311 BB
da2b161d76b0fb0b07b56cc4d229f92fa9424d5a AAA
a3239e33bcfb422c3f9fc72a622e50f207a7e61d add l3
22ff17b9dce0a7a2fd9f6c3760cef876e733b0a2 add test2.txt
ae61384fee29e82c3975cae1a03122eb69eee31b (newbranch2) add l1
1d000b4679adca98a168328dad432a9216877442 add file

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (master)
$ git checkout a3239e
Note: checking out 'a3239e'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at a3239e3 add l3

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn ((a3239e3...))
$ git branch
* (HEAD detached at a3239e3)
  branch1
  branch2
  master
  newbranch
  newbranch2
  newbranch3

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn ((a3239e3...))
$ git add .

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn ((a3239e3...))
$ git commit -m "add l4"
[detached HEAD 3cfcff2] add l4
 1 file changed, 2 insertions(+), 1 deletion(-)

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn ((3cfcff2...))
$ git checkout -b newbranch4
Switched to a new branch 'newbranch4'

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (newbranch4)
$ git log --pretty=online
fatal: invalid --pretty format: online

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (newbranch4)
$ git log --pretty=oneline
3cfcff2024cfd555dffe9fcdffd6d0ded4c4b43f (HEAD -> newbranch4) add l4
a3239e33bcfb422c3f9fc72a622e50f207a7e61d add l3
22ff17b9dce0a7a2fd9f6c3760cef876e733b0a2 add test2.txt
ae61384fee29e82c3975cae1a03122eb69eee31b (newbranch2) add l1
1d000b4679adca98a168328dad432a9216877442 add file

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (newbranch4)
$ git branch
  branch1
  branch2
  master
  newbranch
  newbranch2
  newbranch3
* newbranch4

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (newbranch4)
$ git checkout master
Switched to branch 'master'

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (master)
$ git merge newbranch4
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
Automatic merge failed; fix conflicts and then commit the result.

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (master|MERGING)
$ git add .

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (master|MERGING)
$ git commit
[master b13df65] Merge branch 'newbranch4'

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (master)
$ git branch -d newbranch4
Deleted branch newbranch4 (was 3cfcff2).

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (master)
$ git branch
  branch1
  branch2
* master
  newbranch
  newbranch2
  newbranch3

 

 當從臨時性分支切換到其他分支后,如果還想保存臨時性分支的內容,那么可以通過以下命令創建一個新分支來保存臨時性分支的內容

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn ((5f43910...))
$ git branch
* (HEAD detached from a3239e3)
  branch1
  branch2
  master
  newbranch
  newbranch2
  newbranch3

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn ((5f43910...))
$ git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  5f43910 add l4 l5

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 5f43910

Switched to branch 'master'

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (master)
$ git branch newbanch5 5f43910

w_gao@YRL47-80972 MINGW64 /c/w_gao/git learn (master)
$ git branch
  branch1
  branch2
* master
  newbanch5
  newbranch
  newbranch2
  newbranch3

 

 6.git checkout  filename 當沒有提交版本號時將工作區的內容恢復到暫存區的狀態

 7.    git checkout <commit>  filename 當有提交版本號時,表示將工作區和暫存區都恢復到版本庫指定提交版本的指定文件的狀態,此時HEAD指針不變,此時的狀態相當於把工作區的內容修改到指定版本的文件內容后,再把修改的內容添加到暫存區。因此git checkout <commit> --filename后,可以直接執行git commit而不需要先執行git add

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM