Removing unwanted files from local and reverse commit/merge

This question already has an answer here:

  • How to revert Git repository to a previous commit? 38 answers

  • As I already pointed out in the comments, your question is basically a duplicate of How to revert Git repository to a previous commit?, where the top voted answer offers two solutions that are relevant to you.

    Solution 1: Hard Reset

    You mentioned that you already pushed to your remote. That's OK, if you're not sharing your branch with other people, then you can simply do a hard reset back to the commit that you want to restore, and then force push back to your remote:

    git reset --hard sha-2
    git push origin HEAD -f
    

    Warning: don't do this if you're sharing your branch with other people , or at least if it's not OK with them if you throw away commits on the remote branch like this. Those other people may have their own work based off of the old commits that you're throwing away, so it creates more work for those users to resync their own commits with the newly reset branch.

    If you can't use this solution for the reason above, then you can use the following alternative solution, as mentioned in How to revert Git repository to a previous commit?.

    Solution 2: Revert

    This solution simply reverts the previous commits by creating new commits that are a reverse-patch. This is safe to use even if other users are sharing your branch, because it doesn't throw away new commits, it just creates new ones, so it isn't as confusing to synchronize with as throwing away commits would be:

    git revert --no-edit sha2..sha5
    

    That will revert the commits in the range (sha2, sha5], where sha2 is an exclusive starting point, meaning it isn't included in the reverts. Then you can simply push the results to your remote,

    git push origin HEAD
    

    Documentation

  • Official git-reset(1) Manual Page
  • Official git-revert(1) Manual Page
  • Additional Notes

    I tried to have this question closed as a duplicate of How to revert Git repository to a previous commit?, but it looks like the question isn't getting enough close votes, and might not be closed, so that's why I decided I better answer it just in case it isn't.


    Try to use:

    git reset --hard <previous-commit-hash>
    

    This resets everything to your previous commit. and you will get the fresh copy of your previous-commit-hash .

    链接地址: http://www.djcxy.com/p/3156.html

    上一篇: 在Git中恢复到特定点

    下一篇: 从本地删除不需要的文件并反向提交/合并