Is there a single command that will print the current branch (e.g. master) if there is one, or the commit hash if it is detached? The purpose is to give me something to write down so I can later execute git switch <branch_or_commit> to get back to where I started.
This is to be used programmatically, so no scraping of git status, Bash pipes or whatever. The answer needs to use the "plumbing" commands, which have guarantees about the format and stability of their output.
Also I should note that there are many many questions asking for each of these thinks individually, but not together. PLEASE THINK BEFORE CASUALLY MARKING THIS AS A DUPLICATE.
Here are the things I have tried. The closest is actually cat .git/HEAD.
| Command | Output on a branch | Output when detached |
|---|---|---|
git rev-parse HEAD |
cedbe9... |
cedbe9... |
git rev-parse --abbrev-ref HEAD |
master |
HEAD |
git symbolic-ref --short HEAD |
master |
fatal: ref HEAD is not a symbolic ref |
cat .git/HEAD |
ref: refs/heads/master |
cedbe9... |
git branch --show-current |
master |
(no output) |
| your answer here | master |
cedbe9... |
Impossible with exactly one command but possible with two:
git symbolic-ref -q --short HEAD || git rev-parse HEAD
Get the current branch; but if detached get the hash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With