How can I restore a previous version of my project?
Short answer
To inspect a previous version of your project without losing current work, run git checkout <commit-hash> to enter a detached HEAD state where all files reflect that older revision. If you want to start new work from that historical point, immediately create a branch with git checkout -b <new-branch-name>, which anchors the detached HEAD to a named branch. To reset your entire current branch to a previous commit and discard all later commits and changes, use git reset --hard <commit-hash> — this is irreversible, so double-check the hash with git log first. To restore only specific files from an earlier revision rather than the whole project, use git checkout <commit-hash> -- path/to/file, which overwrites only that file without altering the branch pointer. For safety, consider creating a new branch before any hard reset so you can return to the current state if needed.
Using a version control system like Git brings a fantastic benefit: you can return to any old version of your project at any time.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Free. We email you a ZIP with our cheat sheets.
Returning to an Old Revision
The fastest way to restore an old version is to use the "reset" command:
Careful: this command can discard work.
git reset --hard 0ad5a7a6
This will rewind your HEAD branch to the specified version. All commits that came after this version are effectively undone; your project is exactly as it was at that point in time.
The reset command comes with a couple of options, one of the more interesting ones being the "--soft" flag. If you use it instead of --hard, Git will keep all the changes in those "undone" commits as local modifications:
git reset --soft 0ad5a7a6
You'll be left with a couple of changes in your working copy and can then decide what to do with them.
In case you are using the Tower Git client, you can use the reset command right from a commit's contextual menu. And in case you made a mistake: simply hit CMD+Z to undo the reset and restore the removed commits!
Restoring a Revision in a New Local Branch
As said, using the reset command on your HEAD branch is a quite drastic action: it will remove any commits (on this branch) that came after the specified revision. If you're sure that this is what you want, everything is fine.
However, there is also a "safer" way in case you'd prefer leaving your current HEAD branch untouched. Since "branches" are so cheap and easy in Git, we can easily create a new branch which starts at that old revision:
git checkout -b old-project-state 0ad5a7a6
Normally, the checkout command is used to just switch branches. However, providing the -b parameter, you can also let it create a new branch (named "old-project-state" in this example). If you don't want it to start at the current HEAD revision, you also need to provide a commit hash - the old project revision we want to restore.
Voilà: you now have a new branch named "old-project-state" reflecting the old version of your project - without touching or even removing any other commits or branches.
In case you are using the Tower Git client, you can simply drag & drop the commit you want to return to and create a new branch:
Learn More
- Check out the chapter Undoing Things – Command Line edition in our free online book