Git 合并冲突以始终获取最新文件

发布于 2024-12-07 14:58:28 字数 150 浏览 0 评论 0原文

如何在没有提示的情况下通过获取最新文件(最新更改的时间戳)来始终解决 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一曲琵琶半遮面シ 2024-12-14 14:58:28

我想出了这个小合并驱动程序,它可以满足我的需求。出于我的目的,它被硬编码到“master”分支和“origin”远程。我不知道如何使这些部分动态化。

#!/usr/bin/env sh
if git merge-file -p -q "$2" "$1" "$3" > /dev/null;
        then git merge-file "$2" "$1" "$3";
        else
                MINE=$(git log --format="%ct" --no-merges master -1);
                THEIRS=$(git log --format="%ct" --no-merges origin/master -1);
                if [ $MINE -gt $THEIRS ];
                        then git merge-file -q --ours "$2" "$1" "$3";
                        else git merge-file -q --theirs "$2" "$1" "$3";
                fi
fi

简而言之,我使用 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.

#!/usr/bin/env sh
if git merge-file -p -q "$2" "$1" "$3" > /dev/null;
        then git merge-file "$2" "$1" "$3";
        else
                MINE=$(git log --format="%ct" --no-merges master -1);
                THEIRS=$(git log --format="%ct" --no-merges origin/master -1);
                if [ $MINE -gt $THEIRS ];
                        then git merge-file -q --ours "$2" "$1" "$3";
                        else git merge-file -q --theirs "$2" "$1" "$3";
                fi
fi

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.

舞袖。长 2024-12-14 14:58:28

我认为你必须为此编写自己的合并驱动程序。有关如何执行此操作的详细信息,请参阅“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.

红焚 2024-12-14 14:58:28

基于@Lilleman 的答案,这个答案很好,但有一个错误:
如果在本地 master 上修改了比 origin/master 更旧的文件,则预期的行为是选择 origin/master,但如果您在本地 master 之上添加提交,它将选择该文件的本地(旧版本)。
git log 需要使用一个额外的参数来执行,即文件路径。

这里有一个更好的版本,包括可变分支名称,它有点老套,但它可以

合并

#!/bin/sh
cd `findup .git`
PATH=$PATH:$( dirname "${BASH_SOURCE[0]}" )
HEAD=`git rev-parse --abbrev-ref HEAD`

echo "* merge=newest" > .gitattributes
sed -ie '/merge "newest"/,+2d' .git/config
echo "[merge \"newest\"]" >> .git/config
echo -e "\tname = Merge by newest commit" >> .git/config
echo -e "\tdriver = git-merge-newest %O %A %B %L %P $HEAD $1" >> .git/config
git merge $1
sed -ie '/merge "newest"/,+2d' .git/config
rm .gitattributes
cd - 

git-merge-newest

#!/bin/sh
if git merge-file -p -q "$2" "$1" "$3" > /dev/null;
        then git merge-file "$2" "$1" "$3";
        else
                MINE=$(git log --format="%ct" --no-merges "$6" -1 $5);
                THEIRS=$(git log --format="%ct" --no-merges "$7" -1 $5);
                if [ $MINE -gt $THEIRS ]; then
                  git merge-file -q --ours "$2" "$1" "$3" >/dev/null
                else
                  git merge-file -q --theirs "$2" "$1" "$3">/dev/null
                fi
fi

findup 使用

#!/bin/sh

pwd="`pwd`"
start="$pwd"
while [ ! "$pwd" -ef .. ]; do
[ -e "$1" ] && echo -n "$pwd" && exit
cd .. || exit 1
pwd="`pwd`"
done
exit 1

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

#!/bin/sh
cd `findup .git`
PATH=$PATH:$( dirname "${BASH_SOURCE[0]}" )
HEAD=`git rev-parse --abbrev-ref HEAD`

echo "* merge=newest" > .gitattributes
sed -ie '/merge "newest"/,+2d' .git/config
echo "[merge \"newest\"]" >> .git/config
echo -e "\tname = Merge by newest commit" >> .git/config
echo -e "\tdriver = git-merge-newest %O %A %B %L %P $HEAD $1" >> .git/config
git merge $1
sed -ie '/merge "newest"/,+2d' .git/config
rm .gitattributes
cd - 

git-merge-newest

#!/bin/sh
if git merge-file -p -q "$2" "$1" "$3" > /dev/null;
        then git merge-file "$2" "$1" "$3";
        else
                MINE=$(git log --format="%ct" --no-merges "$6" -1 $5);
                THEIRS=$(git log --format="%ct" --no-merges "$7" -1 $5);
                if [ $MINE -gt $THEIRS ]; then
                  git merge-file -q --ours "$2" "$1" "$3" >/dev/null
                else
                  git merge-file -q --theirs "$2" "$1" "$3">/dev/null
                fi
fi

findup used

#!/bin/sh

pwd="`pwd`"
start="$pwd"
while [ ! "$pwd" -ef .. ]; do
[ -e "$1" ] && echo -n "$pwd" && exit
cd .. || exit 1
pwd="`pwd`"
done
exit 1
远昼 2024-12-14 14:58:28

如果有人来这里寻找解决方案以避免解决自动生成的文件(例如 package-lock.jsonpubspec.lock*.pbxproj< /code>,然后您可以创建一个定义合并策略的 .gitattributes 文件

package-lock.json merge=union

查看有关 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 strategy

package-lock.json merge=union

See more info on git attributes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文