I know that's rewriting of history which is bad yada yada.
But how to permanently remove few commits from remote branch?
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.
To delete commits from remote, you can use the git reset command if your commits are consecutive from the top or an interactive rebase otherwise. After you delete the commits locally, push those changes to the remote using the git push command with the force option.
To undo the last commit from a remote git repository, you can use the git reset command. command. This will undo the last commit locally. command to force push the local commit which was reverted to the remote git repository.
All you need to do is typing "drop" at the beginning of each commit you want to delete. Be careful while using the git rebase command, as it may cause sudden problems. So, it is more recommended to use the git revert command.
You git reset --hard
your local branch to remove changes from working tree and index, and you git push --force
your revised local branch to the remote. (other solution here, involving deleting the remote branch, and re-pushing it)
This SO answer illustrates the danger of such a command, especially if people depends on the remote history for their own local repos.
You need to be prepared to point out people to the RECOVERING FROM UPSTREAM REBASE section of the git rebase
man page
With Git 2.23 (August 2019, nine years later), you would use the new command git switch
.
That is: git switch -C mybranch origin/mybranch~n
(replace n
by the number of commits to remove)
That will restore the index and working tree, like a git reset --hard
would.
The documentation adds:
-C <new-branch> --force-create <new-branch>
Similar to
--create
except that if<new-branch>
already exists, it will be reset to<start-point>
.
This is a convenient shortcut for:$ git branch -f <new-branch> $ git switch <new-branch>
Just note to use the last_working_commit_id
, when reverting a non-working commit
git reset --hard <last_working_commit_id>
So we must not reset to the commit_id
that we don't want.
Then sure, we must push to remote branch:
git push --force
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