Andy
Andy

Reputation: 11492

can I delete a whole branch from a git repo?

I'm not just talking about the branch pointer :-)

Say I have a already fully merged branch, and now I want to make it look like that branch never existed. It seems to me I should be able to completely prune that branch from the repo (i.e. delete all the commits on that branch so it never shows in the history) without sacrificing the integrity. Is there any way to do that?

Would it be possible to push the remaining branch upstream without pushing the one i'd rather forget?

EDIT: I've added in an example to try and illustrate this better

git checkout master
git commit -m "C1"
git branch b1
git commit -m "C2"
git checkout b1
git commit -m "C3"
git checkout master
git merge -m "C4" b1
git branch -d b1

gives this result

--C1--+----C2----+--C4
       \        /
        +--C3--+

what I want to do is delete C3 but leave C4 exactly as it is, so that the history looks linear. It seems to me the sha-1 of C4 will be the same whether or not C3 is there, so I don't understand why I should need to rebase/cherry pick to do it

Upvotes: 0

Views: 118

Answers (3)

Ananth
Ananth

Reputation: 797

You can do that using the following commands:

git branch -D branchname.
git reflog expire --expire=now --all
git gc --aggressive --prune=now
git repack -a -d -l

More information on each command is available on git documentation:https://git-scm.com/docs

Upvotes: 0

Marina Liu
Marina Liu

Reputation: 38116

Yes, it's possible to delete a branch completely:

git branch -D branchname
git push origin :branchname

-D is force to delete the branch even the branch has not merged.

Upvotes: 2

Alex Lemesios
Alex Lemesios

Reputation: 532

Have you try using :

$git branch -d ?

Upvotes: -1

Related Questions