How to Remove a Commit in Git

In Git, a commit is a fundamental feature for saving changes in a local repository. Think of it to identify the change that you have made and make it easy to follow up in the future.

And you must use Git best practices to make sure your commit message is atomic in nature. You should commit only a complete and tested code. A commit message must be precise and valuable for developers. To be honest good commit messages are act as documentation of the project.

When you commit changes with Git commit command, you want to change your last commit message for many good reasons. And sometimes you want to change some ancient commit messages.

Before we dwell on how to remove the commit from Git, you need to understand how it will affect your work. The effect will be positive when you know what you are doing and the benefits of removing a given commit.

You may find removals to be incredibly useful in the following scenarios:

  • You realize that you are on an incorrect track or the wrong branch.
  • You realize that you need to replace the commit message.
  • You want to do away with some local commits before you can push the changes.
  • You have already made commits to your central repository and need to undo them.
  • You have merged and a commit has broken something.

If you can already spot your problem in any of the listed instances, you have solved half the problem and can proceed to a specific removal.

In this guide, I will explain to you how you can remove your Git commit.

Remove recent commit message in Git

If you realize that you only need to change part of the latest commit, It will be effortless with the  –amend option.

Let’s take an example

In the following commit, you can see there is a typo in the commit message.

 

You can use the following command to update it.

git commit –amend ( and then press enter)

And the following window will open for you to change the commit message.

 

Remove commit message from a Branch in Git

If you realize that you are working on the wrong branch and need to restore it without the unsaved changes, you will need to use git reset which does away with the changes. There are two ways to use Git reset. They include:

  • Using git reset HEAD~2 which does not reset the index and the tree
  • Using git reset –hard HEAD~2 which resets your tree and index

Be careful while using the second option because resetting your index means that you lose files in a current branch, the files’ SHA1 checksums, their timestamps, and names.

Therefore, you should only use git reset –hard HEAD~2 for unpublished commits that do not affect your overall project.

The git reset HEAD~2 also removes two recent commits but you retain the file data that may help you restore the file. If you are dealing with a file that

Remove commit before pushing changes

If you want to change various local commits before pushing your changes to a repository, you will need to open up the interactive rebase.

The rebase will make it easier to make any other changes, such as squashing and reordering your commits. You open it by typing the following command:

git rebase –interactive origin dev

The command allows you to specify the branch for your commit and go directly to it in the rebase.

The rebase will bring up a list of commands that you can apply to the commit and you can pick the appropriate one.

You can set it to r = use commit if you want to reword it or use any of the other commands to change the commit.

Remove commits from the Central Repo

Sometimes you realize that the wrong commits were pushed to the central repo. For example, generated files added in the repo.

You can remove them with the following options:

  • git revert ID where ID should be the actual or specific ID of the commit.
  • git revert HEAD^ to remove the previous commit
  • git revert develop~Ni..develop~No, where Ni and No represent the last and starting commits in the range that you want to remove.

On a safe side, you can always avoid unwanted files by ignoring them. And you should use Git ignore file in your repository.

Remove commits from merged commits

When a wrong commit creates a bug or hitch in your work, you can track it and remove it. The process involves using a binary search with the following commands:

git bisect start

The command will start the bisection search. The search will list your commits and you can mark the wrong ones with:

git bisect bad

You can mark the correct ones with:

git bisect good revision

The feature helps to sort the commits until you identify the commit for removal and clean up your work. You will then need to create the right one as before.

Conclusion

In this information age, it’s not hard to learn how to code and manage your code repository. And in this post, we have seen the different ways to remove a commit in git.

You can remove a commit from a branch, revert it from a central repo, remove it due to a bad message, or you can remove the commit message before pushing your changes Also you can retrieve and delete merged commits.

It’s always helpful for the dev team to use valuable commit messages. And in any case, if you need to update or remove previous git messages hope this guide will help you.

About Amit Shaw

Amit Shaw, Administrator of iTechCode.He is a 29 Year Ordinary Simple guy from West Bengal,India. He writes about Blogging, SEO, Internet Marketing, Technology, Gadgets, Programming etc. Connect with him on Facebook, Add him on LinkedIn and Follow him on Twitter.

Speak Your Mind

*