Regarding Greg Hewgill's 2012 answer, Git 1.7.9 (Q1 2012) also included some checks for git branch --edit-description
:
See commit c2d17ba (05 Feb 2012) by Junio C Hamano (gitster
).
(Merged by Junio C Hamano -- gitster
-- in commit d88698e, 10 Feb 2012)
It is very easy to mistype the branch name when editing its description, e.g.
$ git checkout -b my-topic master
: work work work
: now we are at a good point to switch working something else
$ git checkout master
: ah, let's write it down before we forget what we were doing
$ git branch --edit-description my-tpoic
The command does not notice that branch 'my-tpoic' does not exist.
It is not lost (it becomes description of an unborn my-tpoic branch), but is not very useful.
So detect such a case and error out to reduce the grief factor from this common mistake.
This incidentally also errors out --edit-description
when the HEAD points at an unborn branch (immediately after "init
", or "checkout --orphan
"), because at that point, you do not even have any commit that is part of your history and there is no point in describing how this particular branch is different from the branch it forked off of, which is the useful bit of information the branch description is designed to capture.
We may want to special case the unborn case later, but that is outside the scope of this patch to prevent more common mistakes before 1.7.9 series gains too much widespread use.
While Greg Hewgill's 2012 answer is accurate, using git branch --edit-description
can lead to:
fatal: could not unset 'branch.main.description'
"GIT_EDITOR=: git branch --edit-description
(man) resulted in failure, which has been corrected with Git 2.39 (Q4 2022).
See commit e288b3d (30 Sep 2022) by Junio C Hamano (gitster
).
(Merged by Junio C Hamano -- gitster
-- in commit 272be0d, 17 Oct 2022)
branch
: do not fail a no-op --edit-desc
Imagine running "git branch --edit-description
"(man) while on a branch without the branch description, and then exit the editor after emptying the edit buffer, which is the way to tell the command that you changed your mind and you do not want the description after all.
The command should just happily oblige, adding no branch description for the current branch, and exit successfully.
But it fails to do so:
$ git init -b main
$ git commit --allow-empty -m commit
$ GIT_EDITOR=: git branch --edit-description
fatal: could not unset 'branch.main.description'
The end result is OK in that the configuration variable does not exist in the resulting repository, but we should do better.
If we know we didn't have a description, and if we are asked not to have a description by the editor, we can just return doing nothing.
This of course introduces TOCTOU.
(Time-of-check to time-of-use (TOCTOU): a class of software bugs caused by a race condition involving the checking of the state of a part of a system (such as a security credential) and the use of the results of that check.)
If you add a branch description to the same branch from another window, while you had the editor open to edit the description, and then exit the editor without writing anything there, we'd end up not removing the description you added in the other window.
But you are fooling yourself in your own repository at that point, and if it hurts, you'd be better off not doing so ;-).
Also: "git branch --edit-description
"(man) on an unborn branch misleadingly said that no such branch exists, which has been corrected with Git 2.39 (Q4 2022).
See commit bcfc82b (08 Oct 2022) by Rubén Justo (rjusto
).
(Merged by Junio C Hamano -- gitster
-- in commit 4050354, 17 Oct 2022)
branch
: description for non-existent branch errors
Signed-off-by: Rubén Justo
When the repository does not yet have commits, some errors describe that there is no branch:
$ git init -b first
$ git branch --edit-description first
error: No branch named 'first'.
$ git branch --set-upstream-to=upstream
fatal: branch 'first' does not exist
$ git branch -c second
error: refname refs/heads/first not found
fatal: Branch copy failed
That "first" branch is unborn but to say it doesn't exists is confusing.
Options "-c" (copy) and "-m" (rename) show the same error when the origin branch doesn't exists:
$ git branch -c non-existent-branch second
error: refname refs/heads/non-existent-branch not found
fatal: Branch copy failed
$ git branch -m non-existent-branch second
error: refname refs/heads/non-existent-branch not found
fatal: Branch rename failed
Note that "--edit-description
" without an explicit argument is already considering the empty repository circumstance in its error.
Also note that "-m
" on the initial branch it is an allowed operation.
Make the error descriptions for those branch operations with unborn or non-existent branches, more informative.
This is the result of the change:
$ git init -b first
$ git branch --edit-description first
error: No commit on branch 'first' yet.
$ git branch --set-upstream-to=upstream
fatal: No commit on branch 'first' yet.
$ git branch -c second
fatal: No commit on branch 'first' yet.
$ git branch [-c/-m] non-existent-branch second
fatal: No branch named 'non-existent-branch'.
With Git 2.39 (Q4 2022), "git branch --edit-description @{-1}
"(man) is now a way to edit branch description of the branch you were on before switching to the current branch.
See commit 0dc4e5c (11 Oct 2022) by Rubén Justo (rjusto
).
(Merged by Junio C Hamano -- gitster
-- in commit c2058ea, 21 Oct 2022)
branch
: support for shortcuts like @{-1}
, completed
Signed-off-by: Rubén Justo
branch command with options "edit-description", "set-upstream-to" and "unset-upstream" expects a branch name.
Since ae5a6c3 (checkout
: implement "@{-N}
" shortcut name for N-th last branch, 2009-01-17, Git v1.6.2-rc0 -- merge), a branch can be specified using shortcuts like @{-1}
.
Those shortcuts need to be resolved when considering the arguments.
We can modify the description of the previously checked out branch with:
$ git branch --edit--description @{-1}
We can modify the upstream of the previously checked out branch with:
$ git branch --set-upstream-to upstream @{-1}
$ git branch --unset-upstream @{-1}
With Git 2.41 (Q2 2023), error messages given when working on an unborn branch that is checked out in another worktree have been improved.
See commit 3521c63, commit a675ad1, commit 7a6ccdf, commit d7f4ca6, commit 2e8af49 (27 Mar 2023) by Rubén Justo (rjusto
).
(Merged by Junio C Hamano -- gitster
-- in commit d3f2e4a, 15 May 2023)
branch
: description for orphan branch errors
Signed-off-by: Rubén Justo
In bcfc82b ("branch
: description for non-existent branch errors", 2022-10-08, Git v2.39.0-rc0 -- merge listed in batch #3) we checked the HEAD in the current worktree to detect if the branch to operate with is an orphan branch, so as to avoid the confusing error: "No branch named...".
If we are asked to operate with an orphan branch in a different working tree than the current one, we need to check the HEAD in that different working tree.
Let's extend the check we did in bcfc82b, to check the HEADs in all worktrees linked to the current repository, using the helper introduced in 31ad6b6 ("branch
: add branch_checked_out()
helper", 2022-06-15, Git v2.38.0-rc0 -- merge listed in batch #1).