以前用svn管理代碼,圖形界面使用TortoiseSVN,svn update出現沖突時,在log窗口點擊右鍵就可以直接選擇“以自己的為准”或“以倉庫的為准”。
切換到git后,苦於沒有好用的圖形工具(SmartGit還湊合),一直使用命令行,更新代碼出現沖突時,沒有上述兩個選項,感覺很不習慣,於是自己寫了兩個小函數來實現上述功能。
加入到~/.bash_profile就可以愉快的使用了,Windows,Linux都可以。
function resolve_conflict_using_mine { git status --porcelain | egrep '^UU' | cut -d ' ' -f 2 | xargs git checkout --theirs git add -A git rebase --continue } function resolve_conflict_using_theirs { git status --porcelain | egrep '^UU' | cut -d ' ' -f 2 | xargs git checkout --ours git add -A git rebase --continue }
遇到沖突時,敲resolve_conflict_using_mine就可以“以自己的為准”解決沖突,反之敲resolve_conflict_using_theirs可以“以倉庫的為准”解決沖突。
當然,用“自己的”,“倉庫的”來描述git模型並不准確。明白就好~
上面兩個函數是不是看着有點奇怪,ours和theirs感覺反了?
因為我習慣使用rebase而不是merge,全局配置了pull.rebase=true,所以ours和theirs是反的。
git help checkout 查看文檔,可以看到有以下描述:
Note that during git rebase and git pull --rebase, ours and theirs may appear swapped; --ours gives the version from the branch the changes are rebased onto, while --theirs gives the version from the branch that holds your work that is being rebased.
使用merge而不是rebase的同學,可以使用下面的函數。
function resolve_conflict_using_mine { git status --porcelain | egrep '^UU' | cut -d ' ' -f 2 | xargs git checkout --ours git add -A git merge --continue } function resolve_conflict_using_theirs { git status --porcelain | egrep '^UU' | cut -d ' ' -f 2 | xargs git checkout --theirs git add -A git merge --continue }