provides multiple unique tools for fixing up mistakes during your development. Commands such as , , and allow you to erroneous changes in your repository. Git toolbox git reset git checkout git revert undo Because they perform similar operations, it is easy to mix them up. There are a few guidelines and rules for when each command should and should not be used. Let’s take a look! very Be careful! You can’t always redo after an undo . This is one of the few areas in Git where you may lose some work if you do it wrong. Undoing with Git Commands Let’s start off by clarifying the main differences between these three commands. Checkout: Use this to move the to a specific commit or between branches. HEAD pointer switch It any content changes to those of the specific commit. rollbacks This will make changes to the commit history. not Has the potential to files in the working directory. overwrite Revert: you have committed. Rollback changes Creates a from a specified commit by inverting it. Hence, adds a new commit history to the project, but it does not modify the existing one. new commit Has the potential to overwrite files in the working directory. Reset: Use this to the working tree to the last committed state. This will discard commits in a private branch or throw away uncommitted changes! return entire Changes which commit a branch HEAD is currently pointing at. It alters the existing commit history. Can be used to a file. unstage lets you some kind of change in your repository, only checkout and reset can be used to manipulate either commits or individual files. Every command undo Using the Commands There are many different ways you can , it all depends on the current scenario. Selecting an appropriate method depends on whether or not you have committed the change by mistake, and if you have committed it, whether you have shared it or not. undo your changes Undo Public Changes Image that you did in hotfix branch for commits you didn't want to make yet. Scenario: [git push](https://kolosek.com/git-commands-tutorial-part2/) The safest way to fix this is by your changes since it doesn’t re-write the commit history. Solution: reverting git checkouthotfix git revert HEAD~1 You have successfully undone committed changes! Everything that was changed in the old commit will be reverted with this new commit. Git forces you to commit or any changes in the working directory that will be lost during the checkout. Result: stash You can think of _git revert_ as a tool for undoing committed changes, while _git reset HEAD_ is for undoing uncommitted changes. Undo Local Changes You started working on a feature, but you didn’t like the end result. These changes haven’t been with anyone else. Scenario: shared You want to in that files to the previous state, just the way it looked in the last . Solution: undo everything commit git checkout file_name.rb File has been to a state previously known to Git. Note that this removes all of the subsequent changes to the file! Result: file_name.rb reverted You can use _git checkout branch_name_ to switch between branches. Git forces you to commit or stash any changes in the working directory that will be lost during the checkout operation. Undo Private Changes You’ve made some commits locally in the but everything is terrible! You want to remove the last two commits from the current branch. Scenario: hotfix branch the hotfix branch backward by two commits as if those commits never happened. Solution: Reset git checkout hotfixgit reset HEAD~2 Our git repository has been all the way back to the specified commit. Those left out commits are now and will be removed the next time Git performs a . For now, then their contents are still on disk. Result: rewinded orphaned garbage collection You can tell Git what to do with your (set of files that will become the next commit) and when performing by using one of the parameters: index working directory [git reset](https://kolosek.com/git-commands-tutorial-part2/) : Tells Git to reset HEAD to another commit, so index and the working directory will in any way. All of the files changed between the original HEAD and the commit will be staged. --soft not be altered : Just like the soft, this will reset HEAD to another commit. It will also reset the to match it while working directory will not be touched. All the will stay in the working directory and appear as modified, but not staged. --mixed index changes The main difference between _--mixed_ and _--soft_ is whether or not your index is also modified. Check more on git-reset-guide . : This resets everything - it resets HEAD back to another commit, resets the index to match it, and resets the working directory to match it as well. --hard Tips and Tricks I will cover two additional things that can come in handy during your Git adventures. Fix the Previous Commit Message Everyone makes typo mistakes when writing commits and this is completely fine! It can be easily fixed before you do a . Scenario: git push Just run or . This will update and replace the most recent commit with a new commit. Solution: git commit --amend git commit --amend -m 'The new message' Redo After Undo You have done a for some unwanted changes, but then you realized that you actually needed them. Scenario: git reset --hard comes to your rescue! It is an amazing command for recovering project history and it can recover anything. Solution: git reflog almost Hope this three tools help you whenever you need to undo your recent changes. Originally published at kolosek.com on May 21, 2018.