How to Fix a Mistaken Commit Immediately?

How to Fix a Mistaken Commit Immediately?

Daily short news for you
  • Void - the name I've mentioned quite some time ago. From the time when continue.dev just emerged. It's similar to Cursor and Windsurf, and today they've released the Beta version and allowed everyone to download it.

    The strength of this is that it is open source, free, and uses free local models on your machine via Ollama or LM Studio... If you don't like it, you can plug in APIs from other providers. I just tried it out and found the suggestion capabilities and chat framework quite similar to Cursor, and it even has an Agent feature 👏. It's more stable than continue.dev (the last time I used it), and the only thing left to do is to choose a better model 🤤

    » Read more
  • Zed has recently introduced a new feature called Agent - similar to Agent in Cursor or Write in Windsurf, and they call it The Fastest AI Code Editor.

    It is indeed fast because Zed is written in Rust. However, their strategy seems to be shifting, focusing on AI instead of developing the currently limited extensions that cannot compete with VSCode 🥶

    Zed: The Fastest AI Code Editor

    » Read more
  • Right after the news that OpenAI reached an agreement to acquire Windsurf for $3 billion, today Cursor has offered 1 year of free Pro access for students. Chaaaaà 🤔

    OpenAI Reaches Agreement to Buy Startup Windsurf for $3 Billion

    Cursor for Students | Cursor - The AI Code Editor

    » 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 danh7 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ống7 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