Git permanently deletes the commit

Once I accidentally submitted the code that should not be submitted to GIT, and submitted several commits before discovery. Now I want to permanently delete this commit on the GIT server. The specific steps are recorded as follows.


    Assuming that the current branch is master, the current commit situation is as follows, and now commit_id_2 and commit_id_4 need to be deleted:

commit_id_1
commit_id_2
commit_id_3
commit_id_4
commit_id_5
....


1. Create a new branch for reset operation

git checkout -b develop
git push orign -u develop


Second, on the new branch develop, reset the commit to the commit before commit_id_4

git reset --hard <commit_id_5>


3. Submit a new branch to overwrite the previous commit information

git push orign HEAD --force


Fourth, according to the original order to restore the commit does not need to delete, and then submit

git cherry-pick commit_id_3
git cherry-pick commit_id_1
git push orign develop


Five, delete the original master branch, checkout a new branch from develop as master

git push orign --delete master 
// or git push orign: master 
// Note: master is generally the default branch, you need to set the default branch to develop to delete 

git checkout -b master 
git push orign -u master 
// last Reset master to default branch


The operation is cumbersome, but it is relatively safe. If there is any good way, you can share it. .

发布了60 篇原创文章 · 获赞 44 · 访问量 34万+

Guess you like

Origin blog.csdn.net/beyond702/article/details/64549705