How to Remove Commit From a Branch in Git
- Remove Changes if Not Pushed to Repository in Git
- Remove Changes if Pushed to Repository in Git
- Remove Deleted Commit
When we start working on a massive project, we often encounter situations where we have to update, remove, or add parts in a branch. The maintenance of commit messages might be a hurdle for the team if the development duration is stretched across a larger period with a strict deadline.
So for making the maintenance of concise commit history simpler, we will learn the usage of some common conditions in this article that we might go through while working with Git.
Git provides us with a few commands to delete our previous commits from the branch. The available strategies and commands are discussed below in this article.
There are a few ways in Git to delete or remove a commit from the branch. The first step is to sort out which trick is best for our scenario, depending upon if we have or haven’t pushed our commit into the repository.
Before attempting this, we should note that executing these commands will delete our working directory changes. And also, note that to save the changes separately that we would like to have in the repository in the future, git reset
is a command that resets our working directory to the specified commit.
Remove Changes if Not Pushed to Repository in Git
In the scenario when we have not pushed our changes to the repository, the following command will be used.
git reset --hard HEAD~1
This will get rid of all working directory changes and move the HEAD to the commit before HEAD.
Suppose we have to delete the commits in an upward direction until a particular commit. In that case, we will execute the git log
command into the command line to find the particular commit id and will then run the following:
git reset --hard <sha1-commit-id>
This will omit all working directory changes and will move HEAD to the commit that is chosen.
Remove Changes if Pushed to Repository in Git
In the other scenario, when we have already pushed our changes, we need to execute the following command.
git push origin HEAD --force
Note that if others have pulled the respective branch, starting with the latest branch would be better. If we don’t perform this when someone else has pulled, it will combine it into their work, and we will get it pushed back up once more.
If we encounter a situation where we need to find a commit that we deleted, it is present in git reflog
unless we have garbage collected in our repository.
Remove Deleted Commit
To remove a deleted commit from the branch, we can use the following command:.
git reset --soft HEAD^
This command will revert or reset all the changes from the previous commit and take it back into a new commit in the repository.
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn