1

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.

4
  • 1
    Did you modify Foo.java while on the other branch? Commented May 27, 2013 at 14:50
  • @larsmans I believe I did not. Commented May 27, 2013 at 14:51
  • I also sometimes believe I didn't edit a file, but some command I issued did. Maybe your IDE added a newline at the end or something. Do a git status and git diff Foo.java to be sure. Commented May 27, 2013 at 14:53
  • I've tested on a new repository, and it's working great. Commented May 27, 2013 at 14:58

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.