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 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~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 commitgit 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>
Facebook Page Facebook Group Twitter Feed Telegram Group
No comments:
Post a Comment