Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git, change on local branch affects other local branches?

Tags:

git

git-branch

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

like image 359
user2071938 Avatar asked Aug 20 '15 11:08

user2071938


People also ask

What happens if you make a change on a branch you don't own git?

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.

Does git pull update all local branches?

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.


2 Answers

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

like image 117
Vishnu Atrai Avatar answered Oct 14 '22 04:10

Vishnu Atrai


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.

like image 45
axiac Avatar answered Oct 14 '22 04:10

axiac