In order to undo the last Git commit, keep changes in the working directory but NOT in the index, you have to use the “git reset” command with the “–mixed” option. Next to this command, simply append “HEAD~1” for the last commit.
To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.
Luckily, you can process your files with Git and remove from commit and fix the history. If you've made several commits or cloned a repository with an already existing commit history, you'd typically want to look back and see what all commits were made.
To drop a commit, simply replace the command 'pick' with 'drop' and close the editor. You can also delete the matching line. The following command will remove an entire commit e78d8b1 in one go using the --rebase-merges mode with the --onto option. That's all about deleting commits from a Git branch.
git-rebase(1) does exactly that.
$ git rebase -i HEAD~5
git awsome-ness [git rebase --interactive] contains an example.
git-rebase
on public (remote) commits.commit
or stash
your current changes).$EDITOR
.pick
before C
and D
by squash
. It will meld C and D into B. If you want to delete a commit then just delete its line.If you are lost, type:
$ git rebase --abort
# detach head and move to D commit
git checkout <SHA1-for-D>
# move HEAD to A, but leave the index and working tree as for D
git reset --soft <SHA1-for-A>
# Redo the D commit re-using the commit message, but now on top of A
git commit -C <SHA1-for-D>
# Re-apply everything from the old D onwards onto this new place
git rebase --onto HEAD <SHA1-for-D> master
Here is a way to remove a specific commit id knowing only the commit id you would like to remove.
git rebase --onto commit-id^ commit-id
Note that this actually removes the change that was introduced by the commit.
To expand on J.F. Sebastian's answer:
You can use git-rebase to easily make all kinds of changes to your commit history.
After running git rebase --interactive you get the following in your $EDITOR:
pick 366eca1 This has a huge file
pick d975b30 delete foo
pick 121802a delete bar
# Rebase 57d0b28..121802a onto 57d0b28
#
# 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
You can move lines to change the order of commits and delete lines to remove that commit. Or you can add a command to combine (squash) two commits into a single commit (previous commit is the above commit), edit commits (what was changed), or reword commit messages.
I think pick just means that you want to leave that commit alone.
(Example is from here)
You can non-interactively remove B and C in your example with:
git rebase --onto HEAD~5 HEAD~3 HEAD
or symbolically,
git rebase --onto A C HEAD
Note that the changes in B and C will not be in D; they will be gone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With