Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why changes in one branch can affect the other branch in git?

  1. I was at master branch,
  2. I commit everything
  3. then I create a new branch "git checkout -b xxx"
  4. then I switch back to master "git checkout master"
  5. then I delete everything with master branch without using "git rm"
  6. I checkouted to branch xxx, during the branch switching, it shows a long list that every file had a status 'D'
  7. then I found the branch xxx had a empty folder too.

It wasn't a problem since I can revert in branch xxx with "gt reset --hard HEAD"

I just wonder why deleting behavior at one branch can affect the other branch? Doesn't the branch stored everything?

like image 444
Albert Gao Avatar asked Nov 01 '25 20:11

Albert Gao


1 Answers

  1. then I tried to merge one of my branch using "git merge xxx", it said "alread update to date"

This happen because your xxx branch was already merged into master and nothing changed in xxx after that.

  1. I checkouted to branch xxx, during the branch switching, it shows a long list that every file had a status 'D'

You haven't commit changes and switched to a branch xxx, so all your changes moved to this branch

I just wonder why deleting behavior at one branch can affect the other branch? Doesn't the branch stored everything?

Yes it does. But you moving your changes between branches without commiting so you could see it in every "up-to-date" branch. Just commit it somewhere and it will behave as expect

Edit: Deletion of files is the same operation as modifying for git, so you have to commit changes after delete, somewhere between steps 5 and 6

like image 174
Anton Kolyaev Avatar answered Nov 03 '25 11:11

Anton Kolyaev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!