问题
I have a master branch
now for some testing other things I made a branch A
I checkout branch A modify the file and when I checkout master again the changes are there as well.
On other repositories I have the right behaviour
回答1:
Uncommitted changes will move from one branch to other. To keep them separated, you must stash
those changes before moving to another branch. When you return to your branch, you can apply
those changes to retrieve them.
As seen below:
>$ git status
On branch branch_1
Your branch is up-to-date with 'origin/branch_1'.
modified: dir/file.rb
>$ git stash
>$ git checkout <branch_2>
>$ git checkout <branch_1> #after finishing your tasks in branch_2 you can go back to branch_1
>$ git stash apply
Now you will get back the changes you have done earlier in branch_1
回答2:
I checkout branch A modify the file and when I checkout master again the changes are there as well.
The changes that are not committed do not belong to any branch. They are present only in the work tree (and in the index if they were added).
It's a good practice to have a clean working tree when switch branches to avoid troubles when the changes in the work tree conflict with the differences between the switched branches.
Because branch A
was just created and you didn't commit anything on it and neither on master
, the branch A
points to the same commit as master
and switching between A
and master
does not require changes in the work tree. This is why you can switch the branches without reaching into conflict.
In order to put the changes you just did in a branch (let's say the checked out branch is A
) you have to add the to the index then commit them:
git add .
git commit
Read more about git add and git commit.
来源:https://stackoverflow.com/questions/32116776/git-change-on-local-branch-affects-other-local-branches