I know that's rewriting of history which is bad yada yada.
But how to permanently remove few commits from remote branch?
I know that's rewriting of history which is bad yada yada.
But how to permanently remove few commits from remote branch?
There are three options shown in this tutorial. In case the link breaks I'll leave the main steps here.
1 Revert the full commit
git revert dd61ab23
2 Delete the last commit
git push <<remote>> +dd61ab23^:<<BRANCH_NAME_HERE>>
or, if the branch is available locally
git reset HEAD^ --hard
git push <<remote>> -f
where +dd61... is your commit hash and git interprets x^ as the parent of x, and + as a forced non-fastforwared push.
3 Delete the commit from a list
git rebase -i dd61ab23^
This will open and editor showing a list of all commits. Delete the one you want to get rid off. Finish the rebase and push force to repo.
git rebase --continue
git push <remote_repo> <remote_branch> -f
If you want to delete for example the last 3 commits, run the following command to remove the changes from the file system (working tree) and commit history (index) on your local branch:
git reset --hard HEAD~3
Then run the following command (on your local machine) to force the remote branch to rewrite its history:
git push --force
Congratulations! All DONE!
Some notes:
You can retrieve the desired commit id by running
git log
Then you can replace HEAD~N with <desired-commit-id> like this:
git reset --hard <desired-commit-id>
If you want to keep changes on file system and just modify index (commit history), use --soft flag like git reset --soft HEAD~3. Then you have chance to check your latest changes and keep or drop all or parts of them. In the latter case runnig git status shows the files changed since <desired-commit-id>. If you use --hard option, git status will tell you that your local branch is exactly the same as the remote one. If you don't use --hard nor --soft, the default mode is used that is --mixed. In this mode, git help reset says:
Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated.
TL:DR;
git switch -C branch_name origin/branch_name~ngit push --forceDone, remote branch will be reverted by n commits.
Explaination:
Use git switch, resets the branch by n number of commits. The -C option will force create a new branch with same name.
branch_name with your branch name,n (at the end of command), with number of commits you want to revert.Command #1: git switch -C branch_name origin/branch_name~n
Example: git switch -C feature/dashboard origin/feature/dashboard~1 // This command reverts 1 commit in dashboard branch.
Force push the local change
Command #2: git push --force
Tip: To undo committed(unpushed) changes git reset HEAD~
I like to do this using rebase. Below, n is the last n commits. So, if you want to delete the third one, replace n by 3.
git rebase -i HEAD~n
then, find the desired commit in the listing and change it from "pick" to "drop". Exit the rebase and use git push with the "-f" option as you just did a rebase.