views:

2942

answers:

5

Hello,

I have got the git branch I'm working on to a nice place. So I make a commit with a useful commit message. I then absentmindedly make minor changes to the code that are not work keeping. I now want to changes branches, but git gives me,

error: You have local changes to "X"; cannot switch branches.

I thought that I could change branches without committing? If so how can I set this up. If not, how do I get out of this problem? I want to ignore the minor changes without committing and just changes branches!

Cheers,

Dan

+2  A: 

If you have made changes to files that Git also needs to change when switching branches, it won't let you. To discard working changes, use:

git reset --hard HEAD

Then, you will be able to switch branches.

Greg Hewgill
+12  A: 

You should either:

  • stash your current change or
  • reset --hard HEAD (if you do not mind losing those minor changes) or
  • checkout -f (When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes. )

You need a clean state to change branches (only if the branch change affects the 'dirty files', as Charles Bailey remarks in the comments)

VonC
"You need a clean state to change branches." is only true if the branch change affects the 'dirty files'.
Charles Bailey
+2  A: 

Follow,

$: git checkout -f

$: git checkout next_branch
simplyharsh
+1  A: 

If you want to discard the changes,

git checkout -- <file>
git checkout branch

If you want to keep the changes,

git stash save
git checkout branch
git stash pop
Jamie Macey
+1  A: 

well, it should be

git stash save
git checkout branch
// do something
git checkout oldbranch
git stash pop
romerun