Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: remove dead branch after a rebase?

Tags:

git

rebase

I have the following git branches: master and dev:

   *master*
      |
A--B--C
   \--D--E
         |
       *dev*

After rebasing dev on master, I have:

   *master*
      |
A--B--C--D'--E'
   \--D--E   |
             |
           *dev*

So D and E remain in a 'dead branch'. How can I remove them?

like image 935
Laurent D. Avatar asked Dec 20 '14 08:12

Laurent D.


People also ask

How do I reset a branch after rebase?

So for example, if you rebase featureA branch onto your master branch, but you don't like the result of the rebase, then you can simply do git reset --hard featureA@{1} to reset the branch back to exactly where it was before you did the rebase.

What should I do after rebasing?

To push the changes to the branch after a rebase, you need to force push your commits using the -f or --force flag in the git push command on Git. This is because something has changed in the remote branch and the commit history is different for both the remote and your local branch.

Why do I have to pull after a rebase?

is because your rebased feature branch now has 25 new commits that aren't reachable from origin/feature (since they came from the rebase on master ) plus 2 commits that are reachable from origin/feature but have different commit IDs.


1 Answers

You don't need to: because commits D and E have no1 name pointing to them, they're eligible for "garbage collection". Eventually git will run a git gc and throw them away for you.

If you want to speed this up, you can run git gc yourself, but then footnote 1 comes into play. :-)


1This is not quite true. While the branch-name dev now contains the ID of commit E' (the copy), there are two reflog entries, one for dev and one for HEAD, that allow you (and git) to find commit E. There is also a semi-special name, ORIG_HEAD, which lasts until something else replaces its contents (another rebase, or a git merge, for instance).

By default, most reflog entries persist for either 30 days or 90 days, depending on whether the commit is reachable from the current branch head. Once the reflog entry expires, then the object is a candidate for garbage collection.

As yet another precaution, git gc leaves "loose objects" alone unless they are at least two weeks old (by default—this is configurable, and there is a --prune= option to override this as well). So in general, for a nominally-abandoned object to go away, it:

  • must be at least two weeks old;
  • must have had any reflog entries expire (generally 30 days); and
  • must not have any alternatives names keeping it available.

(And of course, git gc must run, although git automatically runs this whenever it "looks promising".)

like image 78
torek Avatar answered Oct 13 '22 08:10

torek