我正在尝试合并两个分支,它们有很多变化,有几个有合并冲突。我使用git mergetool
合并了文件,但后来我意识到我错误地合并了其中的几个文件。我基本上想要返回到这两个文件的冲突状态,这样我就可以重新运行mergetool并纠正我的错误。我不想丢弃我的整个合并,因为它的大部分是正确的。
我试着重置到我的头,然后做git checkout -m other_branch -- my_file
无济于事。我最终重置为HEAD,从另一个分支获取文件,然后对文件执行git add --patch
操作,只暂存我想要的内容。但肯定有更好的方法。
发布于 2009-12-01 05:18:55
您可以使用:
git checkout [--ours|--theirs|--merge] <paths>
检出在要合并的分支上找到的路径,或重新创建冲突的合并。
git-checkout手册页上有更多关于这个问题的信息。
正如Charles Bailey所指出的,当合并的文件已经添加到索引中时,这不起作用。我试了一下,下面是一个脚本,应该可以完成这项工作:
#!/bin/bash
#
# Distributed under the GNU General Public License, version 2.0.
#
# git-goat:
#
# Restore a merge conflict, after it has already been resolved.
# Lifted from contrib/completion/git-completion.bash
__gitdir ()
{
if [ -z "${1-}" ]; then
if [ -n "${__git_dir-}" ]; then
echo "$__git_dir"
elif [ -d .git ]; then
echo .git
else
git rev-parse --git-dir 2>/dev/null
fi
elif [ -d "$1/.git" ]; then
echo "$1/.git"
else
echo "$1"
fi
}
into=$(git describe --all HEAD)
from=$(cat $(__gitdir)/MERGE_HEAD)
base=$(git merge-base $into $from)
case "$1" in --ours|--theirs|--merge) whose=$1; shift; esac
[ -z "$1" ] && echo "fatal: at least one file has to be specified" && exit
for file in "$@"
do
(
echo -e "0 0000000000000000000000000000000000000000\t$file"
git ls-tree $base $file | sed -e "s/\t/ 1\t/"
git ls-tree $into $file | sed -e "s/\t/ 2\t/"
git ls-tree $from $file | sed -e "s/\t/ 3\t/"
) | git update-index --index-info
git checkout ${whose:-"--merge"} $file
done
请注意,我没有测试过这么多。如果您发现任何问题或有其他改进,here是一个可派生的“要点”。
发布于 2014-06-18 02:38:01
如果您意外地接受了冲突解决提示,而没有保存mergetool中的更改,我认为您可以直接隐藏暂存的更改并重做合并。
git stash
git merge <other branch>
https://stackoverflow.com/questions/1677049
复制相似问题