Can @{-N} always be used where a branch name is expected?
- From
Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
- Date
- Nov 19, 2017, 18:09 UTC
- Message-ID
- <38b4b665-18c4-5c95-f142-aedf33bc1b5c@gmail.com>
I was recently digging to find if there is any special syntax accepted for <oldbranch> in "git branch -m <oldbranch> <newbranch>" other than the plain branch name. I discovered the @{-N} notation. I was trying to play around with it and found that it didn't work as guaranteed by the last sentence of the following paragraph in the "check-ref-format" documentation,
With the --branch option, it expands the “previous branch syntax”
@{-n}. For example, @{-1} is a way to refer the last branch you were on.
This option should be used by porcelains to accept this syntax
anywhere a branch name is expected, so they can act as if you typed the
branch name.In particular the following case doesn't work,
git init test && cd test && echo "Hello" >file && git add file && git commit -m "Initial commit that will be checked out" && echo "Hello world" >file && git commit file -m "Second commit" && git checkout HEAD^ && git checkout - && git branch -m @{-1} initial-commit
It failed with an error,
error: refname refs/heads/d21e72600673c670b3ae803488d0cebfa949e4c3 not found fatal: Branch rename failed
Then I digged into why it didn't work to discover that @{-N} just expands to a valid checkout and not a valid "branch name". So, the documentation guaranteeing that "@{-N} acts as if you typed the branch name" seems wrong. That makes me think that we should avoid misleading the user in the "check-ref-format" documentation.
So, should we update the 'check-ref-format' doc or am I missing something?
--- Kaartic