How To Remove An Unpushed Commit In Git?
While working on a repository, a developer can often commit something by mistake without pushing it to the remote server. Luckily, Git gives a quick way to remove the unpushed commit.
To remove the last unpushed commit in Git, use this command:
bashgit reset --soft HEAD~1
To remove all unpushed commits in Git, use the git reset command:
bashgit reset --hard origin
This article will show different ways to remove unpushed commits in Git and answer some of the most common questions about removing commits.
Let's get to it 😎.
How to delete the last unpushed commit?
To delete or uncommit the last unpushed commit, you need to use the git reset command.
The git reset command undoes local changes in a Git repository. It has three invocation types (soft, hard, and mixed).
To delete the last unpushed commit AND keep the changes, use:
bashgit reset --soft HEAD~1
Use the git status command to verify that the command worked.
To undo the last unpushed commit AND unstage the files, use:
bashgit reset --mixed HEAD~
To delete the last unpushed commit AND destroy the work, use:
bashgit reset --hard HEAD~1
In Git, HEAD refers to the last commit of the active branch. HEAD~1 refers to the latest commit.
How to remove all unpushed commits?
Removing all unpushed commits involves syncing the local repository with the remote server.
To delete all unpushed commits in your Git repository, use:
bashgit reset --hard origin
To delete all unpushed commits from a specific branch, use:
bashgit reset --hard origin/<branch>
This command will sync your local repository with the remote and eliminate all uncommitted commits.
Final thoughts
As you can see, removing an unpushed commit in Git is very easy.
You just need to use the git reset command and continue working!
Here are some other Git tutorials that I wrote for you to enjoy: