尽管存在差异,但 Git 合并报告“已经是最新的”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/634546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:15:10  来源:igfitidea点击:

Git merge reports "Already up-to-date" though there is a difference

gitmerge

提问by Charles Darke

I have a git repository with 2 branches: master and test.

我有一个带有 2 个分支的 git 存储库:master 和 test。

There are differences between master and test branches.

主分支和测试分支之间存在差异。

Both branches have all changes committed.

两个分支都提交了所有更改。

If I do:

如果我做:

git checkout master
git diff test

A screen full of changes appears showing the differences. I want to merge the changes in the test branch and so do:

出现一个充满变化的屏幕,显示差异。我想合并测试分支中的更改,所以这样做:

git merge test

But get the message "Already up-to-date"

但是收到消息“已经是最新的”

However, examining files under each different branch clearly shows differences.

但是,检查每个不同分支下的文件清楚地显示出差异。

What's the problem here and how do I resolve it?

这里有什么问题,我该如何解决?

采纳答案by Bombe

The message “Already up-to-date” means that all the changes from the branch you're trying to merge have already been merged to the branch you're currently on. More specifically it means that the branch you're trying to merge is a parent of your current branch. Congratulations, that's the easiest merge you'll ever do. :)

消息“Already up-to-date”意味着您尝试合并的分支中的所有更改都已合并到您当前所在的分支中。更具体地说,这意味着您尝试合并的分支是当前分支的父分支。恭喜,这是您做过的最简单的合并。:)

Use gitkto take a look at your repository. The label for the “test” branch should be somewhere below your “master” branch label.

用于gitk查看您的存储库。“test”分支的标签应该在你的“master”分支标签下方。

Your branch is up-to-date with respect to its parent. According to merge there are no new changes in the parent since the last merge. That does not mean the branches are the same, because you can have plenty of changes in your working branch and it sounds like you do.

您的分支相对于其父分支是最新的。根据合并,自上次合并以来父级没有新的变化。这并不意味着分支是相同的,因为您可以在工作分支中进行大量更改,这听起来像您一样。

Edit 10/12/2019:

2019 年 10 月 12 日编辑:

Per Charles Drake in the comment to this answer, one solution to remediate the problem is:

根据 Charles Drake 对此答案的评论,解决问题的一种解决方案是:

git checkout master
git reset --hard test

This brings it back to the 'test' level.

这使它回到“测试”级别。

Then do:

然后做:

git push --force origin master

in order to force changes back to the central repo.

为了强制更改回中央回购。

回答by ACarter

This often happens to me when I know there are changes on the remote master, so I try to merge them using git merge master. However, this doesn't merge with the remote master, but with your local master.

当我知道远程主机上有更改时,这经常发生在我身上,因此我尝试使用git merge master. 但是,这不会与远程 master 合并,而是与您的本地 master 合并。

So before doing the merge, checkout master, and then git pullthere. Then you will be able to merge the new changes into your branch.

因此,在进行合并之前,先检出 master,然后git pull在那里。然后您将能够将新更改合并到您的分支中。

回答by Marek Stanley

Say you have a branch masterwith the following commit history:

假设您有一个master具有以下提交历史记录的分支:

A -- B -- C -- D

Now, you create a branch test, work on it, and do 4 commits:

现在,您创建一个分支测试,对其进行处理,并执行 4 次提交:


                 E -- F -- G -- H
                /
A -- B -- C -- D

master's head points to D, and test's head points to H.

master的头指向 D,而test的头指向 H。

The "Already up-to-date" message shows up when the HEAD of the branch you are merging into is a parent of the chain of commits of the branch you want to merge. That's the case, here: Dis a parent of E.

当您要合并到的分支的 HEAD 是您要合并的分支的提交链的父级时,将显示“已经是最新的”消息。就是这样,这里:DE.

There is nothing to merge from testto master, since nothing has changed on mastersince then. What you want to do here is literally to tell Git to have master's head to point to H, so master's branch has the following commits history:

没有什么可以从 合并testmaster,因为从那时起没有任何变化master。您在这里要做的就是告诉 Git 将master头指向 H,因此 master 的分支具有以下提交历史记录:

A -- B -- C -- D -- E -- F -- G -- H

This is a job for Git command reset. You also want the working directory to reflect this change, so you'll do a hardreset:

这是 Git 命令的工作reset。您还希望工作目录反映此更改,因此您将进行重置:

git reset --hard H

回答by Stefan Pintilie

What works for me, let's say you have branch1 and you wanna merge it into branch2.

什么对我有用,假设你有 branch1 并且你想将它合并到 branch2 中。

You open git command line go to root folder of branch2 and type:

您打开 git 命令行转到 branch2 的根文件夹并键入:

git checkout branch1
git pull branch1
git checkout branch2
git merge branch1
git push

If you have comflicts you don't need to do git push, but first solve the conflits and then push.

如果您有冲突,则不需要执行 git push,而是先解决冲突,然后再推送。

回答by FoxDeploy

This happens because your local copy of the branch you want to merge is out of date. I've got my branch, called MyBranchand I want to merge it into ProjectMaster.

发生这种情况是因为您要合并的分支的本地副本已过期。我有我的分支,被调用MyBranch,我想将它合并到ProjectMaster.

_>git status
On branch MyBranch-Issue2
Your branch is up-to-date with 'origin/MyBranch-Issue2'.

nothing to commit, working tree clean

_>git merge ProjectMaster
Already up-to-date.

But I knowthat there are changes that need to be merged!

我知道有一些变化需要合并!

