Michael Arrison
Michael Arrison

Reputation: 1553

What git branch was I just in?

My workflow often consists of the following:

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

Upvotes: 4

Views: 112

Answers (4)

Florian F
Florian F

Reputation: 8875

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)'

Upvotes: 0

torek
torek

Reputation: 489083

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.)

Upvotes: 2

ggradnig
ggradnig

Reputation: 14189

Try:

git branch --sort=-committerdate

or

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

Upvotes: 0

Duncan
Duncan

Reputation: 95712

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.

Upvotes: 7

Related Questions