How to Fix a Mistaken Commit Immediately?

How to Fix a Mistaken Commit Immediately?

Daily short news for you
  • For a long time, I have been thinking about how to increase brand presence, as well as users for the blog. After much contemplation, it seems the only way is to share on social media or hope they seek it out, until...

    Wearing this shirt means no more worries about traffic jams, the more crowded it gets, the more fun it is because hundreds of eyes are watching 🤓

    (It really works, you know 🤭)

    » Read more
  • A cycle of developing many projects is quite interesting. Summarized in 3 steps: See something complex -> Simplify it -> Add features until it becomes complex again... -> Back to a new loop.

    Why is that? Let me give you 2 examples to illustrate.

    Markdown was created with the aim of producing a plain text format that is "easy to write, easy to read, and easy to convert into something like HTML." At that time, no one had the patience to sit and write while also adding formatting for how the text displayed on the web. Yet now, people are "stuffing" or creating variations based on markdown to add so many new formats that… they can’t even remember all the syntax.

    React is also an example. Since the time of PHP, there has been a desire to create something that clearly separates the user interface from the core logic processing of applications into two distinct parts for better readability and writing. The result is that UI/UX libraries have developed very robustly, providing excellent user interaction, while the application logic resides on a separate server. The duo of Front-end and Back-end emerged from this, with the indispensable REST API waiter. Yet now, React doesn’t look much different from PHP, leading to Vue, Svelte... all converging back to a single point.

    However, the loop is not bad; on the contrary, this loop is more about evolution than "regression." Sometimes, it creates something good from something old, and people rely on that goodness to continue the loop. In other words, it’s about distilling the essence little by little 😁

    » Read more
  • Alongside the official projects, I occasionally see "side" projects aimed at optimizing or improving the language in some aspects. For example, nature-lang/nature is a project focused on enhancing Go, introducing some changes to make using Go more user-friendly.

    Looking back, it resembles JavaScript quite a bit 😆

    » Read more

The Problem

Note: If you are looking to delete a pushed commit, please refer to this solution How to Delete a Pushed Commit?. The following article is only a guide to deleting a "not" pushed commit, yet somehow it often appears in the top Google search results. Thank you!

A daily task for programmers is to write code, and by evening before leaving, we often ask each other, "Have you committed the code yet?". Or sometimes we hear a quirky joke like, "Even if the sky falls, don’t forget to commit the code before leaving." Because of someone’s urgent push, after committing, you realize there’s another file you forgot to add with the commit. It’s really frustrating, but should you add another commit just to push it up to remote?

Many people choose to create a new commit to correct the mistake, but that might be unnecessary because you may not know that an unpushed commit can still be fixed. So how do you fix the commit immediately?

Adding New Content to the Commit

Git allows us to change the last commit with the command git commit --amend --no-edit.

For example, right after committing, you realize you forgot to add README.md:

$ git add README.md  
$ git commit --amend --no-edit  

And that's it, the changes in README.md are now included in the last commit.

Removing Content from the Commit

Similarly, if you accidentally added a file, you can remove it using git reset HEAD^ path/to/file.

For example, right after committing, you realize you mistakenly added README.md to the commit and want to remove it:

$ git reset HEAD^ README.md  
$ git commit --amend --no-edit  

Changing the Commit Message

Git also allows you to change the message of the last commit with the command git commit --amend -m <message>.

$ git commit --amend -m "edited message"  

Soft Reset

This is a method when you want to roll back the last commit. That is, cancel the last commit but keep all changes in the staged state.

$ git reset --soft HEAD^  

This command cancels the last commit and returns all changes to staged, allowing you to start committing again from scratch.

Deleting an Unpushed Commit Without Reverting

This method applies to cases where you just committed locally and have not pushed to remote.

$ git reset --hard origin/branch  

With origin being the remote and branch being the working branch.

Note: A hard reset synchronizes the remote with the local. This means that whatever commits are on the remote will be brought down to local. All local commits that are not on the remote will be deleted. Be cautious when using this.

For example, if I just committed to develop and want to delete it:

$ git reset --hard origin/develop  
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 danh8 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ống8 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ền2 years 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