How to Fix a Mistaken Commit Immediately?

How to Fix a Mistaken Commit Immediately?

![How to Fix a Mistaken Commit Immediately?](toi-vua-lo-commit-sai-lam-sao-de-sua-lai-ngay-lap-tuc =1200x900)

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!

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 danh2 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ống1 month 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