blueFast
blueFast

Reputation: 44361

Move branches, but do not overwrite modified files

I am in branch wip and want to move to branch dev. I have some not-committed changes in the working dir.

$ git checkout dev 
error: Your local changes to the following files would be overwritten by checkout:
        .idea/runConfigurations/celery_beat.xml
Please, commit your changes or stash them before you can switch branches.
Aborting

Ok, I get it: that file won't merge. And I do not want to lose my changes to it. What I want is:

  1. Checkout the dev branch
  2. For the files that are not possible to get merged, keep the version in my working dir (do not try to merge)

Alternatively, I sometimes want this:

  1. Checkout the dev branch
  2. Do not overwrite any of the files which have changes in the working dir. That is, the non-comitted files that I have while in the wip branch should stay as they are, the rest of the working dir should be populated from the dev branch. A simple git diff will show me what changes I have after moving to the dev branch.

EDIT

The way I would solve this now is:

  1. git status
  2. Get list of changed files
  3. Copy those files somewhere outside the repo
  4. git checkout -- .
  5. git checkout dev
  6. Get back those files into the repo
  7. git diff gives me now the changes as compared to dev

Does git have a built-in mechanism to do this?

Upvotes: 0

Views: 76

Answers (1)

ElpieKay
ElpieKay

Reputation: 30868

#stash the changes to ".idea/runConfigurations/celery_beat.xml"
git stash

#switch to dev without conflicts or overwriting
git checkout dev

#restore ".idea/runConfigurations/celery_beat.xml" to the version before the switch
git checkout stash@{0} -- .idea/runConfigurations/celery_beat.xml

#now ".idea/runConfigurations/celery_beat.xml" is staged, in order to remove it from the index:
git reset HEAD -- .idea/runConfigurations/celery_beat.xml

#if you'd like to drop the stash entry
git stash drop

But still I have to say, it's not a good idea to track .idea.

Upvotes: 1

Related Questions