How to permanently remove few commits from remote branch

后端 未结 8 1590
予麋鹿
予麋鹿 2020-11-27 09:07

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

But how to permanently remove few commits from remote branch?

相关标签:
8条回答
  • 2020-11-27 09:13

    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>
    
    0 讨论(0)
  • 2020-11-27 09:18

    Important: Make sure you specify which branches on "git push -f" or you might inadvertently modify other branches![*]

    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
    2. Delete the last commit
    3. Delete commit from a list

    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
    
    0 讨论(0)
  • 2020-11-27 09:20

    Sometimes the easiest way to fix this issue is to make a new branch from the place where you know the code is good. Then you can leave the errant branch history alone in case you need to cherry-pick other commits from it later. This also ensures you did not lose any commit history.

    From your local errant branch:

    git log
    

    copy the commit hash that you wanted the branch to be at and exit the git log

    git checkout theHashYouJustCopied
    git checkout -b your_new_awesome_branch
    

    Now you have a new branch just the way you want it.

    If you also needed to keep a specific commit from the errant branch that is not on your new branch, you can just cherry-pick that specific commit you need:

    git checkout the_errant_branch
    git log
    

    Copy the commit hash of the one commit you need to pull into the good branch and exit the git log.

    git checkout your_new_awesome_branch
    git cherry-pick theHashYouJustCopied
    

    Pat yourself on the back.

    0 讨论(0)
  • 2020-11-27 09:22

    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
    
    0 讨论(0)
  • 2020-11-27 09:22

    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.

    0 讨论(0)
  • 2020-11-27 09:25
     git reset --soft commit_id
     git stash save "message"
     git reset --hard commit_id
     git stash apply stash stash@{0}
     git push --force
    
    0 讨论(0)
提交回复
热议问题