-3

Assume that i've repo like this:

~/Desktop/git_repo
$ ls                                                                          
cover.jpg  cover_small.jpg  README.md

~/Desktop/git_repo on  master
$ git status                                                                     
On branch master
nothing to commit, working directory clean

Now i will add an new branch, and switch current branch to new_branch

~/Desktop/git_repo on  master
$ git branch new_branch                                                          

~/Desktop/git_repo on  master
$ git checkout new_branch                                                        
Switched to branch 'new_branch'

~/Desktop/git_repo on  new_branch
$ git status                                                                     
On branch new_branch

Let's see our files on new_branch:

~/Desktop/git_repo on  new_branch
$ ls                                                                             
cover.jpg  cover_small.jpg  README.md

Nice, it is expected. Now, let's remove cover.jpg on new_branch but NOT commit it:

~/Desktop/git_repo on  new_branch
$ git rm cover.jpg                                                               
rm 'cover.jpg'

~/Desktop/git_repo on  new_branch!
$ ls                                                                             
cover_small.jpg  README.md

~/Desktop/git_repo on  new_branch!
$ git status                                                                     
On branch new_branch
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    cover.jpg

Okay, again it is expected. Now, let's switch branch to our master branch and list files. I expect that cover.jpg is contained:

~/Desktop/git_repo on  new_branch!
$ git checkout master                                                            
D   cover.jpg
Switched to branch 'master'

~/Desktop/git_repo on  master!
$ ls                                                                             
cover_small.jpg  README.md

~/Desktop/git_repo on  master!
$ git status                                                                     
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    cover.jpg

What? Why my master branch is effected from new_branch? What is my mistake? That's my question.

Thanks..

SEGV
  • 848
  • 1
  • 8
  • 19

1 Answers1

3

You misunderstand.

You've only staged the deletion of that file, and since that file exists in both master and new_branch, Git assumes that it's fine to keep the change in its stage.

To make the change permanent to new_branch, you want to check it out again and commit the change.

Once you do that, then the commits between your two branches will be divergent.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Thanks, i think not a good design. Assume that, i make change in _new_branch_ and it is not completed. When i have to switch branch to _master_, i can not **commit** because th changes not completed. Only chance is **stash** before switch branch. But, in this time, all "stash"es are combined for all branches. In large projects it will be so complex, isnt it? – SEGV Apr 01 '18 at 15:14
  • @geTRekt: It really isn't. It just takes time and practice to get used to the nuances of Git. Commiting things should be routine. Stashing them should be when you *must* change branches in a hurry. It's all practice from there. – Makoto Apr 01 '18 at 15:15
  • I see, then the rule is "_commit_ for every little change, don't stack at _stage_".. Thanks, so much. – SEGV Apr 01 '18 at 15:19