Though most of us know we should never commit any sensitive information to a source code repository (be it Git, Subversion, or whatever), there are times in which it happens.

Most of the time, I imagine it happens whenever we’re working on code and then hopping back and forth between the IDE and a terminal and committing code to make sure we’re not losing any changes.

This happens long enough, and then we end up committing a consumer key and consumer secret or a username and password or something similar to the repository.

Luckily, we can remove commits to revert our code, but most source control systems end up keeping a history of everything (which is a good thing). But what if we need to go about removing Git commit history in both our local and remote repositories?

Removing Git Commit History

For this example, I’m assuming you’re working with Git, and you’re working with the command-line (though I’d imagine most GUIs would allow for the ability to do this).

Next, I’m assuming that the commit you want to remove from your history is the most recent commit and you just want to go back one step.

To go about completely removing git commit history, there are only two things you need to do:

  1. rebase to the previous commit,
  2. push the branch to the remote.

To do this, check the middle two steps here:

The additional steps before and after the main commits allow you to get a visual list of commits in case you want to choose a separate commit to which you want to rollback.

Removing Git Commit History

And you can run the command both before and after the other two commands (as seen in the gist above) to make sure the list of commits appears how you want in the history of the project.

Category:
Articles
Tags:

Join the conversation! 2 Comments

  1. You may need to add –force, or the short flag -f, if you rebase local commits and try to push to a remote repo that has previous history.

  2. I know this is about doing it by hand, but there’s an easier way with the git-extras commands installed, there’s a command under that repo called git obliterate.

    https://github.com/tj/git-extras/blob/master/Commands.md#git-obliterate

    You pass it a file name and it’ll disappear entirely from the repo history, it has helped me a lot when the new guys push something they should not have.

    https://github.com/tj/git-extras

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.