Modified files in a git branch are spilling over into another branch

GitGit Branch

Git Problem Overview


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

For example:

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 the master branch even 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 something here.

Has anyone got a clue stick?

Git Solutions


Solution 1 - Git

> Why is that the file is shown as modified in master branch even though it was modified in git-build branch?

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

Solution 2 - Git

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.

Solution 3 - Git

This is the default behaviour of git.

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

Solution 4 - Git

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.

Solution 5 - Git

  • It is not like git branches are dependent on each other but also they do not have a complete code base separately for each branch either.
  • For each commit, Git stores an object that contains a pointer to the changes. So each branch points to its own latest commit and HEAD points to the branch currently you are in.
  • When you switch the branch, the HEAD pointer points to that particular commit of the branch. So if there are modified files, the default behavior is to copy over them.

You can do the following things to overcome this issue.

  1. Use -f option to ignore the changes.

If you want to save the changes:

  1. Commit the changes locally in the same branch and then switch the branch.

  2. Use git stash, switch the branch, do your work, switch back to the original branch and do git stash apply.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajkumar SView Question on Stackoverflow
Solution 1 - GitGarethView Answer on Stackoverflow
Solution 2 - GitPeter BurnsView Answer on Stackoverflow
Solution 3 - GitMarkoView Answer on Stackoverflow
Solution 4 - GitGreg HewgillView Answer on Stackoverflow
Solution 5 - GitRavitejaView Answer on Stackoverflow