Skip to content

Commit 474e4f9

Browse files
committed
Merge branch 'rs/branch-allow-deleting-dangling' into maint
"git branch -D <branch>" used to refuse to remove a broken branch ref that points at a missing commit, which has been corrected. * rs/branch-allow-deleting-dangling: branch: allow deleting dangling branches with --force
2 parents cd9a57f + 597a977 commit 474e4f9

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

Documentation/git-branch.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ OPTIONS
118118
Reset <branchname> to <startpoint>, even if <branchname> exists
119119
already. Without `-f`, 'git branch' refuses to change an existing branch.
120120
In combination with `-d` (or `--delete`), allow deleting the
121-
branch irrespective of its merged status. In combination with
121+
branch irrespective of its merged status, or whether it even
122+
points to a valid commit. In combination with
122123
`-m` (or `--move`), allow renaming the branch even if the new
123124
branch name already exists, the same applies for `-c` (or `--copy`).
124125

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static int check_branch_commit(const char *branchname, const char *refname,
168168
int kinds, int force)
169169
{
170170
struct commit *rev = lookup_commit_reference(the_repository, oid);
171-
if (!rev) {
171+
if (!force && !rev) {
172172
error(_("Couldn't look up commit object for '%s'"), refname);
173173
return -1;
174174
}

t/t3200-branch.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,19 @@ test_expect_success 'attempt to delete a branch merged to its base' '
12721272
test_must_fail git branch -d my10
12731273
'
12741274

1275+
test_expect_success 'branch --delete --force removes dangling branch' '
1276+
git checkout main &&
1277+
test_commit unstable &&
1278+
hash=$(git rev-parse HEAD) &&
1279+
objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") &&
1280+
git branch --no-track dangling &&
1281+
mv $objpath $objpath.x &&
1282+
test_when_finished "mv $objpath.x $objpath" &&
1283+
git branch --delete --force dangling &&
1284+
git for-each-ref refs/heads/dangling >actual &&
1285+
test_must_be_empty actual
1286+
'
1287+
12751288
test_expect_success 'use --edit-description' '
12761289
write_script editor <<-\EOF &&
12771290
echo "New contents" >"$1"

0 commit comments

Comments
 (0)