Note: Are you looking for a solution to fix a commit that has already been pushed to the remote? If so, you should read the article How to Delete a Pushed Commit? to learn how to resolve it. This article you are currently reading only covers deleting "unpushed" commits, but I don't understand why it appears in Google search results. Thank you!
Committing code is a daily task for developers, but sometimes due to some reason, you make a commit with missing content or realize that there are still some places that need to be fixed. What should you do in those situations?
Many people may choose to create another commit to fix the mistake, but that would result in a git history with long, messy commits or even be perceived as sloppy and unprofessional.
Git allows us to modify the content of the last commit using the command git commit --amend --no-edit
. You can use it to resolve the issue mentioned above.
For example, my last commit is "release version 1.0.1" and right after committing, I realize that I forgot to add the README.md file. In this case, I can do the following:
$ git log
commit 56da84715291dbb683269c085fe9a4b42aafb1e7
Author: hoaitx <[email protected]>
Date: Sun Jun 20 15:31:35 2021 +0700
release version 1.0.1
$ git add README.md
$ git commit --amend --no-edit
$ git log
commit ac48bc383726cfaaf4032b68ef6e6bece6cec368
Author: hoaitx <[email protected]>
Date: Sun Jun 20 15:32:35 2021 +0700
release version 1.0.1
After that, check the commit history again, and you will see that the README.md file has been added.
Git also allows you to modify the message of the last commit using the command git commit --amend -m <message>
.
For example, if I want to change the message from "release version 1.0.1" to "release v1.0.1", I can do the following:
$ git commit --amend -m "release v1.0.1"
$ git log
commit dbdd6c3844a01b3d03bbc777516fba16ba07d64d
Author: hoaitx <[email protected]>
Date: Sun Jun 20 15:33:35 2021 +0700
release v1.0.1
This method only applies when you have only committed locally and have not pushed to the remote. When you commit without pushing to the remote, you can delete them locally using the command git reset --hard <remote/branch>
.
This command will reset the local HEAD to match the remote HEAD, essentially synchronizing the remote commits to the local repository, which will delete any unpushed commits in the local repository.
Note that this also means that any unpushed commits in the local repository will be lost, and you will have to rewrite them.
For example, I'm resetting the develop branch to the remote branch named origin:
$ git reset --hard origin/develop
Above are some git commands to deal with situations related to "sloppy" commits. Regardless, before committing, everyone should double-check all the content carefully!
The secret stack of Blog
As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!
Subscribe to receive new article notifications
Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.
Comments (3)