简体   繁体   English

合并到 master 时,Git 忽略分支上的更改

[英]Git ignores changes on branch when merging to master

Let's say I have the branch "staging" and I had created some files on it and committed them.假设我有分支“暂存”,并且我在其上创建了一些文件并提交了它们。 I merged the "staging" branch to master accidentally.我不小心将“暂存”分支合并到 master 分支。

Because it was too soon to merge (I still had work to do on the "staging" branch), I tried to revert the merge commit (using git revert ; I don't know the exact command because I used PyCharm's Git UI).因为现在合并还为时过早(我在“暂存”分支上还有工作要做),我尝试恢复合并提交(使用git revert ;我不知道确切的命令,因为我使用了 PyCharm 的 Git UI)。 Now those changes are reverted on the "master" branch and everything's good.现在这些更改在“主”分支上恢复,一切都很好。

After finishing my work on the "staging" branch, I tried to merge it to master using these commands:在“暂存”分支上完成工作后,我尝试使用以下命令将其合并到 master:

git checkout master
git pull
git merge staging

And this is git's response:这是 git 的回应:

Auto-merging app/static/css/style.bundle.rtl.css
CONFLICT (content): Merge conflict in app/static/css/style.bundle.rtl.css
Removing app/static/js/main.js
Removing app/static/css/util.css
..
(All of my new files were listed here as removed)
..
Removing app/static/css/main.css
Automatic merge failed; fix conflicts and then commit the result.

Because of that old merge commit's reversion, git has detected all my changes on pre-existing files as conflicts and deleted all new files.由于旧的合并提交的还原,git 已将我对预先存在的文件的所有更改检测为冲突,并删除了所有新文件。

I aborted this operation by git merge --abort , and now my files are all back.我通过git merge --abort中止了这个操作,现在我的文件都回来了。 But I'm stuck and cannot merge this branch into master.但是我被卡住了,无法将此分支合并到 master 中。 What should I do?我该怎么办?

I tried:我试过:

  • Merging using git merge staging -X theirs , but this ignored all changes that have been made on master by other developers使用git merge staging -X theirs ,但这忽略了其他开发人员在 master 上所做的所有更改
  • Merging from the other hand: git checkout staging && git merge master -s ours , same problem as above从另一方面合并: git checkout staging && git merge master -s ours ,与上述相同的问题

When you revert, you tell Git that you don't want these changes (ever (again)).当你恢复时,你告诉 Git 你不想要这些更改(永远(再次))。 This also means that merging a branch with a revert will remove those changes or not merge them again.这也意味着将分支与还原合并将删除这些更改或不再合并它们。

In other words: when merging, only "new" changes are merged, not old ones (ie changes before a branch started).换句话说:合并时,只合并“新”更改,而不是旧更改(即分支开始之前的更改)。

If you decide that you actually need the reverted changes, you have to revert the revert, eg如果您决定确实需要还原的更改,则必须还原还原,例如

git checkout branch
git revert hash-of-revert-commit
git checkout master
git merge branch

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

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