Oh no, this commit is a mess! 🤦♂️
We all have a bad day sometimes. We can forget to include a file, leave some comments, or a merge didn't go as expected. Fortunately, Git has some commands to deal with these common situations and I want to show you some of them.
Modify commit message
Oops... You found a spelling mistake in the commit message. No worries, you can modify it:
bash
git commit --amend -m "new message"
Add files to last commit
You already have committed the changes but forgot to add some files. No problem, you can still add them to the commit:
bash
git add <file_name>
git commit --amend HEAD~1
Undo commits
If you want to undo the last commit but keep the changes:
bash
git reset --soft HEAD~1
If you want to undo both commit and changes: ⚠️ Be sure that you want to lose the changes:
bash
git reset --hard HEAD~1
Alternatively, if you want to undo all your local changes, you can reset to the origin version of the branch:
bash
git reset --hard origin/<branch_name>
If you want to undo the commit without altering the existing history. You can use git revert
, this command undoes a commit by creating a new commit:
bash
git revert HEAD
If you have just resolved some conflicts, finished the merge, and push to origin. But wait, something went wrong...
The safe way to undo a merge commit that has already pushed to the remote branch is using the git revert
command:
bash
git revert -m 1 <commit_id>
commit_id
is the merge commit id that you want to revert.
Notes:
-
You can also undo any number of commits. E.g:
-
git reset HEAD~3
(going back three commits before HEAD). -
git reset --hard <commit_id>
(going back to a specific commit).
-
Use
git reset
if the commit is not pushed yet and you don't want to introduce a bad commit to the remote branch.Use
git revert
to revert a merge commit that has already pushed to the remote branch.Use
git log
to review the commit history.If you prefer, you can also create a new commit with the fix.
I hope you find these commands as useful as I do and can use them. If you already knew these or think one is missing, please let me know in the comments. Thanks for reading!
Top comments (22)
Those are some great tips! I always loved this site called „Oh Sh*t, git“ because it has a pretty elaborative list of what can go wrong and how to fix it: ohshitgit.com/
What a great resource! Thanks a lot for sharing it. 🚀
just learn it like any other language or a tool in toolchain.
Like cmake or makefile syntax... :)
Its not hard especially if you do not mind studying how it is implemented there are nice courses on the topic.
thanks, really useful. It's funny that most dev use git, but not many know stuff you mentioned here.
Thanks to you, @pavelee ! 🙏🏻️
Thank you for useful tips
Thank you 🙌
great tips, thanks
Thank you! 🙌
Using reflog can also be helpful to see what you did on your local. :)
Thanks for mentioning it! It's very useful in figuring out what happened
Thanks for not mentioning force push 😉 Worked at project where lead used it routinely, and that was very frustrating
Thanks, @alekseiberezkin ! I can't imagine how frustrating it was. 🤦🏻♂️️
Useful tips
Thank you, @mrzhouzh ! 🙏🏻️
Wrote this script to undo your last git f**k up (still in WIP for a stable release)
Might help someone :)
Bhupesh-V / ugit
🚨️ ugit helps you undo your last git command with grace. Your damage control git buddy
ugit
Undo your last oopsie 🙈️ in git without much effort
Why
ugit
git
command you didn't want to.What's in the box
ugit
supports undoing following operations, some are a WIP. If you know of any operation that can be undone and is not in the list, make sure to send a quick PRgit commit
git add
git push
git branch -D
(branch delete)git pull
git reset
git tag -d
(tag delete)git stash apply
git stash pop/drop/clear
git merge
git tag
(rename a tag)git rebase
git cherry-pick
git worktree remove
(recover deleted work-tree)Installation
…
Wow! Great job! 🤯️
Wow, that's super useful. I have to say I LOVE git, but I don't know as much as I'd like on how to use it properly haha
Thanks a lot @lelepg , Git is hugeee!