tags:

views:

1278

answers:

5

Hi,

I am working on a git repository with a master branch and another topic branch. I have switched to topic branch and modified a file. Now if I switched to master branch that same file is shown as modified.

For eg:

git status in git-build branch:

# On branch git-build
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   cvsup_current
#

Switch to master branch

[root@redbull builder_scripts (git-build)]# git co master
M       builder_scripts/cvsup_current
Switched to branch "master"

git status in master branch

[root@redbull builder_scripts (master)]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   cvsup_current
#

Why is that the file is shown as modified in master branch though it was modified in git-build branch. My understanding was that the branches are independent of each other and when I change from one branch to another the changes do not "spill over" from one branch to another. So I am obviously missing some thing here.

Any one to help with a clue stick ?

regards,

raj

+4  A: 

The modified files are not put in the repository until you add and commit them. If you switch back to your topic branch and commit the file, then it won't appear on the master branch.

Greg Hewgill
+8  A: 

This is the default behaviour of git.

You can use -f flag to checkout to do "clean checkout" if you like.

Dev er dev
Be aware that this doesn't keep the changes on top of the branch you're leaving or anything. This will throw away the uncommitted changes (except if the file is untracked).
webmat
A: 

Thanks Marko and Greg for quick answers!

Rajkumar S
+15  A: 

The key to remember is that the file was not modified in the git-build branch. It was only modified in your working copy.

Only when you commit are the changes put back into whichever branch you have checked out

Gareth
Thanks, the answers combined have made this much clear for me!
Rajkumar S
+9  A: 

If you want to temporarily store your changes to one branch while you go off to do work on another, you can use the git stash command. It's one of the amazing little unsung perks of using git. Example workflow:

git stash #work saved
git checkout master
#edit files
git commit
git checkout git-build
git stash apply #restore earlier work

git stash stores a stack of changes, so you can safely store multiple checkpoints. You can also give them names/descriptions. Full usage info here.

Peter Burns