git, change on local branch affects other local branches?

前端 未结 2 1523
花落未央
花落未央 2021-01-31 19:41

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 t

相关标签:
2条回答
  • 2021-01-31 19:51

    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.

    0 讨论(0)
  • 2021-01-31 20:08

    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

    0 讨论(0)
提交回复
热议问题