r/git • u/kudikarasavasa • 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
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:
SOLVED!