djechlin
djechlin

Reputation: 60748

git checkout -b, commit some files, checkout back to original branch complains about overridden changes

This surprises me and I can't think of how it can be possible.

  1. git checkout -b newbranch.
  2. commit some files other than Foo.java.
  3. git checkout master (which was the original branch I was on) gives error:

    error: Your local changes to the following files would be overwritten by checkout:
    Foo.java Please, commit your changes or stash them before you can switch branches.

But I was already on master. So I do the usual workflow:

  1. git stash
  2. git checkout master

And it auto-merges successfully. Why did it need to auto-merge at all? Why did it need to block my git checkout going away from master but not back to master? I'm trying to understand the logic for when git makes these complaints.

Upvotes: 1

Views: 133

Answers (1)

aragaer
aragaer

Reputation: 17848

When you used git checkout -b it didn't actually go away, it stayed on the same commit. That's why it allowed you to checkout keeping all the local changes. Going "back" to master changes the commit (and actually modifies the working tree), that's why it would override your changes.

Upvotes: 3

Related Questions