My scrapbook about almost anything I stumble upon in my tech world. If you find anything useful don't forget to give thumbs-up :)


Tuesday, February 4, 2020

Rewrite history with Git reset - Undo changes in Git

Continuing the topic Modify the last (recent) commit in git, today we are going to explore 'reset' command in git. This command (git reset) rewrite the history, i.e.- delete the commits, so not advisable when your changes are already pushed into remote git repo. 

Scenario #1:  Change commits are not being pushed to git repo (want to reset the local commit)
Before running git reset, keep this in mind that this command will delete the changes done so take a backup of current repo if you want to change your mind later. If you are sure about resetting, we'll proceed.

  • Either reset the uncommitted local changes or stash them
   git checkout -- <files>          # this will remove the uncommitted change
   git stash save "message"       # this will stash the changes for later use
  • git reset works in 3 ways but mostly used these 2 - soft and hard
  • Below command will soft reset the last commit (delete the last commit but keep the last commit changes as working files) 
   git reset --soft HEAD~1                            # reset last commit
   git reset --soft HEAD~3                            # reset last 3 commit
       git reset --soft 7a2bf5x..HEAD           # reset commit from  sha 7a2bf5x to HEAD   

  • Now, run git status command to check if the git history is changed as you wanted and changes are safe as unstaged/working files
  • If you run, reset hard, git will flush the changes as well which will not allow you to go back if want to. So, think before running reset hard. 
  • Command is pretty similar as soft reset 
   git reset --hard HEAD~1                            # reset last commit 
   git reset --hard HEAD~3                            # reset last 3 commit
   git reset --hard 7a2bf5x..HEAD           # reset commit from  sha 7a2bf5x to HEAD  

After resetting/rewriting git history, you can push your changes to remote git repo.

Scenario #2:  Change commits are already pushed to git repo (want to reset the remote commit)

NOT ADVISABLE at all as it will rewrite the remote commit history which might be pulled by some of the folks who are sharing this repo with you and make their work a bit messy and as it deletes the commit from git history, there is no way back then doing all the changes again which might be more messy.

But if you still want to do it, execute the command as you like and force push to remote repo.

git push -f origin <branch>

Like the below page to get the update  
Facebook Page      Facebook Group      Twitter Feed      Telegram Group

No comments:

Post a Comment


The postings on this site are my own and don't necessarily represent IBM's or other companies positions, strategies or opinions. All content provided on this blog is for informational purposes and knowledge sharing only.
The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of his information.