git冲突处理

以前用svn管理代码,图形界面使用TortoiseSVN,svn update出现冲突时,在log窗口点击右键就可以直接选择“以自己的为准”或“以仓库的为准”。

切换到git后,苦于没有好用的图形工具(SmartGit还凑合),一直使用命令行,更新代码出现冲突时,没有上述两个选项,感觉很不习惯,于是自己写了两个小函数来实现上述功能。

加入到~/.bash_profile就可以愉快的使用了,Windows,Linux都可以。

1
2
3
4
5
6
7
8
9
10
11
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的同学,可以使用下面的函数。

1
2
3
4
5
6
7
8
9
10
11
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
}

 

posted @   编程柚子  阅读(3731)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示