git branch/tag切換與合並


  幾個關於分支應用的命令:

git branch <分支名稱> -- 創建分支
git tag -- 給某一次固定的提交做標記
git checkout <分支名稱> -- 分支之間進行切換
git stash -- 切換分支之前保存本地修改
git merge -- 合並分支

  簡單應用:

當提交的次數比較多的時候,如果想查找歷史的提交:

git log --oneline --graph --decorate --all
==================================
$ git log --oneline --decorate --graph --all
* 244afc0 (HEAD -> master) create c
* 8612d29 create a and b
* 6f5eaaf delete all
* d695139 deleta all
* c8ed67d modify a
* cb9052e init

新建分支,在分支上修改,再查看歷史的提交記錄:

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git branch test

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git checkout test
A       d
Switched to branch 'test'

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git log --oneline --decorate --graph --all
* 244afc0 (HEAD -> test, master) create c
* 8612d29 create a and b
* 6f5eaaf delete all
* d695139 deleta all
* c8ed67d modify a
* cb9052e init

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git rm a
rm 'a'

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git add .

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git commit -m "delete file a"
[test c9b3fab] delete file a
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename a => d (100%)

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git log --oneline --decorate --graph --all
* c9b3fab (HEAD -> test) delete file a
* 244afc0 (master) create c
* 8612d29 create a and b
* 6f5eaaf delete all
* d695139 deleta all
* c8ed67d modify a
* cb9052e init

如果相對當前的commit打個tag,不用hash就是默認當前commit,用歷史的hash(5-7位足夠,git會自動補全)就是對歷史某次commit進行tag:

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git log --oneline --decorate --graph --all
* c9b3fab (HEAD -> test) delete file a
* 244afc0 (master) create c
* 8612d29 create a and b
* 6f5eaaf delete all
* d695139 deleta all
* c8ed67d modify a
* cb9052e init

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (test)
$ git checkout master
Switched to branch 'master'

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git tag "v0" 8612d29

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git log --oneline --decorate --graph --all
* c9b3fab (test) delete file a
* 244afc0 (HEAD -> master) create c
* 8612d29 (tag: v0) create a and b
* 6f5eaaf delete all
* d695139 deleta all
* c8ed67d modify a
* cb9052e init

該種tag是輕量級的,也可以用annotated tag,方便共享,用git tag可以方便查看所有的tag

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git tag -a "INITIAL_ANNOTATED_COMMIT" 8612d29

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git tag
INITIAL_ANNOTATED_COMMIT
v0

git checkout tag名稱 可以切換到那個tag,但是會處於“detached HEAD”狀態,commit之后的歷史記錄會被清除(暫時的理解是,這種單個commit上的提交跟分支根本不是一個維度),所以需要這么做時,常見的做法是加參數 -b來創建並切換到新的分支

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)
$ git checkout v0
Note: checking out 'v0'.

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 8612d29... create a and b

BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo ((INITIAL_ANNOTATED_COMMIT))
$ git checkout v0 -b new_fixed_tag
Switched to a new branch 'new_fixed_tag'

 當做過一些具體的操作之后,我們會發現,在切換分支之前,必須要把可以提交的代碼都提交了,但如果我們希望保持修改之前的狀態是不是就只能放棄本次修改呢?其實,我們可以用git stash 將本次修改存放起來,工作區與暫存區都與原先的狀態一致,可以切換到新的分支,當我們返回這個分支時,又可以根據保存的名稱將之前修改的內容復原出來。此部分略過。

 另外,merge以及它的放棄也略過。

  


免責聲明!

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



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