Git 合并错误

发布于 2024-11-07 11:24:16 字数 573 浏览 0 评论 0原文

我有一个名为 9-sign-in-out 的 git 分支,其代码工作完美,我想将其变成主分支。我目前在 master 分支上。

$ git branch
9-sign-in-out
* master

我正在尝试切换到 9-sign-in-out 分支,但它不允许我这样做:

$ git checkout 9-sign-in-out
app/helpers/application_helper.rb: needs merge
config/routes.rb: needs merge
error: you need to resolve your current index first

知道如何忽略所有主分支错误并将 9 -sign-in-out 分支到master?也许 git rebase ?但我不想丢失 9-sign-in-out 分支中的代码。

I have a git branch called 9-sign-in-out with perfectly working code, and I want to turn it into the master. I'm currently on the master branch.

$ git branch
9-sign-in-out
* master

I'm trying to switch to 9-sign-in-out branch, but it doesn't allow me to:

$ git checkout 9-sign-in-out
app/helpers/application_helper.rb: needs merge
config/routes.rb: needs merge
error: you need to resolve your current index first

Any idea how can I ignore all the master branch errors and turn the 9-sign-in-out branch into the master? Maybe git rebase? But I don't want to lose the code in 9-sign-in-out branch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

岁月无声 2024-11-14 11:24:16

值得理解这些错误消息的含义 - 需要合并错误:您需要首先解析当前索引表示合并失败,并且这些文件中存在冲突。如果您确定您尝试执行的任何合并毕竟都是一个坏主意,您可以使用以下命令使事情恢复正常:

git reset --merge

但是,否则您应该解决这些合并冲突,如 git 手册中所述


一旦您通过任一技术处理了该问题,您应该能够签出 9-sign-in-out 分支。按照 wRAR 的答案是,如果您与任何人共享了以前的主分支,这会给他们带来问题,因为如果两个分支的历史记录出现分歧,您'将出版重写的历史。

本质上,您想要做的是将主题分支 9-sign-in-out 合并到 master 中,但完全保留主题分支中的文件版本。您可以通过以下步骤来完成此操作:

# Switch to the topic branch:
git checkout 9-sign-in-out

# Create a merge commit, which looks as if it's merging in from master, but is
# actually discarding everything from the master branch and keeping everything
# from 9-sign-in-out:
git merge -s ours master

# Switch back to the master branch:
git checkout master

# Merge the topic branch into master - this should now be a fast-forward
# that leaves you with master exactly as 9-sign-in-out was:
git merge 9-sign-in-out

It's worth understanding what those error messages mean - needs merge and error: you need to resolve your current index first indicate that a merge failed, and that there are conflicts in those files. If you've decided that whatever merge you were trying to do was a bad idea after all, you can put things back to normal with:

git reset --merge

However, otherwise you should resolve those merge conflicts, as described in the git manual.


Once you've dealt with that by either technique you should be able to checkout the 9-sign-in-out branch. The problem with just renaming your 9-sign-in-out to master, as suggested in wRAR's answer is that if you've shared your previous master branch with anyone, this will create problems for them, since if the history of the two branches diverged, you'll be publishing rewritten history.

Essentially what you want to do is to merge your topic branch 9-sign-in-out into master but exactly keep the versions of the files in the topic branch. You could do this with the following steps:

# Switch to the topic branch:
git checkout 9-sign-in-out

# Create a merge commit, which looks as if it's merging in from master, but is
# actually discarding everything from the master branch and keeping everything
# from 9-sign-in-out:
git merge -s ours master

# Switch back to the master branch:
git checkout master

# Merge the topic branch into master - this should now be a fast-forward
# that leaves you with master exactly as 9-sign-in-out was:
git merge 9-sign-in-out
如果没有你 2024-11-14 11:24:16

更改分支,放弃所有本地修改

git checkout -f 9-sign-in-out 

将当前分支重命名为 master,放弃当前 master

git branch -M master 

Change branch, discarding all local modifications

git checkout -f 9-sign-in-out 

Rename the current branch to master, discarding current master

git branch -M master 
ぶ宁プ宁ぶ 2024-11-14 11:24:16

按照 git status 中的建议,

Unmerged paths:                                                                                                                                
(use "git add <file>..." to mark resolution)                                                                                                 

    both modified:   a.jl                                  
    both modified:   b.jl

我使用 git add 完成合并,然后 git checkout 工作正常。

as suggested in git status,

Unmerged paths:                                                                                                                                
(use "git add <file>..." to mark resolution)                                                                                                 

    both modified:   a.jl                                  
    both modified:   b.jl

I used git add to finish the merging, then git checkout works fine.

离线来电— 2024-11-14 11:24:16

从 dev 分支切换到 master 分支时我遇到了同样的问题。我所做的就是提交更改并切换到主分支。您可能有未提交的更改。

您也可以尝试这个 git reset --merge 。这可用于解决任何冲突并恢复。

I had the same issue when switching from a dev branch to master branch. What I did was commit my changes and switch to the master branch. You might have uncommitted changes.

And also you can try this git reset --merge as well. This can be used to resolve any conflicts and revert.

執念 2024-11-14 11:24:16

我在尝试从 dev 分支切换到 master 分支时使用 GitHub Desktop 时遇到了这个问题,即使我已经解决了所有合并冲突。

在命令行上输入 git add * 后,我可以再次切换分支。

I got this when using GitHub Desktop when trying to switch from a dev branch to master, even after I had resolved all merge conflicts.

After typing git add * on the command line it allowed me to switch branches again.

酷炫老祖宗 2024-11-14 11:24:16

我的问题是(master | REBASE 1/1)

这个命令对我有用

 git rebase --skip

my issue was (master|REBASE 1/1)

this command worked for me

 git rebase --skip
小巷里的女流氓 2024-11-14 11:24:16

这并不能回答问题中描述的确切场景。它回答了以下场景,您最终会得到相同的错误消息。在这里发帖是因为当谷歌搜索这个链接时,这个SO链接首先出现。


这通常发生在 GitHub Desktop 用户身上。

如果您在远程删除的分支上进行了本地更改,然后从主分支合并,并且已经解决了冲突,那么现在您无法将更改移动到任何其他分支,因为:

您需要首先解析当前索引
needs merge 错误,

只需执行以下操作:

# unset the upstream which is deleted 
- git branch --unset-upstream
# rename the branch
- git branch -m <newname>

从这里您可以执行通常的操作。

This doesn't answer the exact scenario described in the question. It answers the following scenario which you end up with the same error messages. Posting here because this SO link comes up first when Googling for this.


This usually happens for GitHub Desktop users.

If you made changes locally on a branch that's deleted in remote, then merged from master, and already resolved conflicts, and now you cannot move your changes to any other branch because of the;

you need to resolve your current index first,
and needs merge error,

simply do below:

# unset the upstream which is deleted 
- git branch --unset-upstream
# rename the branch
- git branch -m <newname>

From here you can do the usual.

森林散布 2024-11-14 11:24:16

git commit -m“合并主修复冲突。”

git commit -m "Merged master fixed conflict."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文