git分支管理和stash

c3tc3tc3t發表於2015-11-06

1 建立分支
$ git branch 分支名

2切換到分支
$ git checkout 分支名

3檢視提交
git log --oneline --decorate --graph --all

4 輕量級tag 是一個commit而已
git tag "v0" hash值

5 annoation tag 其實是一個tag物件,tag物件指向了一個commit
git tag -a "tagName" hash值

6檢視tag
git tag

7設定別名
$ git config --global alias.lol "log --oneline --decorate --graph --all"
$ git lol

 分離頭指標

1 用git checkout切換到一個commit時,這時候head指向了一個commit而不是一個分支名時候,這時處於分離頭指標狀態
,在新這個切換的commit上工作,工作歷史會丟失。

rudy-Pc :: ~/git_checkout ‹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 目前位於 329f7cb... init on master first

 


所以解決辦法。是執行git checkout -b branch_name,基於這個commit建立分支,然後在進行工作
這個命令相當於 git branch 和git checkout的組合

rudy-Pc :: ~/git_checkout ‹329f7cb› » git checkout -b fixv0
切換到一個新分支 'fixv0'

在這個fixv0上工作

 

stash

1 在一個分支上進行了操作,並提交到了暫存去,這時候如果checkout到其他分支就會發生,新checkout的分支內容將原來沒有commit的覆蓋掉。
這時要麼將原來的commit掉,要麼使用git stash儲存起來

git checkout master
error: Your local changes to the following files would be overwritten by checkout:
master.txt
Please, commit your changes or stash them before you can switch branches.
Aborting

 

 

2暫存區,這樣臨時操作的工作去內容和暫存區就會儲存了
git stash save -a “stash info

 

3 當切換到了其他分支工作完成後,再想切換回原來的stash儲存的哪個分支,需要將stash的東西還原

git stash 檢視


加入--index是將暫存區也還原
git stash pop --index stash@{0}


4 git apply 不清除stash歷史,同時將stash狀態還原,git pop會清除stash歷史
git stash apply --index stash@{0}


5手動刪除 stash歷史

git stash drop stash@{0}


6一次性清理所有stash 歷史
git stash clear

 

合併分支

1 想將a 分支內容合併到b分支上

需要線切換到b分支
git checkout b
然後再將a分支內容合併過來
git merge a


2衝突問題

發生衝突的檔案處在工作區,決掉衝突檔案後。需要新增到暫存去然後再commmit

3取消合併
git merge --abort

 

相關文章