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 } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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吗?