I want to remove few commits from my remote repository. I have few commits like this in my repo:
commits messages
abcd123 some message
xyze456 another commit message
98y65r4 this commit is not required
987xcrt this commit is not required
bl8976t initial commit
I want to remove commit 98y65r4
and 987xcrt
from my repo. How to achieve it?
You can simply remove that commit using option "d" or Removing a line that has your commit. In the latest git version there is no more option d. You need just remove lines with commits from rebase to delete them.
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.
There are two alternatives to this: the safe one that leaves you with a dirty git history, or the unsafe one that leaves you with a clean git history. You pick:
You can tell git to "Revert a commit". This means it will introduce a change that reverts each change you made in a commit. You would need to execute it twice (once per commit):
git revert 98y65r4
git revert 987xcrt
This solution will leave your git history like this (you can execute gitk --all
to see a graphical representation of the state of your repo):
2222222 revert of 987xcrt: this commit is not required
1111111 revert of 98y65r4: this commit is not required
abcd123 some message
xyze456 another commit message
98y65r4 this commit is not required
987xcrt this commit is not required
bl8976t initial commit
Then you can push the new 2 commits to your remote repo:
git push
This solution is safe because it does not make destructive operations on your remote repo.
You also can use an interactive rebase for that. The command is:
git rebase -i bl8976t
In it, you are telling git to let you select which commits you want to mix together, reorder or remove.
When you execute the command an editor will open with a text similar to this:
pick bl8976t initial commit
pick 987xcrt this commit is not required
pick 98y65r4 this commit is not required
pick xyze456 another commit message
pick abcd123 some message
Just go on and delete the lines you don't want, like this:
pick bl8976t initial commit
pick xyze456 another commit message
pick abcd123 some message
Save the file and close the editor.
So far, this has only modified the local copy of your repository (you can see the tree of commits with gitk --all
).
Now you need to push your changes to your repo, which is done with a "push force", but before you execute the command bear in mind that push force is a destructive operation, it will overwrite the history of your remote repository and can cause merge troubles to other people working on it. If you are ok and want to do the push force, the command is:
git push -f
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