简体   繁体   English

合并后Git提交消失了

[英]Git commit disappeared after merge

I'm working on a project with some collaborators, each having a remote copy of the master branch, which may or may not be up-to-date with the origin master branch. 我正在与一些协作者合作进行一个项目,每个协作者都有master分支的远程副本,该副本可能与原始master分支有关,也可能没有。

Suppose one of the collaborators has an out-of-date copy due to a commit X , commits some Y and tries to push it to the origin branch. 假设其中一个协作者由于提交X而提交了一个过时的副本,提交了一些Y并尝试将其推到origin分支。 It fails and the collaborator merges the changes and pushes again, this time successfully. 它失败,并且协作者合并更改并再次成功推送,这一次成功。

Now, when looking at the history of the repository, commit X cannot be found anymore, except the changes of it are now in another commit called Merge branch 'master' of https://.... . 现在,当查看存储库的历史记录时,不再可以找到提交X ,除了它的更改现在位于另一个名为Merge branch 'master' of https://....提交中。

Is this normal behaviour of git? 这是git的正常行为吗? This makes tracking down changes hard. 这使得很难跟踪更改。 Is there something that can be done to avoid this issue? 有什么办法可以避免这个问题?

Thanks in advance! 提前致谢!

This is completely normal behavior for Git when using git merge . 当使用git merge时,这对于Git来说是完全正常的行为。 Git merge replaces the commits in the source branch with a single merge commit in the destination branch. Git merge将源分支中的提交替换为目标分支中的单个合并提交。

One option to preserve history is to use git rebase instead. 保留历史记录的一种方法是改用git rebase In a rebase, your collaborator would first bring in the changes from the remote master branch, and then recommit his work on top. 在重新设置基础时,您的协作者将首先从远程master分支引入更改,然后再重新提交其工作。 This would leave his local master ahead of the remote, and he can simply push his changes in. Consider the following diagram: 这会将他的本地master置于远程控制之前,而他只需将其更改推入即可。请考虑下图:

remote: A -- B -- C -- X
local:  A -- B -- C -- Y

After doing git rebase origin/master the diagram would look like this: 在执行git rebase origin/master该图将如下所示:

remote: A -- B -- C -- X
local:  A -- B -- C -- X -- Y'

where Y' is your collaborator's original Y commit slightly modified. 其中Y'是您的协作者的原始Y提交,略有修改。 Now your collaborator is in the enviable position of being able to push his work directly onto the remote master branch. 现在,您的协作者处于令人羡慕的位置,可以将其工作直接推到远程master分支上。 After doing git push origin master from his master branch, the diagram would look like this: 从其master分支执行git push origin master之后,该图将如下所示:

remote: A -- B -- C -- X -- Y'
local:  A -- B -- C -- X -- Y'

Now your collaborator's commit appear intact in the remote master branch. 现在,您的协作者的提交在远程master分支中完整显示。

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

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