Can I recover a branch after its deletion in Git? Can I recover a branch after its deletion in Git? git git

Can I recover a branch after its deletion in Git?


Yes, you should be able to do git reflog --no-abbrev and find the SHA1 for the commit at the tip of your deleted branch, then just git checkout [sha]. And once you're at that commit, you can just git checkout -b [branchname] to recreate the branch from there.


Credit to @Cascabel for this condensed/one-liner version and @Snowcrash for how to obtain the sha.

If you've just deleted the branch you'll see something like this in your terminal Deleted branch <your-branch> (was <sha>). Then just use that <sha> in this one-liner:

git checkout -b <your-branch> <sha>


Most of the time unreachable commits are in the reflog. So, the first thing to try is to look at the reflog using the command git reflog (which display the reflog for HEAD).

Perhaps something easier if the commit was part of a specific branch still existing is to use the command git reflog name-of-my-branch. It works also with a remote, for example if you forced push (additional advice: always prefer git push --force-with-lease instead that better prevent mistakes and is more recoverable).


If your commits are not in your reflog (perhaps because deleted by a 3rd party tool that don't write in the reflog), I successfully recovered a branch by reseting my branch to the sha of the commit found using a command like that (it creates a file with all the dangling commits):

git fsck --full --no-reflogs --unreachable --lost-found | grep commit | cut -d\  -f3 | xargs -n 1 git log -n 1 --pretty=oneline > .git/lost-found.txt

If you should use it more than one time (or want to save it somewhere), you could also create an alias with that command...

git config --global alias.rescue '!git fsck --full --no-reflogs --unreachable --lost-found | grep commit | cut -d\  -f3 | xargs -n 1 git log -n 1 --pretty=oneline > .git/lost-found.txt'

and use it with git rescue

To investigate found commits, you could display each commit using some commands to look into them.

To display the commit metadata (author, creation date and commit message):

git cat-file -p 48540dfa438ad8e442b18e57a5a255c0ecad0560

To see also the diffs:

git log -p 48540dfa438ad8e442b18e57a5a255c0ecad0560

Once you found your commit, then create a branch on this commit with:

git branch commit_rescued 48540dfa438ad8e442b18e57a5a255c0ecad0560

For the ones that are under Windows and likes GUIs, you could easily recover commits (and also uncommited staged files) with GitExtensions by using the feature Repository => Git maintenance => Recover lost objects...


A similar command to easily recover staged files deleted: https://stackoverflow.com/a/58853981/717372


If you like to use a GUI, you can perform the entire operation with gitk.

gitk --reflog

This will allow you to see the branch's commit history as if the branch hadn't been deleted. Now simply right click on the most recent commit to the branch and select the menu option Create new branch.