This tutorial teaches how to reset a commit that hasn’t been pushed to the remote repository in Git, a version control system that we use to keep track of the changes done to files in our project directory.
In Git, commits are used to track the changes done to files. Sometimes, we may commit the changes of the files locally and later may feel that those changes are no longer relevant.
We now want to reset this commit instead of pushing this commit to the remote repository. We can accomplish this task of resetting a commit that has not been pushed to remote yet by using the
git reset command.
Undo a Commit Before Pushing to Remote Repository in Git
We use Git in a collaborative development environment to keep track of the changes done to files. Git accomplishes this task of tracking the changes to the files using commits.
When we are satisfied with the changes done to files, we commit the changes to the Git repository of our project. In a basic Git workflow, once we have tested and completed our changes, we add the changes to the staging area. We use the
git add command for that.
After adding the changes to the staging area, we now proceed to commit the change to our Git repository. We use the command
git commit to commit the changes.
Each commit is used to record a snapshot of the repository’s state and the name, timestamp, and message. When we commit changes using the
git commit command, those changes are committed to the local Git repository.
This local repository is associated with a remote Git repository. We then proceed to push the commits of the local Git repository to the remote repository. We use the
git push command to achieve that.
Sometimes, before pushing the commit to the remote repository, we may feel that the current commit in the local repository is no longer required, and we wish to discard it.
Thus, to discard the commit from the local repository, we can use the
git reset command. Suppose we have a commit history as follows in our Git repository.
$ git log --oneline 453dcfc (HEAD -> master) minor change bea3aac (origin/master, origin/HEAD) some change b14f387 Other change ...
In commit history, the commit
453dcfc is in the local Git repository but not in the remote repository yet. The previous commits are in the remote repository as well.
Thus, to discard a commit present in the local repository and not yet pushed to the remote one, we can execute the
git reset command as follows.
$ git reset --soft HEAD~1
git reset command above resets the current
HEAD by one commit. The
git reset command with the
--soft option does not touch the staging area and the working tree.
It keeps all our changed files as “Changes to be committed”, as
git status would put it, as follows.
$ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: README.md
We can now recheck the commit history and observe that commit
453dcfc is no longer present.
$ git log --oneline bea3aac (origin/master, origin/HEAD) some change b14f387 Other change ...
We’ve shown how to reset a commit that hasn’t been pushed to the remote repository in Git.
For more information, please visit these links.