Git – 切换分支引起冲突

huangapple go评论48阅读模式
英文:

Git - Switching branches causes conflicts

问题

当我在我的开发分支上并拉取最新代码,然后切换到一个功能分支时,尽管没有未提交的更改,我遇到了许多合并冲突。

更奇怪的是,这些更改似乎不是来自开发分支,而是来自一个早已不存在的提交,而文件自那时以来已经更改了数十次。

我尝试过保留当前分支的更改,然后将功能分支合并到开发分支,我以为这会解决冲突,但在分支合并后再次发生。

英文:

When I'm on my develop branch and pull the latest, and then switch to a feature branch I get a bunch of merge conflicts despite there being no uncommitted changes.

What's even more strange is that the changes aren't coming from the develop branch, they seem to be from an old commit that's long gone and the files since changed a dozen times.

I have tried to keep the current branches changes and then merge the feature branch into develop, I thought this would resolve the conflict, but it happens again after the branches get merged.

答案1

得分: 1

这是你有一个文件没有在当前分支上跟踪,但在另一个分支上有跟踪的原因。

输入 git status 你应该会注意到有未跟踪的文件。

这通常不会成为问题,因为当你切换分支时,这些文件通常会被保留,就好像 Git 甚至没有注意到它们一样。

不幸的是,你想切换到的分支已经跟踪了该文件,因此会覆盖它。由于切换可能导致代码丢失,Git 阻止了这个操作。

要解决这个问题:

  • 如果你不关心未跟踪的文件,可以在切换之前删除它们。
  • 如果你关心未跟踪的文件,并且它们需要在当前分支上,可以在切换之前添加并提交它们。
  • 如果你关心未跟踪的文件但不希望它们出现在当前分支上,请使用 git stash 将它们保存在一个临时分支中,然后可以使用 git stash pop 将它们“粘贴”到另一个分支上(在执行之前请查看相关信息)。
英文:

It's because you have a file that is not tracked on your current branch but that is tracked on your other branch.

Type git status and you should notice there are untracked files.

This should not be a problem, as those files are usually kept when you switch branches: it's like if they weren't even noticed by Git.

Unfortunately, the branch you want to switch to has tracked that file and therefore will override it. As you could possibly lose some code by switching, Git prevents you from doing this action.

To solve this problem:

  • If you don't care about the untracked files, delete them before switching.
  • If you care about the untracked files and they need to be in the current branch, add and commit them before switching.
  • If you care about the untracked files but don't want them in the current branch, use git stash to save them in a temporary branch that you can "paste" in another branch with git stash pop (look into it before doing it).

huangapple
  • 本文由 发表于 2023年5月25日 21:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定