Git合并错误

时间:2011-05-15 05:47:37

标签: git branch git-merge git-branch

我有一个名为9-sign-in-out的git分支,代码完美,我希望将其转换为主代码。我现在在主分公司。<​​/ p>

$ 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分支转换为主分支?也许git rebase?但我不想丢失9-sign-in-out分支中的代码。

5 个答案:

答案 0 :(得分:464)

值得了解这些错误消息的含义 - needs mergeerror: you need to resolve your current index first表示合并失败,并且这些文件存在冲突。如果你已经确定你想要做的任何合并都是一个坏主意,你可以把事情恢复正常:

git reset --merge

但是,您应该解决这些合并冲突as described in the git manual


一旦你通过任何一种方法处理了这个问题,你就应该能够检查9-sign-in-out分支。如wRAR's answer中所述,将9-sign-in-out重命名为master的问题在于,如果您与任何人共享了以前的主分支,则会给他们带来问题,因为如果两个分支的历史分歧,你将发表改写的历史。

基本上,您要做的是将主题分支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

答案 1 :(得分:37)

git checkout -f 9-sign-in-out # change branch, discarding all local modifications
git branch -M master # rename the current branch to master, discarding current master

答案 2 :(得分:5)

git status中所述,

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

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

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:-2)

git commit -m&#34;合并主固定冲突。&#34;