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
When you change a file in your work-tree, nothing happens to the copy in the index. It still matches the copy in the commit you chose. You have to run git add to copy the file from the work-tree, to the index.
git fetch. On its own, git fetch updates all the remote tracking branches in local repository. No changes are actually reflected on any of the local working branches.
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With