【问题标题】:How to Pull from github and keep local file without resolving merge conflicts如何从 github 拉取并保留本地文件而不解决合并冲突
【发布时间】:2013-03-13 16:14:03
【问题描述】:

我想从 github master full 中提取来更新我当前的工作目录。在我的工作目录中存在我处理过的一个名为 form.html 的文件,并且在 github 原始主分支上存在一个旧版本。

 git pull origin master

当我从主人那里拉出来时,我要求解决冲突。我不想解决合并冲突,而是希望我的本地分支中的文件 form.html 完全替换原始主分支(远程)上的 form.html

实现这一目标的最佳方法是什么?谢谢

【问题讨论】:

  • 作为解决冲突的一部分,为什么不将整个新文件放置到位?

标签: git github merge


【解决方案1】:

假设你已经提交了你的 form.html 文件...

在您执行 'git pull origin master' 并发生冲突后,您可以根据本地版本轻松解决冲突。拉取后执行以下操作:

git checkout --ours -- <files in conflict>
git add <files in conflict>
git commit -m 'resolved with --ours'
git push origin master

【讨论】:

    【解决方案2】:

    如果您还没有提交本地文件,您可以(遵循“git pull keeping local changes”的想法):

    git stash
    git pull
    git stash pop
    # if conflict on pop:
    git checkout --theirs -- form.html
    

    但如果您的文件已提交,请查看“Is it possible to exclude specific commits when doing a git merge?”,并使用 .gitattributes 声明正确的合并:

    form.html merge=ours
    

    【讨论】:

    • git checkout --theirs --form.html 的作用是什么?
    • @RyanPerera 以防在stash pop 期间发生冲突,checkout --theirs 确保您取回隐藏的东西,而不是拉取的东西。它将用您保存的内容覆盖拉取引入的修改。
    猜你喜欢
    • 2020-01-21
    • 1970-01-01
    • 2019-07-23
    • 2016-03-04
    • 2020-12-15
    • 2012-04-20
    • 2016-10-27
    • 2018-01-30
    • 1970-01-01
    相关资源
    最近更新 更多