Can I Recover a Branch after Its Deletion in Git?
Yes, you can recover a branch after it has been deleted in Git. Git provides several mechanisms to restore branches even after they have been deleted locally or remotely, assuming the commits are still reachable in the repository's history. Here’s how you can recover a deleted branch:
Recovering a Deleted Local Branch
If you deleted a local branch and want to recover it, follow these steps:
Check Reflog: Use
git reflog
to find the commit where your branch was last pointing to before deletion. This command lists all recent actions and their associated commit hashes.git reflog
Look for the entry where your branch was deleted. It will typically look like
HEAD@{1}: branch: Deleted branch 'your-branch-name'
.Create a New Branch: Once you identify the commit where the branch was before deletion, create a new branch at that commit:
git checkout -b <branch-name> <commit-hash>
- `<branch-name>`: Replace with the name of the branch you want to restore.
- `<commit-hash>`: Replace with the commit hash where the branch was before deletion, found from the `git reflog`.
For example, if the reflog shows the branch was deleted at `HEAD@{1}`, you can recreate it as follows:
```bash
git checkout -b your-branch-name HEAD@{1}
```
Verify and Push (if necessary): Verify that you have restored the branch and check it out to work on it:
git checkout your-branch-name
If you need to push the recovered branch to a remote repository:
git push -u origin your-branch-name
Replace
origin
with your remote repository name.
Recovering a Deleted Remote Branch
If a branch was deleted from a remote repository (like GitHub, GitLab, etc.), and you want to recover it, follow these steps:
- Check Remote Reflog (if available): Some Git hosting services maintain reflogs for a period. You can check if the deleted branch is still in the reflog of your remote repository.
Recover Using Local Reflog: If remote reflog is not available or doesn't have the information, you can recover it using your local reflog:
git checkout -b recovered-branch origin/deleted-branch
- `recovered-branch`: Replace with the name you want to give to the recovered branch.
- `deleted-branch`: Replace with the name of the deleted branch on the remote repository.
This command creates a new local branch (`recovered-branch`) that tracks the deleted branch (`deleted-branch`) on the remote (`origin`).
Push Recovered Branch: If you want to restore the branch to the remote repository:
git push -u origin recovered-branch
This command pushes the recovered branch (
recovered-branch
) to the remote repository (origin
).
Notes:
- Garbage Collection: If the branch was deleted a long time ago and Git's garbage collection has occurred on the remote repository, it may not be possible to recover it. Ensure you act promptly to recover deleted branches.
- Collaboration: Communicate with your team if you're recovering a branch from a shared repository to avoid conflicts or confusion.
By following these steps, you can recover a branch in Git, whether it was deleted locally or remotely, as long as the necessary commits are still reachable in the repository's history.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for us![](/assets/articles/authors/marin-0d19fe482115fe2b15c8429a7ee4eb88d803984eaa7c1d8c7309241049f3d402.png)
Build on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github