2

Simple git functionality question. I create a new branch, checkout that branch, and make changes to newbranch. Prior to the first commit on newbranch, I'm seeing that those changes are shown when doing git status from master as well as newbranch. If I stage those changes while on newbranch, they show up staged on master. It is not until I commit these changes to newbranch that master appears to be separated. A) am I crazy? B) What is the logic behind this?

There is a similar question here with no answer.

user2611761
  • 169
  • 1
  • 1
  • 11
  • I imagine there are quite a few answers addressing this, [here's mine](https://stackoverflow.com/a/40324524/1290731). – jthill Oct 16 '17 at 21:06
  • I've closed this as a duplicate, but I'll add here that for this particular case, you have two branch names *pointing to the same commit*, so `git checkout` of either name makes *no* changes to the index or work-tree, by definition. It therefore always works, and always simply updates `HEAD` to contain the new target branch name (and does nothing else). – torek Oct 16 '17 at 21:30

1 Answers1

2

As along as you did not stage changes, they exist only in the work space and you can switch branches as many times as you want. If you stage them, then they are 'scheduled' for the next commit, however you still may change the branch before commit.

This behavior is correct. In some cases you may start work on one branch, but would like to commit to a different one. This is typical, when you need to send small changes for code review, for example or for an quick bug fix.

kofemann
  • 4,217
  • 1
  • 34
  • 39