Recovering
Recovering from a lost commit
Section titled “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 reflogThen find your lost commit, and reset back to it by doing
git reset HEAD --hard <sha1-of-commit>Restore a deleted file after a commit
Section titled “Restore a deleted file after a commit”In case you have accidentally commited 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.
git log --diff-filter=D --summaryWill give you a sorted summary of commits which deleted files.
Then proceed to restore the file by
git checkout 81eeccf~1 <your-lost-file-name>(Replace 81eeccf with your own commit id)
Restore file to a previous version
Section titled “Restore file to a previous version”To restore a file to a previous version you can use reset.
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
Recover a deleted branch
Section titled “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 reflogYou can then recreate the branch by running
git checkout -b <branch-name> <sha1-of-commit>You will not be able to recover deleted branches if git’s garbage collector deleted dangling commits - those without refs. Always have a backup of your repository, especially when you work in a small team / proprietary project
Recovering from a reset
Section titled “Recovering from a reset”With Git, you can (almost) always turn the clock back
Section titled “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 reflogc4f708b HEAD@{0}: reset: moving to @~32c52489 HEAD@{1}: commit: more changes4a5246d HEAD@{2}: commit: make important changese8571e4 HEAD@{3}: commit: make some changes... earlier commits ...$ git reset 2c52489... and you're back where you started- Watch out for options like
--hardand--forcethough — they can discard data. - Also, avoid rewriting history on any branches you’re collaborating on.
Recover from git stash
Section titled “Recover from git stash”To get your most recent stash after running git stash, use
git stash applyTo see a list of your stashes, use
git stash listYou will get a list that looks something like this
stash@{0}: WIP on master: 67a4e01 Merge tests into developstash@{1}: WIP on master: 70f0d95 Add user role to localStorage on user loginChoose 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 same as ‘git stash apply’ like..
git stash popor
git stash pop stash@{2}Difference in git stash apply and git stash pop…
git stash pop:- stash data will be remove from stack of stash list.
Ex:-
git stash listYou will get a list that looks something like this
stash@{0}: WIP on master: 67a4e01 Merge tests into developstash@{1}: WIP on master: 70f0d95 Add user role to localStorage on user loginNow pop stash data using command
git stash popAgain Check for stash list
git stash listYou will get a list that looks something like this
stash@{0}: WIP on master: 70f0d95 Add user role to localStorage on user loginYou can see one stash data is removed (popped) from stash list and stash@{1} became stash@{0}.