Albert Gao
Albert Gao

Reputation: 3773

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?

Upvotes: 4

Views: 3614

Answers (2)

Anton Kolyaev
Anton Kolyaev

Reputation: 316

  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

Upvotes: 3

Jin
Jin

Reputation: 63

For better understanding of git branches if you use Windows try use application "Git Extensions". This app shows all branches in your repo and it is do understanding simpler.

Upvotes: 1

Related Questions