How to eliminate junk commits in a Git repository?

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:

  • Contributing to an existing pull request
  • git “error: corrupt loose object” after moving a directory
  • How to update git repos automatically?
  • How can I deploy direct from Google Cloud Source Repository to Google App Engine?
  • git-svn with git extenstions stuck on Authentication realm
  • How do I find out which version I've got checked out in git?
  •     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

  • Git Partial Fork, synced pull/merge
  • Deploy website using Git/Gitolite
  • Is it possible to have a tracked .gitignore AND an untracked .gitignore?
  • Re-enable mirroring with git-svn
  • Git branch reset
  • Build project imported from Eclipse git as Android Project
  • 3 Solutions collect form web for “How to eliminate junk commits in a Git repository?”

    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.

    Before merging to master reset all your commits (since most of them are useless anyway).

    git reset $(git merge-base --fork-point master)

    Then, add your changes to the index in a coherent way. Meaning, that changes should be added so that each change can be described in one line. This way you do not need numerous inline comments, but rather rely in the commit comment.

    Changes can be added on a line-by-line basis if desired using

    git add -p

    When finished, rebase onto master (if master moved), and then merge, (I prefer the non-fast-forward merge to show how master actually changed.

    git rebase master
    git checkout master
    git merge --no-ff @{-1}

    The repo's current situation (full of garbage commits)

    You can squash all those commits into one of the commits that you need.

    git rebase -i <your first garbage commit id>

    During a rebase operation
    Complete the rebase , solve if there are merge issues.

    Git Baby is a git and github fan, let's start git clone.