Skip to content

Commit 67affd5

Browse files
author
Junio C Hamano
committed
git-branch -D: make it work even when on a yet-to-be-born branch
This makes "git branch -D other_branch" work even when HEAD points at a yet-to-be-born branch. Earlier, we checked the HEAD ref for the purpose of "subset" check even when the deletion was forced (i.e. not -d but -D). Because of this, you cannot delete a branch even with -D while on a yet-to-be-born branch. With this change, the following sequence that now works: mkdir newdir && cd newdir git init-db git fetch -k $other_repo refs/heads/master:refs/heads/othre # oops, typo git branch other othre git branch -D othre Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 75e6e21 commit 67affd5

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

builtin-branch.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ static int in_merge_bases(const unsigned char *sha1,
3838

3939
static void delete_branches(int argc, const char **argv, int force)
4040
{
41-
struct commit *rev, *head_rev;
41+
struct commit *rev, *head_rev = head_rev;
4242
unsigned char sha1[20];
4343
char *name;
4444
int i;
4545

46-
head_rev = lookup_commit_reference(head_sha1);
46+
if (!force) {
47+
head_rev = lookup_commit_reference(head_sha1);
48+
if (!head_rev)
49+
die("Couldn't look up commit object for HEAD");
50+
}
4751
for (i = 0; i < argc; i++) {
4852
if (!strcmp(head, argv[i]))
4953
die("Cannot delete the branch you are currently on.");
@@ -53,8 +57,8 @@ static void delete_branches(int argc, const char **argv, int force)
5357
die("Branch '%s' not found.", argv[i]);
5458

5559
rev = lookup_commit_reference(sha1);
56-
if (!rev || !head_rev)
57-
die("Couldn't look up commit objects.");
60+
if (!rev)
61+
die("Couldn't look up commit object for '%s'", name);
5862

5963
/* This checks whether the merge bases of branch and
6064
* HEAD contains branch -- which means that the HEAD

0 commit comments

Comments
 (0)