Here's the thing, when I type git merge ProjectMaster, git looks at my local copy of this branch, which might not be current. To see if this is the case, I first tell Git to check and see if my branches are out of date and fetch any changes if so using, uh, fetch. Then I hop into the branch I want to merge to see what's happening there...

事情是这样的,当我输入 时git merge ProjectMaster,git 会查看这个分支的本地副本,它可能不是 current。要查看是否是这种情况,我首先告诉 Git 检查我的分支是否过时,如果是,则使用 uh, 获取任何更改fetch。然后我跳进我想合并的分支,看看那里发生了什么......

_>git fetch origin

_>git checkout ProjectMaster
Switched to branch ProjectMaster
**Your branch is behind 'origin/ProjectMaster' by 85 commits, and can be fast-forwarded.**
  (use "git pull" to update your local branch)

Ah-ha! My local copy is stale by 85 commits, that explains everything! Now, I Pulldown the changes I'm missing, then hop over to MyBranchand try the merge again.

啊哈!我的本地副本因 85 次提交而陈旧,这说明了一切!现在,我Pull删除了我缺少的更改,然后跳到MyBranch并再次尝试合并。

_>git pull
Updating 669f825..5b49912
Fast-forward

_>git checkout MyBranch-Issue2
Switched to branch MyBranch-Issue2
Your branch is up-to-date with 'origin/MyBranch-Issue2'.

_>git merge ProjectMaster
Auto-merging Runbooks/File1.ps1
CONFLICT (content): Merge conflict in Runbooks/Runbooks/File1.ps1

Automatic merge failed; fix conflicts and then commit the result.

And now I have another issue to fix...

现在我还有另一个问题要解决......

回答by VonC

A merge is always between the current HEAD and one or more commits (usually, branch head or tag),
and the index file must match the tree of HEAD commit (i.e. the contents of the last commit) when it starts out.
In other words, git diff --cached HEADmust report no changes.

The merged commit is already contained in HEAD. This is the simplest case, called "Already up-to-date."

合并总是在当前 HEAD 和一个或多个提交(通常是分支头或标签)之间,
并且索引文件在开始时必须匹配 HEAD 提交的树(即最后一次提交的内容)。
换句话说,git diff --cached HEAD必须报告没有变化。

合并的提交已包含在HEAD. 这是最简单的情况,称为“已经是最新的”。

That should mean the commits in test are already merged in master, but since other commits are done on master, git diff testwould still give some differences.

这应该意味着测试中的提交已经合并到 master 中,但是由于其他提交是在 master 上完成的,git diff test仍然会产生一些差异。

回答by cdupont

This happened to me because strangely GIT thought that the local branch was different from the remote branch. This was visible in the branch graph: it displayed two different branches: remotes/origin/branch_name and branch_name.

这发生在我身上,因为奇怪的是 GIT 认为本地分支与远程分支不同。这在分支图中是可见的:它显示了两个不同的分支:remotes/origin/branch_name 和 branch_name。

The solution was simply to remove the local repo and re-clone it from remote. This way GIT would understand that remotes/origin/branch_name>and branch_name are indeed the same, and I could issue the git merge branch_name.

解决方案只是删除本地存储库并从远程重新克隆它。这样 GIT 就会明白 remotes/origin/branch_name> 和 branch_name 确实是一样的,我可以发出git merge branch_name.

rm <my_repo>
git clone <my_repo>
cd <my_repo>
git checkout <branch_name>
git pull
git checkout master
git merge <branch_name>

回答by John Murphy

Be sure to checkout the branch you want to merge first and then pull it (so your local version matches the remote version).

请务必先检出要合并的分支,然后再拉取它(以便您的本地版本与远程版本匹配)。

Then checkout back to your branch you want to do the merge on and your git merge should work.

然后结帐回到您想要合并的分支,您的 git merge 应该可以工作。

回答by Justas

git merge origin/masterinstead git merge masterworked for me. So to merge master into feature branch you may use:

git merge origin/master而是git merge master对我来说有效。因此,要将 master 合并到 feature 分支中,您可以使用:

git checkout feature_branch
git merge origin/master

回答by bubjavier

happend to me and was sent to this page, not sure if I had the same scenario, but mine was me trying to "re-merge" that "test" branch.

发生在我身上并被发送到这个页面,不确定我是否有相同的场景,但我的是我试图“重新合并”那个“测试”分支。

So I previously merged it but I intentionally exclude some specific changes during that merge, so it clearly has some diff between branches. I was then trying to re-merge it because I realize/forget that I should have and wanted to add a particular change/file that I have previously excluded and I was hoping if I do a merge again would show all changes that I excluded before, but I was wrong and I get the "Already up-to-date" message instead.

所以我之前合并了它,但我故意排除了合并过程中的一些特定更改,因此它在分支之间显然存在一些差异。然后我试图重新合并它,因为我意识到/忘记我应该添加一个我以前排除的特定更改/文件,我希望如果我再次进行合并会显示我之前排除的所有更改,但我错了,我收到了“已经是最新的”消息。

Upon reading @Bombe's comment/answer, he is right, and git I think behaves that way, so what I did was to make hard backup of the files on test branch, then checkout the master branch and manually paste the files in it and commit it as if it was new changes.

在阅读@Bombe 的评论/答案后,他是对的,我认为 git 的行为就是这样,所以我所做的是对测试分支上的文件进行硬备份,然后签出主分支并手动将文件粘贴到其中并提交就好像这是新的变化。

am not sure if this is the right way or could help others having this same issue, but it did provide solution to my particular case.

我不确定这是否是正确的方法或可以帮助其他有同样问题的人,但它确实为我的特殊情况提供了解决方案。