【问题标题】:Git: currently in merge/conflict with private remote repo. How to tell Git to just use my local files?Git:目前与私有远程仓库合并/冲突。如何告诉 Git 只使用我的本地文件?
【发布时间】:2012-09-19 21:43:24
【问题描述】:

尝试在个人项目中使用/学习 git。只有我和一个远程 git repo,一些提交,我陷入了失败的合并。我的很多文件现在也有 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!

【问题讨论】:

  • 是的。没有找到一个简单的“使用我的本地一切”命令。
  • Git 合并冲突在这个超级线程中得到解决:stackoverflow.com/questions/161813/…
  • 是的,找到了那个帖子。它没有帮助我。他们谈到使用我无法使用的工具 (git mergetool),查看详细文档,关于如何避免合并的一般建议等。当我在 Google 上搜索“Git use local”时,我一直在寻找有关如何使用 git 的文章在我没有远程来源的计算机上。真让人生气。你是说没有单行命令告诉 git “使用我的本地”?
  • 这与“git merge megathread”的“完全重复”相去甚远。这是一个特定的合并场景,因此,最好在这里拥有它,而不是迷失在具有 10 个响应和 100 个 cmets 的巨型线程的噪音中。
  • @mehaase 谢谢!这正是我所希望的

标签: git merge conflict merge-conflict-resolution


【解决方案1】:

您的 GUI 可能只是设置 --strategy=ours (git 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

【讨论】:

  • +1 用于合并策略。更多关于合并策略的好信息stackoverflow.com/questions/366860/…
  • 旁注:也可以尝试git merge -s recursive -X ours 接受自动合并的更改,但如果有冲突,请始终使用您的本地副本。
  • 没用。我得到:错误:“合并”是不可能的,因为您有未合并的文件。提示:在工作树中修复它们,提示:然后使用 'git add/rm ' 作为提示:适合标记分辨率并进行提交,提示:或使用 'git commit -a'。致命:由于未解决的冲突而退出。
  • @Kache,看起来您仍处于合并中间,此时如果不解析所有文件,您将无法继续。要取消合并,请运行git reset --hard HEAD,然后尝试我的步骤。您还可以在pastebin.com/ScLzuHf7 上尝试展示两种不同“我们的”策略的演示脚本。
  • 像这样重置只会让我回到原来的位置,即我的本地仓库认为一切都很好并且更新了。无论如何,我找到了我需要的答案并将其编辑到我的问题中。我想正式提交答案,但他们关闭了我的问题。 =\
【解决方案2】:
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

有一个混乱的替代方案可以破坏使用相同远程来源的其他所有人的 repo。仅当您是唯一使用它的人时才考虑它:

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

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

【讨论】:

  • man git-merge 在“如何解决冲突”部分提供了替代方案。 git merge --abort
猜你喜欢
  • 2013-03-20
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 2012-10-06
  • 2017-04-15
相关资源
最近更新 更多