Git 合并冲突以始终获取最新文件
如何在没有提示的情况下通过获取最新文件(最新更改的时间戳)来始终解决 git 冲突?
我正在使用 git 后端构建一个同步脚本,我从来不想真正合并文件,只是用最后编辑/删除/添加的副本覆盖所有旧副本。
编辑:对于最新文件,我的意思是包含该文件的最新提交。
How can I make a git conflict ALWAYS get resolved by taking the newest file (latest changed timestamp), completley without a prompt?
I am building a syncscript with git backend, and I never want to actually merge a file, just override all older copies with the one that was edited/deleted/added last.
Edit: With newest file, I mean newest commit containing that file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想出了这个小合并驱动程序,它可以满足我的需求。出于我的目的,它被硬编码到“master”分支和“origin”远程。我不知道如何使这些部分动态化。
简而言之,我使用 git-log 查找最后一次提交,该提交不是合并,格式为 UNIX 时间戳,然后比较它们并与我们或他们的版本运行自定义 git-merge。
作为一个小小的好处,它首先检查是否可以在没有冲突的情况下合并文件。如果可能的话,它会合并两个文件。
I came up this little merge driver that does what I want. For my purpose it is hard coded to the "master" branch and to the "origin" remote. I do not know how to make these parts dynamic.
In short I look for the last commit with git-log that was not a merge, formatted as UNIX timestamp, then I compare them and run a custom git-merge with eiter ours or their version.
As a little bonus, it first makes a check to see if it is possible to merge the file without conflict. If that is possible, it merges both files.
我认为你必须为此编写自己的合并驱动程序。有关如何执行此操作的详细信息,请参阅“git help gitattributes”、“定义自定义合并驱动程序”部分。
I think you will have to write your own merge-driver for this. See "git help gitattributes", the "Defining a custom merge driver" sections for details on how to do just that.
基于@Lilleman 的答案,这个答案很好,但有一个错误:
如果在本地 master 上修改了比 origin/master 更旧的文件,则预期的行为是选择 origin/master,但如果您在本地 master 之上添加提交,它将选择该文件的本地(旧版本)。
git log 需要使用一个额外的参数来执行,即文件路径。
这里有一个更好的版本,包括可变分支名称,它有点老套,但它可以
合并
git-merge-newest
findup 使用
Based on @Lilleman answer which is good but has a bug:
if a file is modified on local master which is older than origin/master the expected behaviour is to pick origin/master but if you add a commit on top of local master it will pick the local (older version) of the file.
git log needs to be executed with an extra argument which is the file path.
here a better version including variable branch names it's a bit hacky but it works
merge
git-merge-newest
findup used
如果有人来这里寻找解决方案以避免解决自动生成的文件(例如
package-lock.json
、pubspec.lock
、*.pbxproj< /code>,然后您可以创建一个定义合并策略的
.gitattributes
文件查看有关 git 属性的更多信息
If anyone came here searching for a solution to avoid resolve conflicts arose from automatically generated files such as
package-lock.json
,pubspec.lock
,*.pbxproj
, then you can create a.gitattributes
file defining a merge strategySee more info on git attributes