Git:目前与私人远程仓库合并/冲突。如何告诉Git只使用我的本地文件?

时间:2012-09-28 02:08:55

标签: git merge conflict merge-conflict-resolution

尝试使用个人项目来学习/学习git。只有我和一个远程git仓库,一些提交,我陷入了失败的合并。我的很多文件现在都有Git合并冲突标记。

我怎么告诉git把所有东西扔掉,只是用我的?

我进入我所处状态的一个具体例子:

echo A new file > myFile.txt             # example file
git add myFile.txt                       # add file
git commit                               # commit changes
git push                                 # push changes to remote repo
echo A conflicting edit > myFile.txt     # oh, no, forgot some changes
git add myFile.txt                       # add again
git commit --amend                       # amend previous commit
git push                                 # fails. Git suggests to do a pull first
git pull origin HEAD                     # "Automatic merge failed" Now what?
                                         # Just use what I have locally!

2 个答案:

答案 0 :(得分:17)

git checkout --ours . # checkout our local version of all files
git add -u            # mark all conflicted files as merged/resolved
git commit            # commit the merge

有一个混乱的替代品可以打破使用相同远程原点的其他人的回购。如果您是唯一使用它的人,请考虑它:

git reset --hard HEAD # undo that failed merge
git push --force      # replace everything remote with local

解释(现在我更了解git
之所以发生这种情况,是因为修改提交更改了“历史”。在本地执行此操作是安全的,因为它不会影响其他任何人。但是,修改已经推送的提交影响其他回购,并且不安全。

答案 1 :(得分:6)

您的GUI可能只是设置--strategy=oursgit merge -s ours <branch>)。这将执行合并,引用两个提交作为父项,但保持整个目录状态。

您的另一个选择是使用git merge -s recursive -X ours <branch>,它会尝试从两个分支机构引入文件,但在发生冲突时会更喜欢您的版本。

Docs

使用以下演示shell脚本可以看到两种不同的样式:

#!/bin/sh

mkdir gittest
cd gittest
git init

git checkout master
echo "Line one" > bar
git add bar
git commit -m "Original commit"

git checkout -b fork1
echo "Line one and something" > bar
echo "Line two" > bam
git add bar bam
git commit -m "Fork1 commit."

git checkout master
git checkout -b fork2
echo "Line one and other stuff" > bar
echo "Line three" > baz
git add bar baz
git commit -m "Fork2 commit."

git checkout fork1
if [ "$1" = "ours" ]; then
  # `ls gittest` => bam bar
  # `cat gittest/bar` => Line one and something
  git merge -s ours fork2
else
  # `ls gittest` => bam bar baz
  # `cat gittest/bar` => Line one and something
  git merge -X ours fork2
fi