How to Fix a Mistaken Commit Immediately?

How to Fix a Mistaken Commit Immediately?

Daily short news for you
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » 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

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member 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 danh5 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ống5 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