Course Tonight

Course Tonight

Did You Know?

Advanced visual search system powered by Ajax

Chapter 18: Recovering

Estimated reading: 4 minutes 4 views

Section 18.1: Recovering from a reset With Git, you can (almost) always turn the clock back. Don’t be afraid to experiment with commands that rewrite history*. Git doesn’t delete your commits for 90 days by default, and during that time you can easily recover them from the reflog:

$ git reset @~3 # go back 3 commits
$ git reflog
c4f708b HEAD@{0}: reset: moving to @~3
2c52489 HEAD@{1}: commit: more changes
4a5246d HEAD@{2}: commit: make important changes
e8571e4 HEAD@{3}: commit: make some changes
... earlier commits ...
$ git reset 2c52489
... and you're back where you started
  • Watch out for options like –hard and –force though — they can discard data.
  • Also, avoid rewriting history on any branches you’re collaborating on.

Section 18.2: Recover from git stash To get your most recent stash after running git stash, use git stash apply.

To see a list of your stashes, use git stash list.

You will get a list that looks something like this:

stash@{0}: WIP on master: 67a4e01 Merge tests into develop
stash@{1}: WIP on master: 70f0d95 Add user role to localStorage on user login

Choose a different git stash to restore with the number that shows up for the stash you want:

git stash apply stash@{2}

You can also choose git stash pop, it works the same as git stash apply like:

git stash pop

or

git stash pop stash@{2}

Difference between git stash apply and git stash pop: git stash pop: stash data will be removed from the stack of stash list. For example:

git stash list

You will get a list that looks something like this:

stash@{0}: WIP on master: 67a4e01 Merge tests into develop
stash@{1}: WIP on master: 70f0d95 Add user role to localStorage on user login

Now, pop stash data using the command git stash pop.

Again, check for the stash list using git stash list. You will get a list that looks something like this:

stash@{0}: WIP on master: 70f0d95 Add user role to localStorage on user login

You can see one stash data is removed (popped) from the stash list, and stash@{1} became stash@{0}.

Section 18.3: Recovering from a lost commit In case you have reverted back to a past commit and lost a newer commit, you can recover the lost commit by running git reflog.

Then find your lost commit and reset back to it by doing:

git reset HEAD --hard <sha1-of-commit>

Section 18.4: Restore a deleted file after a commit In case you have accidentally committed a delete on a file and later realized that you need it back.

First, find the commit ID of the commit that deleted your file by using the command:

git log --diff-filter=D --summary

This will give you a sorted summary of commits that deleted files.

Then proceed to restore the file by running:

git checkout 81eeccf~1 <your-lost-file-name>

Replace 81eeccf with your own commit ID.

Section 18.5: Restore file to a previous version To restore a file to a previous version, you can use the reset command:

git reset <sha1-of-commit> <file-name>

If you have already made local changes to the file that you do not require, you can also use the --hard option.

Section 18.6: Recover a deleted branch To recover a deleted branch, you need to find the commit which was the head of your deleted branch by running:

git reflog

You can then recreate the branch by running:

git checkout -b <branch-name> <sha1-of-commit>

Note that you will not be able to recover deleted branches if Git’s garbage collector deleted dangling commits, which are those without refs. It is always advisable to have a backup of your repository, especially when you work in a small team or on a proprietary project.

Leave a Comment

Share this Doc

Chapter 18: Recovering

Or copy link

CONTENTS