Modified files in git branches flow to another branch

I am working on a git repository with a main branch and another theme branch. I switched to the topic thread and changed the file. Now, if I switched to the main branch, the same file will be shown as modified.

For example:

git status in the git -build branch:

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

Switch to Lead Branch

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

git status in the main 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 the file displayed as changed in the main branch, although it was changed in the git -build branch?

I realized that the branches are independent of each other, and when I move from one branch to another, the changes do not "flow" from one branch to another. So I obviously missed something here.

Anyone got a clue?

+42
git git-branch
Oct 29 '08 at 9:58
source share
5 answers

This is the default behavior of git.

You can use the -f flag to check to do a "clean check" if you want.

+13
Oct 29 '08 at 10:01
source share

Why is the file displayed as changed in the main branch, although it was changed in the git -build branch?

The key to remembering is that the file has not been modified in the git -build branches. It was changed only in your working copy.

Only when you commit changes being returned to any branch did you check

+59
Oct 29 '08 at 10:06
source share

If you want to temporarily save your changes in one branch while you go to another work, you can use the git stash command. This is one of the amazing little unsung when using git. Workflow example:

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

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

+38
Oct 29 '08 at 10:26
source share

Modified files are not put into the repository until you add and commit them. If you return to the branch of your section and commit the file, it will not appear on the main branch.

+9
Oct 29 '08 at 10:00
source share
  • This is not like the w20 branches depend on each other, but also they do not have a complete code base for each branch separately.
  • For each commit, git stores an object containing a pointer to the changes. Thus, each branch points to its last commit, and HEAD points to the branch you are in.
  • When you switch a branch, the HEAD pointer points to that particular commit of the branch. Therefore, if there are modified files, the default behavior is to copy them.

You can do the following to overcome this problem.

  • Use the -f option to ignore changes.

If you want to save the changes:

  1. Commit the changes locally in one branch, and then switch the branch.
  2. Use git stash , switch the branch, do your job, go back to the original branch and git stash apply .
+1
Jun 11 '15 at 8:13
source share



All Articles