How to revert last commit and remove it from history?

后端 未结 5 787
花落未央
花落未央 2020-12-12 19:08

I did a commit and reverted with

git revert HEAD^

just git log

➜  git:(master) git log
commit 45a0b1371e4705c4f875141232d7         


        
5条回答
  •  我在风中等你
    2020-12-12 19:38

    Here is a simple working solution that delete the last commit from remote:

    1. clone the repo and find the last 'good' commit (....c407)
    $ git clone git@host:PROJ/myrepo.git 
    $ cd myrepo 
    $ git log --pretty=oneline
    
    234987fed789de97232ababababcef3234958723 bad_commit
    e4a2ec4a80ef63e1e2e694051d9eb3951b14c407 v4.3
    2f116449144dbe46e7260d5dac2304803751b5c2 v4.2
    
    1. checkout the last good commit to a new temp branch
    $ git checkout e4a2ec4a80ef63e1e2e694051d9eb3951b14c407
    $ git checkout -b temp_branch
    
    1. replace the remote branch ( by delete and push the temp )
    git push origin --delete dev_branch
    git push origin temp_branch:dev_branch
    

提交回复
热议问题