How to Remove Unpushed Commits in Git
This tutorial will teach how to remove unpushed commits in Git.
Git is used in a collaborative development environment to keep track of the changes done to files in the project directory. Git uses commits to keep track of both the local and remote Git repository changes.
One may want to remove the unpushed commits from the local Git repository from time to time.
Remove the unpushed commits using the git reset
command. We will illustrate this with an example.
Use the git reset
Command to Remove Unpushed Commits in Git
Whenever we want to commit changes to the project directory, we can commit the changes using the git add
and git commit
commands.
When using the git commit
command, a commit is created in the local Git repository. We can then use the git push
command to push the commits in the local Git repository to the remote Git repository.
Sometimes, we may realize that we don’t want to push the commits to the remote repository and have just committed to the local repository. In such cases, we can use the git reset
command to uncommit or remove those last commits in the local Git repository.
The git reset
is a command used to undo local changes to the state of a Git repository.
Suppose we have a change to the working directory. We can view the change’s status with the git status
command.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
(commit or discard the untracked or modified content in submodules)
modified: mynotes.txt
We can see that the mynotes.txt
file is modified and eligible for commit.
To commit the modifications, we need to first use the git add
command to add the changes to the staging index of the local Git repository. We have to run the git add
command as follows.
$ git add .
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: mynotes.txt
Thus, upon using the git status
command, we can see that the changes are now staged.
We can commit the changes to the local Git repository. We must use the git commit
command to create a commit for the staged changes.
We run the git commit
command as follows.
$ git commit -m "updated mynotes"
[main e1b08a5] updated mynotes
1 file changed, 1 insertion(+)
Now, we run the git status
command again to see the status.
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
We can now see that the local branch in the local repository is ahead of the remote origin/main
Git repository branch by one commit.
We can use the git push
command to push the commit to the remote Git repository. But instead of doing that, we remove the unpushed commit using the git reset
command as follows.
$ git reset --soft HEAD~1
The git reset
command with the --soft
option removes the unpushed commit from the local Git repository but keeps the local changes. The HEAD~1
specifies the git reset
command to remove only one last commit.
We will now run the git status
to check the status of the repository as follows.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: mynotes.txt
Thus, we can see that the unpushed commit is no longer present. The modifications are still staged, though.
Using the --hard
option instead of the --soft
command option with the git reset
command would have deleted the recent commit as specified by the HEAD~1
and the local changes done.
We can run the git reset
command with the --hard
option.
$ git reset --hard HEAD~1
This also deletes the untracked files or directories along with the tracked modifications. Thus, use it with caution, or we might lose all the work done.
Thus, we have learned to remove unpushed commits in the local Git repository.
For more information, please visit:
Related Article - Git Reset
- Difference Between the Git Reset, Revert, and Checkout Commands
- How to Make the Development Branch Identical to the Master Branch
- How to Remove Local Git Changes
- How to Revert a Git Merge With Conflicts
- Difference Between Git RM --Cached and Git Reset File
- How to Revert a Git Repository by Commit ID