8

How would you clean up unused side-branches in your commit trees (not real git branches)?

Example (tree, fake-commit-hash, commit message, optional [pointers]):

*    0001 last commit [master] [origin/master] [HEAD]
| *  0002 old, unused merge
|/|
* |  0003 some remote commits
* |  0004 another commit from remote
| *  0005 old, unused commits
|/
*    0006 old tree

The path 0001, 0003, 0004, 0006 should stay untouched, but the commits 0002 and 0005 are not useful and aren't doing any good. How do you delete the commits 0002 and 0005?

erikbstack
  • 12,878
  • 21
  • 81
  • 115
  • `git gc` might help: http://www.kernel.org/pub/software/scm/git/docs/git-gc.html – Felix Kling Aug 01 '12 at 09:44
  • possible duplicate of [Listing and deleting Git commits that are under no branch (dangling?)](http://stackoverflow.com/questions/3765234/listing-and-deleting-git-commits-that-are-under-no-branch-dangling) – Felix Kling Aug 01 '12 at 09:52
  • Not 100%. The headline suggests so, but the question text actually talks more about how to understand and output the state of such commits. Inside is a lot of good advice that can be used to write an answer to this question here, though. – erikbstack Aug 01 '12 at 10:23
  • Did you really look at the accepted answer (and the other answers)? *How can I delete commits like those?: Once your reflog entries are expired, those objects will then also be cleaned up by `git gc`.* And there is more information about the expiration time or even about how to mark all dangling commits as expired... what more information do you need? – Felix Kling Aug 01 '12 at 10:27
  • Yes. As I said the info is probably there and just needs to be understood and formulated to an answer for this question here. I done this now but feel uncomfortable that I couldn't give reputation-points to other people (like you!?) this way. – erikbstack Aug 01 '12 at 12:20

1 Answers1

12

tarsius wrote in an answer to another question:

git reflog expire --expire=now --all
git gc --prune=now

which clears the reflog and then cleans up the repository. Cleaning the reflog at first doesn't always work, because meaningles commits marked by the reflog are kept alive by git-gc as long as the reflog doesn't expire (which is 90 days by default).

After doing this all dangling commits are really gone, as far as I understood. So one should be sure that one really doesn't need all of them anymore. If one really wants to keep some of the dangling commits, one can:

git checkout <dangling_commit_id>
git branch <new_branch_name_of_your_choice>

or use git format-patch to store the whole commit in a text file.

Community
  • 1
  • 1
erikbstack
  • 12,878
  • 21
  • 81
  • 115