創(chuàng)建與合并分支
master 主分支
HEAD 當(dāng)前分支
git branch # 查看分支
git branch <name> # 創(chuàng)建分支
git checkout <name> # 切換分支
git checkout -b <name> # 創(chuàng)建與切換分支
git merge <name> # 合并某分支到當(dāng)前分支
git branch -d <name> # 刪除分支
git branch -D <name> # 強行刪除分支
分支沖突
git merge后發(fā)現(xiàn)分支沖突捕发;
git status查看沖突的文件疏旨;
修改文件沖突內(nèi)容;
重新提交扎酷;
通過git log --graph查看分支合并圖
git log --graph
git log --graph --pretty=oneline --abbrev-commit
注意:git log退出方法檐涝,英文狀態(tài)下按鍵q
分支管理
合并分支時,Git一般會用Fast forward模式法挨,但這種模式下谁榜,刪除分支后,會丟掉分支信息凡纳。通過強制禁用Fast forward模式窃植,Git就會在merge時生成一個新的commit,這樣荐糜,從分支歷史上就可以看出分支信息巷怜。
git merge --no-ff -m "merge with no-ff" dev
--no-ff 禁用Fast forward
-m 創(chuàng)建一個新的commit
然后通過git log --graph --pretty=oneline --abbrev-commit
查看分支歷史
bug分支
臨時bug修復(fù):通過git stash
把當(dāng)前工作現(xiàn)場“儲藏”起來;開啟新分支進行bug修復(fù)暴氏,bug修復(fù)完成強制禁用Fast forward模式合并分支延塑,以留下分支信息記錄;刪除bug修復(fù)分支答渔;恢復(fù)儲藏工作关带。
git stash list # 查看stash列表
恢復(fù)臨時儲藏:
git stash apply # 恢復(fù)后,stash內(nèi)容并不刪除
git stash apply stash@{0} #恢復(fù)指定的stash
git stash drop # 刪除stash內(nèi)容
git stash pop # 恢復(fù)并刪除stash內(nèi)容
多人協(xié)作
git remote # 查看遠(yuǎn)程庫的信息
git remote -v # 查看遠(yuǎn)程庫詳細(xì)信息
git pull # 抓取遠(yuǎn)程的新提交
git checkout -b branch -name origin/branch-name # 在本地創(chuàng)建和遠(yuǎn)程分支對應(yīng)的分支沼撕,本地和遠(yuǎn)程分支的名稱最好一致
git branch --set-upstream branch-name origin/branch-name # 建立本地分支和遠(yuǎn)程分支的關(guān)聯(lián)