-2

First I tried to undo a pull (don't ask me why), by moving my origin/develop branch to a specific commit:

git reset --hard <older-commit>
# trying to undo the pull (please don't ask why)
git branch -f origin/develop HEAD

Looks like git created a new local branch called origin/develop:

git log -n 3 --decorate remotes/origin/develop

commit 43187d461a5e2b3a4a3443ac7d91d2414f6ccdd7 (origin/develop)
Author: me <me@me.com>
Date:   Tue Oct 13 17:10:29 2015 -0700

    Commit 3

commit 33187d461a5e2b3a4a3443ac7d91d2414f6ccdd6
Author: me <me@me.com>
Date:   Tue Oct 13 17:01:29 2015 -0700

    Commit 2

commit 23187d461a5e2b3a4a3443ac7d91d2414f6ccdd5 (HEAD, develop, origin/develop)
Author: me <me@me.com>
Date:   Tue Oct 13 17:00:29 2015 -0700

    Commit 1

Two questions:

  1. Can you explain why that happened?
  2. How can I fix this without messing things further?
fernacolo
  • 7,012
  • 5
  • 40
  • 61
  • When you use `-f` (force) options on git commands you risk putting the repository into an invalid state, which it seems has happened. Make a clone of 43187d46 into a new directory and try again. – msw Oct 15 '15 at 00:45

1 Answers1

2

Yes, you created a local branch named origin/develop. That's what the git branch command did: you asked it to create-or-replace-existing (-f) local branch origin/develop such that this particular branch points to the same commit as HEAD:

git branch -f origin/develop HEAD

This "works" (for some value of "works") because all local branches actually start with a hidden refs/heads/: you created refs/heads/origin/develop. All remote-tracking branches actually start with a hidden refs/remotes/, then have the name of the remote (origin) followed by the name the other git repository is using (develop) with the usual path-name separator /. So your local origin/develop is actually refs/heads/origin/develop, which is sufficiently different from refs/remotes/origin/develop that git can tell them apart. But when git shortens names for you, the resulting short names are origin/develop and origin/develop.

The fix is to delete (or rename) your local branch so that it's not so confusing:

git branch -d origin/develop

Meanwhile, the thing to know about git pull is that it's actually git fetch followed by git merge.1 You should never need to undo the git fetch part, it's the git merge part that you may want to undo.


1Or git fetch followed by git rebase, if you tell it to do that instead. There are multiple ways to make pull use rebase, which probably should have been the default. You can undo both merges and rebases, but the methods are a bit different.

torek
  • 448,244
  • 59
  • 642
  • 775