0

I have a repo in a very small environment with a short amount of memory, so every byte counts. I just finished developing, so I want to remove the dev branch. Now, my repo is structured like this:

(master) A---------E
(dev)     \-B-C-D-/

I've already tried git branch -d dev, but it ends up deleting dev keeping all the commits, like this:

(master) A---------E
          \-B-C-D-/

Is there a way to keep only the master branch? Like

(master) A---------E

Thanks.

Ravazz
  • 93
  • 6
  • Only with rebasing if you have already performed the merge (rewriting history can be problematic if history is shared). For future merges, use a squash merge (but this comes with its own problem). Are you sure the few extra bytes for the commit headers are a problem? – knittl Jan 28 '22 at 10:17
  • Assuming dev points to commit D, `git branch -D dev`. Ahh, you mean remove the fact that E is a merge commit? Yeah, you'll need to rebase. – evolutionxbox Jan 28 '22 at 10:17
  • I'll check for the squash merge, thanks. The extra bytes per se, aren't a real problem, but for many other reasons, I have to use the least amount of disk space... the ```-D``` doesn't work either – Ravazz Jan 28 '22 at 10:23

1 Answers1

1

A "branch" is merely a label for a commit. So deleting a branch just frees up some bytes. The commit E still points to D so this commit will not be deleted by the git garbage collection. Same for C because it is pointed to by D and so on.

The suggested method of rebasing/squash merging will get rid of these commits. So you will loose B-C-D and E and they will all be replaced with a new commit F.

If you are really short on disk space and don't care about the history (maybe because you have a clone somewhere else), you could make your repo shallow This will get rid of all the commits except E. There a quite a few ways to achieve this, covered in this question: Converting git repository to shallow?

SebDieBln
  • 3,303
  • 1
  • 7
  • 21