How to Fix a Mistaken Commit Immediately?

How to Fix a Mistaken Commit Immediately?

Daily short news for you
  • Not to be outdone, Google recently introduced Gemini CLI - a type of AI agent similar to Codex or Claude Code.

    What’s notable is that they offer up to... 1000 queries for free each day. That’s quite a lot. Additionally, they have open-sourced this project to ensure transparency, learning, and research 🤓

    » Read more
  • There’s another tool to help quickly search command history: atuinsh/atuin.

    What’s interesting is that it uses SQLite for storage. It also provides the feature of completely synchronizing (encrypting) history between machines. How cool is that 🤓

    » Read more
  • I am really impressed with the gemma-3n-E4B model from Google. This is one of the promising models that aims to bring large language models to run on mobile devices or the web or embedded...

    It feels like it understands the prompts better, because I’ve tried many low-parameter models that often overlook my prompts. For example, when I say, "Just return the answer, no need for further explanation," many still tend to add introductory phrases or explanations... but with gemma-3n, the responses are very much to the point.

    » 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 danh9 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ống9 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