Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-branch

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?

like image 654
Rajkumar S Avatar asked Oct 29 '08 09:10

Rajkumar S


People also ask

How do I discard a modified file in git?

There are two Git commands a developer must use in order to discard all local changes in Git, remove all uncommited changes and revert their Git working tree back to the state it was in when the last commit took place. The commands to discard all local changes in Git are: git reset –hard. git clean -fxd.

Does switching branches change files?

When you switch branches, files that are not tracked by Git will remain untouched. Since Git does not know about new_file. dat , it will not just delete it. The file new_file.

What happens to untracked files when switching branches?

Take away: changing branches does not touch/change/remove untracked or checked in files. Remember that the working directory and index are not 'cleared' before the branch content is loaded into it!


2 Answers

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

like image 184
Gareth Avatar answered Sep 21 '22 10:09

Gareth


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.

like image 38
Peter Burns Avatar answered Sep 23 '22 10:09

Peter Burns