简体   繁体   English

Git pull覆盖并且不会在同一分支上合并或确认冲突(master)

[英]Git pull overwrites and does not merge or acknowledge conflict on same branch (master)

Background: 背景:

I've got what I perceive to be a simple scenario. 我有我认为的简单场景。 I've committed changes locally and now I want to merge what's on the remote, in the same branch (ahead of me) into the local, working directory. 我已经在本地提交了更改,现在我想将远程的,在同一个分支(在我之前)的内容合并到本地的工作目录中。

git branch -vv --list --all gives the following: git branch -vv --list --all给出以下内容:

  master                79d9d4e [origin/master: behind 7] Footprint UI working
  remotes/origin/HEAD   -> origin/master
  remotes/origin/master a86a1a9 Added sample data to webpage

There's one file in particular that I'd like to merge. 特别是我想合并一个文件。 Here's the diff: git diff --stat a86a1a9 79d9d4e views/footprint.handlebars 这是diff: git diff --stat a86a1a9 79d9d4e views/footprint.handlebars

views/footprint.handlebars | 65 +++++++++++++++++++++++++++++++++--------------------------------
 1 file changed, 33 insertions(+), 32 deletions(-)

But when I run git pull , the version of the file in my local commit is overwritten by that of the remote. 但是当我运行git pull ,我本地提交中的文件版本会被远程的文件覆盖。 In more verbose terms: 用更详细的术语:

$ git fetch -v origin  

From github.com:githubusername/foo
 = [up to date]      master     -> origin/master
$ git merge origin  
Updating 79d9d4e..a86a1a9
Fast-forward
...
 views/footprint.handlebars    | 65 ++++++++++++++++++++++++++++++++---------------------------------
...
 6 files changed, 220 insertions(+), 59 deletions(-)
 create mode 100644 static/search.js
 create mode 100644 views/search.handlebars

I've read over the following posts: 我已阅读以下帖子:

  1. git merge overwrites contents git merge覆盖内容
  2. Git merge master into development branch is overwriting, not merging Git merge master进入开发分支是覆盖,而不是合并

And have tried these commands: 并尝试过这些命令:

  1. git pull --rebase overwrites the local version of the file git pull --rebase覆盖文件的本地版本
  2. git merge -s recursive -X ours overwrites local version of file git merge -s recursive -X ours覆盖了本地版本的文件
  3. git merge -s ours prompts for commit message and then overwrites remote changes git merge -s ours提示提交消息,然后覆盖远程更改
  4. git rebase remotes/origin/master says that it will "replay my work on top of" head, but still overwrites it git rebase remotes/origin/master表示它将“重播我的工作”,但仍会覆盖它
  5. git merge --no-ff --no-commit literally reports "Automatic merge went well; stopped before committing as requested" when it's overwritten the file git merge --no-ff --no-commit字面报告“自动合并顺利;在提交请求之前停止”,当它被覆盖文件时

(After running each of the above, I checked the file in question and, noting that it was overwritten, ran git reset --hard master@{"5 minutes ago"} ) (运行上述各项后,我检查了有问题的文件,并注意到它已被覆盖,运行git reset --hard master@{"5 minutes ago"}

$ git config --list  

redential.helper=osxkeychain
user.name=My Name
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.precomposeunicode=true
remote.origin.url=git@github.com:githubusername/foo.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
user.email=email@address.com
remote.heroku.url=https://git.heroku.com/foo.git
remote.heroku.fetch=+refs/heads/*:refs/remotes/heroku/*
$ git log --oneline --decorate --simplify-by-decoration --all -10  

a86a1a9 (origin/master, origin/HEAD) Added sample data to webpage
79d9d4e (HEAD -> master) Footprint UI working
c53160d Initial commit
$git log --format="%h %ci %cn" views/footprint.handlebars

79d9d4e 2019-03-12 19:04:08 -0400 chb
fada3fa 2019-03-10 13:59:41 -0700 JA
9641499 2019-03-08 16:48:14 -0800 JA
1759509 2019-03-08 12:32:08 -0800 GitHub
bfe443e 2019-03-07 16:41:18 -0800 JA

git version 2.17.2 (Apple Git-113)

Question: 题:

Why is this conflict not being flagged as such by git , with the appropriate markers ( <<<<<<< HEAD , ======= , >>>>>>> ) being added to the relevant file? 为什么这个冲突没有被git标记,相应的标记( <<<<<<< HEAD=======>>>>>>> )被添加到相关文件中?

I suspect a misunderstanding about what a merge is supposed to do. 怀疑对合并应该做什么有误解。 (mind the emphasis, and bear with me) (注意重点,并忍受我)

Let's clear it up, if any. 如果有的话,让我们清楚一点。

We'd better focus on the file views/footprint.handlebars . 我们最好关注文件views/footprint.handlebars Was it changed between the initial commit and your local commit? 它是在初始提交和本地提交之间更改的吗? Between the initial commit and origin/master ? 在初始提交和origin/master Both? 都?

If you didn't change it, the fact that after the merge your file reflects their changes is expected. 如果没有更改它,那么合并后您的文件会反映其更改的事实是预期的。

If they didn't change it and your changed version is overwritten with an "older" version from initial commit after the merge, now that's an odd behaviour to investigate. 如果他们没有更改它,并且您的更改版本被合并后的初始提交中的“旧”版本覆盖,那么现在这是一个奇怪的行为进行调查。

If both you and them have brought changes to the file, but at different, non-conflicting points , then the end result should contain changes from both sides, without conflict. 如果您和他们都对文件进行了更改,但是在不同的非冲突点 ,则最终结果应包含来自双方的更改,而不会发生冲突。 I wouldn't call that "overwritten". 我不会称之为“覆盖”。


Lastly, if you in fact want to keep this file in the exact state it is in your recent local commit, you'd have to, like you hinted at like a very tedious thing, make the merge conflict, but that's not a big deal : 最后,如果你实际上想要保持这个文件处于你最近的本地提交中的确切状态,你必须像你暗示的一样非常乏味的事情,使合并冲突,但这不是什么大不了的事:

git checkout master
git fetch
git merge --no-commit origin/master
git checkout HEAD -- views/footprint.handlebars
git commit -am "Kept file views/footprint.handlebars as per commit 79d9d4e"

Give us a bit more feedback and I'll edit to adjust. 给我们一些反馈,我会编辑调整。 (probably tomorrow, though ;-) (可能明天,但是;-)

Addition after comments : 评论后补充:

I'm pretty much agreeing with Useless' comment down below . 我几乎无用的言论下同意以下 I didn't get from your original question that there was back-and-forth on the very file in-between you and your coworker's commits (as you added here ). 我没有从你原来的问题中得到你和你的同事提交之间的文件来回反复(正如你在这里添加的那样)。

The solution to restore both your needed changes and theirs might now be to check file history, then rebuild the contents carefully from the initial shared state, but depending on your context it's hard to advise on the best formula. 现在,恢复所需更改和他们所需更改的解决方案可能是检查文件历史记录,然后从初始共享状态小心地重建内容,但根据您的上下文,很难建议最佳公式。 If the coworker is available to sort it out with you, you're likely to avoid a lot of headaches. 如果同事可以和你一起解决,你可能会避免很多麻烦。

Epilogue addition : 结语补充:

Sorry for the bitter sweet ending, I totally understand that you would have prefered a more intellectually satisfying answer/solution. 对于甜蜜的甜蜜结局感到抱歉,我完全理解你会更喜欢更智能的答案/解决方案。 I thank you for the bounty and everyone else for all the insightful comments. 感谢所有富有洞察力的评论,感谢您和其他所有人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM