r/git • • 19h ago

support What is an elegant way to jump back to the tip of the branch where you were at?

Suppose I do the following:

git checkout HEAD~5
git checkout HEAD~10
git checkout HEAD~100

I am now at a detached HEAD and it is not immediately obvious which branch I was at. What is an elegant way to go back to tip of the branch where I was originally at?

I can get a list from the reflog with git reflog --format='%gs' --grep-reflog='checkout: moving from' | awk '{print $4}' but this contains both branch names and commit hashes so I will have to further process them (git show-ref?) to discard the hashes and I'm wondering if there is a more elegant way to do it.

UPDATE: Solved

5 Upvotes

21 comments sorted by

View all comments

Show parent comment

3

u/kudikarasavasa 19h ago edited 18h ago

This is a good heuristic and does the job pretty well, thanks!

The hash length for the commit we move from is consistent. The hash we move to is abbreviated, and can't be expanded with core.abbrev=40 (strange) and is not required for the match anyway.

Putting it all together, this sends me back to the tip of the branch where I came from:

git checkout $(git reflog --format='%gs' --grep-reflog='checkout: moving from' | awk '$4 !~ /^[0-9a-f]{40}$/ {print $4; exit}')

SOLVED!

3

u/xerkus 18h ago edited 18h ago

new git switch would come in handy here as it will refuse to checkout commit. Also quotes to be safe

git switch --no-guess "$(git reflog --format='%gs' --grep-reflog='checkout: moving from' | awk '$4 !~ /^[0-9a-f]{40}$/ {print $4; exit}')"

Adding this as last-branch alias to my setup, thank you.

git config --global alias.last-branch '!git switch --no-guess "$(git reflog --format='\''%gs'\'' --grep-reflog='\''checkout: moving from'\'' | awk '\''$4 !~ /^[0-9a-f]{40}$/ {print $4; exit}'\'')"'

1

u/xerkus 18h ago

I think I would want to know last branch and switch separately:

git config --global alias.last-branch '!git reflog --format="%gs" --grep-reflog="checkout: moving from" | awk "\$4 !~ /^[0-9a-f]{40}$/ {print \$4; exit}"'
git config --global alias.slb '!git switch --no-guess "$(git last-branch)"'

1

u/kudikarasavasa 18h ago

This one verifies the branch name so it won't discard branches named as a 40 character hash. Git does print a warning for refs named like this, so I've suppressed the warning.

git -c core.warnAmbiguousRefs=false checkout "$(
    git reflog --format='%gs' --grep-reflog='checkout: moving from' |
    awk '{print $4}' |
    while read -r branch; do
        git show-ref --verify --quiet "refs/heads/$branch" &&
            { echo "$branch"; break; }
    done
)"

Not as elegant as I hoped it would be, but at least it's more correct.

1

u/xerkus 18h ago

If last branch was deleted this will select the next one in the list.

I opted for switch --no-guess to avoid it checking out remote branch if no matching local branch exist, avoid checking out commit via shortened hash if no local branch and to avoid checking out tags. Switch will fail in all those cases.

Your validation achieves all of that as well.

-1

u/codeguru42 14h ago

I thought you asked for an elegant solution...

1

u/kudikarasavasa 8h ago edited 36m ago

I do, but apparently there isn't one, so I had to settle for something that works.