367

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.

2
  • Do you mean you don't want to lose your uncomitted code in 9-sign-in-out? Commented May 15, 2011 at 6:09
  • @Mauvis: I already committed my codes in the branch 9-sign-in-out. Commented May 19, 2011 at 9:39

8 Answers 8

750

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
Sign up to request clarification or add additional context in comments.

3 Comments

Mark, I finally understood what you were saying after I got the error again to merge now. I'm, however having this error::::::::::::: Sayanee:twitter sweska$ git checkout master error: Your local changes to the following files would be overwritten by checkout: webrat.log Please, commit your changes or stash them before you can switch branches. Aborting::::::::::::::::: any way, to user the webrat.log in the branch and make it merge with the master?
@Sayanee: that's a different error, arising from different circumstances, and it would be better to ask a new question about that if you're confused by it. (Briefly, though, git's stopping you from switching branches since that would overwrite uncommitted changes in webrat.log.)
I used to just rm the whole thing and reclone. But this is nicer.
60

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 

1 Comment

I think its much cleaner to reset the merge like Mark suggested below instead of forcing the checkout.
15

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.

Comments

6

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.

Comments

5

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.

Comments

2

my issue was (master|REBASE 1/1)

this command worked for me

 git rebase --skip

Comments

0

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.

Comments

-10

git commit -m "Merged master fixed conflict."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.