Gaurav Kumar
Gaurav Kumar

Reputation: 1091

Switch git branch while keeping current branch in conflicted state

I have a branch master. I created a new branch feature, made some changes to its files fileA and fileB. Meanwhile some changes were made to master also in the same files fileA and fileB. Now I merged master into feature. It shows conflicts in file fileA and fileB. Let's suppose, Now I decide to keep the feature branch in the same conflicted state and want to move to master.

git checkout master gives an error saying below:

fileA: needs merge

fileB: needs merge

error: you need to resolve your current index first

How may I switch to master while keeping feature as it is.

EDIT Basically what happened is, I merged from master and some conflicts occurred which made my changes on feature useless. So I wanted to go back to master and cut out a new branch to work on the feature. At the same time, I dont want my earlier feature branch to be removed as it has some code that I may have to write again, hence I want to keep it to refer it as and when needed.

Upvotes: 0

Views: 179

Answers (2)

Brad Mace
Brad Mace

Reputation: 27906

You can't leave it as is, but you can set your uncommitted changes aside and get back to the state you're currently in later.

  • git stash save will set your uncommitted changes aside
  • git checkout master to switch to master
  • do whatever you want on master
  • when you're done, git checkout feature
  • git stash show will list all the changes you've stashed
  • git stash pop will restore the last stashed changes

Upvotes: 2

user229044
user229044

Reputation: 239471

You can't.

You cannot switch away from a branch and have it retain an unmerged or conflicted state. That state applies only to your working directory, not to a branch specifically.

There are changes in your working directory that are not tracked by Git. You need to resolve those changes and commit them, otherwise they would be lost when you switch branches.

Upvotes: 1

Related Questions