2

How can I know which branch I was previously on? More precisely, which branch git checkout - will go to?

I would also like to know what more previous branches were, in case the previous one is not the one I want to checkout.

In other words, how to see the 'branch' history? Not the current branch commit history, but the history of branches I checked out.

All I find in my searches are about commit history.

RSinohara
  • 650
  • 1
  • 4
  • 25
  • Related [question](https://stackoverflow.com/questions/7206801/is-there-any-way-to-git-checkout-previous-branch) that explains about `git checkout -`: – RSinohara Mar 28 '19 at 11:11
  • 3
    I would think the reflog has that info. It shows stuff like `HEAD@{4}: checkout: blah` – evolutionxbox Mar 28 '19 at 11:11
  • It could be, my understanding is that it would go up the current branch commit history, but I realise there is no basis for my assumption – RSinohara Mar 28 '19 at 11:12
  • Absolutely right @evolutionxbox. You can make it an answer, that is totally what I needed. Bonus points for fixing my wrong assumptions about reflog. – RSinohara Mar 28 '19 at 11:12

2 Answers2

4

The preferred method is to use the simple syntax @{-1} (thanks @phd).

git show --decorate -s @{-1}

Original answer:

The reflog does seem to keep a history of where HEAD is moved, and looks to be accessible by the HEAD@{#} variable.

This however does also include resets, rebases, cherry-picks, and commits. I assume that this is because all of those commands move the HEAD.

104f63b (HEAD -> master, master.bak) HEAD@{0}: checkout: moving from b2 to master
e97431a (b2) HEAD@{1}: checkout: moving from b1 to b2
588aa76 (b1) HEAD@{2}: cherry-pick: C6
befdf09 HEAD@{3}: reset: moving to befdf09b
80cf3dd HEAD@{4}: checkout: moving from master to b1
104f63b (HEAD -> master, master.bak) HEAD@{5}: checkout: moving from b2 to master
e97431a (b2) HEAD@{6}: cherry-pick: C7
befdf09 HEAD@{7}: reset: moving to befdf09b
4dd1828 HEAD@{8}: checkout: moving from master to b2
104f63b (HEAD -> master, master.bak) HEAD@{9}: commit: C5
be1bfdb HEAD@{10}: commit: C4
07743f7 HEAD@{11}: commit: C3
befdf09 HEAD@{12}: checkout: moving from b1 to master
80cf3dd HEAD@{13}: commit: C6
4dd1828 HEAD@{14}: checkout: moving from b2 to b1
4dd1828 HEAD@{15}: commit: C7
befdf09 HEAD@{16}: checkout: moving from master to b2
befdf09 HEAD@{17}: commit: C2
464acc2 HEAD@{18}: commit (initial): C1

If checking the reflog is too much there is a program to help with that.

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
  • 1
    Very simple syntax [`@{-1}`](https://git-scm.com/docs/gitrevisions#Documentation/gitrevisions.txt-em-ltngtemegem-1em): `git show --decorate -s @{-1}` – phd Mar 28 '19 at 13:46
  • 1
    `git rev-parse --abbrev-ref @{-1}` is another variant. More info at [man git-rev-parse](https://gitirc.eu/git-rev-parse.html). – jsageryd Mar 28 '19 at 19:52
-2

just type history. It will give you all the previous commands you typed in your shell.