Rollback Commits
Undo Commits
-
Undo last commit putting everything back into the staging area:
git reset --soft HEAD^
-
Add files and change message with:
git commit --amend -m "New Message"
-
Undo last and remove changes:
git reset --hard HEAD^
-
Same as last one but for two commits back:
git reset --hard HEAD^^
** Don’t reset after pushing **
Reset Workflow
- Edit file again ‘edit_this_file.rb’
- Check status
- Add and commit with wrong message
- Check log
- Amend commit
- Check log
- Soft reset
- Check log
- Pull for updates
- Push changes
Commands
# Change file edit_this_file.rb
git status
git commit -am "kjkfjkg"
git log
git commit --amend -m "New comment added"
git log
git reset --soft HEAD^
git log
git pull origin master
git push origin master
Note
- git revert vs git reset
- Reset removes the commit while revert removes the changes but leaves the commit
- Revert is safer considering we can revert a revert
# Changed file
git commit -am "bug introduced"
git revert HEAD
# New commit created reverting changes
# Now we want to re apply the reverted commit
git log # take hash from the revert commit
git revert <rev commit hash>
# reverted commit is back (new commit created again)
Help and feedback
If there's something you don't like about this feature
To propose functionality that GitLab does not yet offer
To further help GitLab in shaping new features
If you didn't find what you were looking for
If you want help with something very specific to your use case, and can use some community support
POST ON GITLAB FORUM
If you have problems setting up or using this feature (depending on your GitLab subscription)
REQUEST SUPPORT
To view all GitLab tiers and features or to upgrade
If you want to try all features available in GitLab.com
If you want to try all features available in GitLab self-managed
If you spot an error or a need for improvement and would like to fix it yourself in a merge request
EDIT THIS PAGE
If you would like to suggest an improvement to this doc