Revert pushed commit from git

There are 2 way to revert pushed code using git commands


1) First Way, Let suppose your current branch name is staging and you want to revert commit from staging
  1. git log
     you will see commit details
  2.  now git checkout <commit_hash_where_you_want_to_point_your_head>
       
        in this picture i want to remove 1st commit
        
       so my command will be git checkout a804a3c4662334b0aa33860548841db54fb508bb
   
    3. Now Head will be on second commit so create new branch from here git checkout -b staging_copy (name will be depends on you)
 
   4. Now staging_copy branch does not have reverted commit, means its code is clean
  
   5. Now delete your staging branch from local machine git branch -D staging

   6. now rename your staging_copy branch to staging git branch -M staging

   7. now we need to force push to staging branch git push origin staging -f

This process is lengthy and complex but we are using this because in this process your local changes does not affected. this process is quite useful when your dont want to stash your changes

2) There is another simple process to revert your commits
    
    before running this command you have stash your local staged changes otherwise that file will checkout
     a) git stash save then revert your code
   1) git reset --hard HEAD  (going back to HEAD) 
   2) git reset --hard HEAD^  (going back to the commit before HEAD) 
   3) git reset --hard HEAD~2 ( going back two commits before HEAD)

  get staged file again git stash apply


















Comments

Popular posts from this blog

Wicked-PDF not showing images, 'wicked_pdf_image_tag' undefined

DmL command || Sql Tutorial -7