git如何选择性合并_git - 如何挑选一系列提交并合并到另一个分支?

以上所有选项都将提示您解决合并冲突。 如果要合并为团队提交的更改,则很难解决开发人员的合并冲突并继续进行。 但是,“git merge”将一次性完成合并,但您无法通过一系列修订作为参数。 我们必须使用“git diff”和“git apply”命令来进行转速的合并范围。 我观察到如果补丁文件包含太多文件的差异,“git apply”将失败,因此我们必须为每个文件创建一个补丁然后应用。 请注意,该脚本将无法删除源分支中删除的文件。 这是一种罕见的情况,您可以从目标分支手动删除此类文件。 如果它不能应用补丁,“git apply”的退出状态不为零,但是如果你使用-3way选项,它将回退到3路合并,你不必担心这个失败。

下面是脚本。

enter code here

#!/bin/bash

# This script will merge the diff between two git revisions to checked out branch

# Make sure to cd to git source area and checkout the target branch

# Make sure that checked out branch is clean run "git reset --hard HEAD"

START=$1

END=$2

echo Start version: $START

echo End version: $END

mkdir -p ~/temp

echo > /tmp/status

#get files

git --no-pager diff --name-only ${START}..${END} > ~/temp/files

echo > ~/temp/error.log

# merge every file

for file in `cat ~/temp/files`

do

git --no-pager diff --binary ${START}..${END} $file > ~/temp/git-diff

if [ $? -ne 0 ]

then

# Diff usually fail if the file got deleted

echo Skipping the merge: git diff command failed for $file >> ~/temp/error.log

echo Skipping the merge: git diff command failed for $file

echo "STATUS: FAILED $file" >> /tmp/status

echo "STATUS: FAILED $file"

# skip the merge for this file and continue the merge for others

rm -f ~/temp/git-diff

continue

fi

git apply --ignore-space-change --ignore-whitespace --3way --allow-binary-replacement ~/temp/git-diff

if [ $? -ne 0 ]

then

# apply failed, but it will fall back to 3-way merge, you can ignore this failure

echo "git apply command filed for $file"

fi

echo

STATUS=`git status -s $file`

if [ ! "$STATUS" ]

then

# status is null if the merged diffs are already present in the target file

echo "STATUS:NOT_MERGED $file"

echo "STATUS: NOT_MERGED $file$" >> /tmp/status

else

# 3 way merge is successful

echo STATUS: $STATUS

echo "STATUS: $STATUS" >> /tmp/status

fi

done

echo GIT merge failed for below listed files

cat ~/temp/error.log

echo "Git merge status per file is available in /tmp/status"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值