How to Fix a Mistaken Commit Immediately?

How to Fix a Mistaken Commit Immediately?

Daily short news for you
  • A software that converts text to speech created by a Vietnamese programmer - J2TEAM - Text to Speech (Free). You can convert dozens of languages into dozens of different natural voices. The special thing is that it is free.

    In preliminary evaluation, the conversion of long texts or texts in pure Vietnamese is very good. However, when it includes English words, it sounds a bit funny 😅

    » Read more
  • How terrifying, Codeium - known as a competitor to Github Copilot, as it allows users to use it for free without limits. Recently, they introduced the Windsurf Editor - no longer just a VSCode Extension but a full Editor now - directly competing with Cursor. And the main point is that it... is completely free 🫣.

    » Read more
  • There is a rather interesting study that I came across: "Users never bother to read things they don't want to." (That's a bold statement, but it's more true than not. 😅)

    Don't believe it? I bet you've encountered situations where you've clicked on a button repeatedly and it doesn't respond, but in reality, it has displayed an error message somewhere. Or you've filled out everything and then when you hit the submit button, it doesn't go through. Frustrated, you scroll up or down to read and find out... oh, it turns out there's an extra step or two you need to take, right?

    It’s not far from the blog here. I thought that anyone who cares about the blog would click on the "Allow notifications" button just below the post. But the truth is, no one bothers to click it. Is it because they don't want to receive notifications? Probably not! I think it's because they just didn’t read that line.

    The evidence is that only when a notification pops up and takes up half the screen, or suddenly appears to grab attention, do they actually read it—and of course, it attracts a few more subscribers—something that was never achieved before.

    » Read more

Issue

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.

Fixing the Last Commit

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.

Modifying the Content of the Last Commit

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

Deleting a Commit Without Reverting

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

Conclusion

Above are some git commands to deal with situations related to "sloppy" commits. Regardless, before committing, everyone should double-check all the content carefully!

Premium
Hello

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!

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!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.
Author

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.

Did you find this article helpful?
NoYes

Comments (3)

Leave a comment...
Avatar
Ẩn danh3 months ago
Muốn sửa lại message của commit cũ ko phải commit gần nhất, thì làm sao ạ?
Reply
Avatar
Xuân Hoài Tống3 months ago
Hơi khó em ạ, nếu thế em phải làm một số lệnh rồi force push lên remote. Mà để làm vậy thì tương đối khó, vì chẳng ai dám force push vì phát sinh nhiều vấn đề trong làm việc nhóm. Còn nếu dự án của em, một mình em làm thì em có thể force nếu muốn.
Avatar
Lê Kế Hiền1 year ago
vậy dùng source tree cho dễ =)) cl rối rắm
Reply
Avatar
Trịnh Cường2 years ago
"Lưu ý rằng việc này cũng đồng nghĩa với những commit chưa push ở local sẽ bị mất hết và bạn sẽ phải viết lại.". câu này có nghĩa là những đoạn code thay đổi ở commit đó cũng mất hết à bạn
Reply
Avatar
Xuân Hoài Tống2 years ago
Đúng rồi bạn, vì lệnh đó là reset lại branch local cho giống y hệt remote nên commit bạn chưa push ở local cũng sẽ bị mất hết