Explore Courses Blog Tutorials Interview Questions
+1 vote
in DevOps and Agile by (29.3k points)

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

$ git branch


* master

I'm trying to switch to branch, but it doesn't allow me to:

$ git checkout branch

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 Branch.

1 Answer

+1 vote
by (50.2k points)

This error basically occurs when you've shared your previous master branch with anyone, this will create problems, 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 branch 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 branch

# 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 branch:

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 branch was:

git merge branch

Note: if you think that merge, you were trying to do was a bad idea, then you can put things back to normal with:

git reset --merge

Browse Categories