(Git) Deleting a commit
When you’re working with Git, there should be a time when you commit/push in the wrong way and want to take it back. In Git, there is a way to do that. I will show you how to delete the latest commit.
1. Get the hash
First, type ‘git log’ command to get information about your commit where you want to go back. You will see a hash right next to the word commit, so copy that hash.
// Log your commits
git log// You will see your hash like below:
commit HASH (HEAD -> master, origin/master)
Author: Kenta Kodashima <email@example.com>
Date: Mon Sep 10 12:23:23 2018 -0700Some commit messages.
2. Back to after the specific commit
Next, in order to reset your local repository to the specific commit, use ‘git reset’ command with the hash which you got in the previous step.
NOTE:
If you just want the commit to be removed, use ‘soft’ option instead of ‘hard’ because ‘hard’ option will remove your files too.
// Reset to a specific commit
git reset --hard YOUR_HASH_CODE
3. Delete the specific commit
Now it’s about time to delete the commit you want to delete. Use ‘git rebase -i’ command to do that. ‘-i’ will make a list of the commits which are about to be rebased. The number right after ‘~’ symbol below defines how many commits you want to see. So if you want to see just 1 commit from now, you can just use ‘HEAD~1’ or ‘HEAD^’.
Typing ‘git rebase -i’ command bring up the editor as below. Just delete the specific commit’s row you want to delete. Then you can just enter ‘:wp’ to get out from Vim editor (wq stands for ‘write’ and ‘quit’).
// You can use any number right after HEAD~ to see your commit list
git rebase -i HEAD~1pick FIRST_PART_OF_HASH Some commit messages.# Rebase xxxxxxx onto yyyyy(1 command)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
If you have unstaged changes you might get an error like below.
Cannot rebase: You have unstaged changes.
In order to avoid this error, you can use ‘git stash’ to save your unstaged changes away.
// Stash your unstaged changes
git stash save
4. push to delete the commit from the remote repository
After you delete the commit from your local repository, you want to delete the commit from your remote repository too if you have already pushed it. Just push your repository’s current state with ‘-f’ option. You need ‘-f’ option to forcibly push to the remote otherwise it gives you an error because the state of your local and remote repository is different.
// push the the deletion of the commit forcibly
git push -f
That’s everything! Thank you for reading.
Reference:
Git Official Documentation:
(rebase) https://git-scm.com/docs/git-rebase
(stash) https://git-scm.com/docs/git-stash
Stack Overflow:
https://stackoverflow.com/questions/3042437/how-to-change-the-commit-author-for-one-specific-commit
The method of deleting a past commit (Written in Japanese):
https://qiita.com/ykawakami/items/0d6826c529ad7c7b37dd