Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to eliminate junk commits in a Git repository?

Tags:

git

When I am investigating on a bug, I am sometime not working carefully especially when I am digging down on several branches with many commits.

Hopefully once I am done with it, I merge the working result to my master branch with a git merge yeah_i_got_it. Here is what my repository might look like in such situations:

    v There is a bug here
A---B---C----------------------------------------------F (master)
     \                                                /
      o---o---o--o--o (bug)                          /
               \                                    / 
                o---o--o--o (oups_this_way_better) /
                     \        v Clean a bit       /
                      o---D---E------------------o (yeah_i_got_it)    
                          ^        
                          Solved the bug here

Eventually I expect to keep only some relevant commits at the end of the day:

A--B--C--D--F (master)

Regarding the rest, is it just garbage and I don't need it. So, what is the best way to deal with this unpleasant workflow?

Because Git keeps everything even after all garbage branches were removed, a useful option to Git would be to give an importance flag with each commit in order to help me cleaning my repository after the rush. In my repositores I can identify useless commits, commits that may be useful one day, commits that will definitely be usefull and, last but not least, the very important commits that I need to keep along with their branches.

Getting rid of the useless commits usually takes me a lot of time. I will need to review my log back, identify the ones I want to eliminate and do the job with no mistake.

So my question is:

How to properly eliminate temporary commits and branches from my Git repositories definitively?

Here some random thoughts:

  • My workflow is just bad, I should never work like that.
  • Just add a prefix to each temporary commits comments like "junk" or "garbage".
  • A native Git solution may exist that can help me to do the job like git rebase-and-clean yeah_i_got_it --keep D
like image 888
nowox Avatar asked Jul 13 '15 18:07

nowox


People also ask

How do I clean up old commits?

Steps to get to a clean commit history:understand rebase and replace pulling remote changes with rebase to remove merge commits on your working branch. use fast-forward or squash merging option when adding your changes to the target branch. use atomic commits — learn how to amend, squash or restructure your commits.

Can we delete commits in git?

Deleting the commit in Git must be approached in one of two ways, depending on if you have or have not pushed your changes. Please note before attempting this, running these commands will DELETE your working directory changes. Any changes to tracked files in the working tree since <commit> are discarded.


1 Answers

The way to do what you're asking is to checkout master at C, then do a git merge --squash yeah_i_got_it. This will collapse the whole branch into one commit.

Another way would be to rebase-interactive the commits, with a git rebase -i master from your yeah_i_got_it branch. In the interactive rebase you could mark E as a "fixup". If its commit message began with "fixup!" and you had the appropriate git config options set, then the marking would be done automatically.

Note: you say you want A-B-C-D-F, but I think you actually just want A-B-C-D. There's no need for the "F" commit.

like image 52
Borealid Avatar answered Nov 01 '22 05:11

Borealid