What git branch was I just in?

My workflow often consists of the following:

  • working on a long term feature branch (call it ‘A’)
  • checking out master and making a hotfix branch (call it ‘B’)
  • pushing my hotfix
  • Then I forget what branch I was working on before that…

Is there any sort of git history that will show me that the thing I was previously working on was branch A?

  • Has no default translation and R is not being generated
  • Github - no write access to team's repo in organisation
  • Changing Branch in Git
  • git clone from debian virtualbox
  • What are some more forceful ways than a .gitignore to keep (force) files out of a repo?
  • Git Cherry-pick vs Merge Workflow
  • How to reverse apply a stash that with conflict?
  • Enforce --recurse-submodules=on-demand for all clones of a repo
  • How do you restore a corrupted object in a git repository (for newbies)?
  • Rebase on github branch
  • How to checkout jetty util jetty-util-8.1.17.v20150415 from git
  • How can one safely use a shared object database in git?
  • 4 Solutions collect form web for “What git branch was I just in?”

    Try:

    git reflog
    

    which will list all of your recent actions that changed something, so checkout, rebase, pull, push, etc.

    Also it includes all the commit ids, so for example if you did a dozen commit --amend you can jump back to one in the middle.

    As documented in gitrevisions, the syntax @{-number} refers to the branch checked out number occurrences ago. This uses the reflogs, specifically the reflog for HEAD; see Duncan’s answer.

    (In your particular example you’d want @{-1}. For git checkout you can abbreviate this as git checkout -, but that only works for git checkout.)

    Try:

    git branch --sort=-committerdate
    

    or

    git for-each-ref --sort=-committerdate refs/heads/
    
    git checkout -
    

    go to the the previously used branch

    I’ve also set this alias that prints the last 10 branches I’ve worked on :

    [alias]
        recent = for-each-ref --count=10 --sort=-committerdate refs/heads/ --format='%(refname:short) %(committerdate:relative)'
    
    Git Baby is a git and github fan, let's start git clone.