Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to permanently remove few commits from remote branch

Tags:

git

I know that's rewriting of history which is bad yada yada.

But how to permanently remove few commits from remote branch?

like image 535
Arnis Lapsa Avatar asked Jul 20 '10 19:07

Arnis Lapsa


People also ask

How do I remove multiple commits from a 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.

How do you remove a few 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.

How remove commit from remote branch?

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.

Can I delete specific commits?

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.


2 Answers

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> 
like image 144
VonC Avatar answered Oct 06 '22 13:10

VonC


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 
like image 20
hd84335 Avatar answered Oct 06 '22 13:10

hd84335