How to Fix a Mistaken Commit Immediately?

How to Fix a Mistaken Commit Immediately?

Daily short news for you
  • After waking up, I saw everyone buzzing about the Reasoning R1 model from Deepseek.

    Wow, what's all the hype about? There's a lot! First of all, it's open-source, then there's its performance, which is on par with OpenAI's o1, and thirdly, there are multiple parameter options from 1.5B to 70B to choose from or research.

    » Read more
  • Lets go hot 🔥🔥🔥

    A really great and detailed guide about jj - Jujutsu - as I shared earlier jujutsu-tutorial

    » Read more
  • Every time I create a new website, what is the first thing that gives you the biggest headache? For me, it’s definitely the layout. Within the layout, there is one aspect that has a very profound impact, which is the font. Indeed, a website becomes much more elegant depending heavily on the font. The combination of multiple fonts together, placed correctly, and visually pleasing to users will leave a very deep impression.

    However, choosing fonts has never been easy; I just discovered the site uifonts.app that helps everyone quickly check whether this font fits their new website idea or not 😇

    » 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

5 profound lessons

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

View all

Subscribe to receive new article notifications

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

Comments (3)

Leave a comment...
Avatar
Ẩn danh4 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ống4 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
Scroll or click to go to the next page