How to Recover a Deleted Branch with Git Reflog
Jul 25, 2025 am 12:46 AMRun git reflog --date=local to view recent reference changes and locate the deleted branch by its name or commit hash. 2. Identify the commit hash just before the "Deleted branch" message in the reflog output. 3. Recreate the branch using git checkout -b branch-name commit-hash, or use git checkout -B to force recreation if the branch already exists. 4. Optionally, verify the branch’s history via git reflog show branch-name before restoring. 5. After recovery, push the branch to the remote with git push origin branch-name to safeguard it. Recovery is only possible locally and works best when done quickly, as reflog entries expire after about 90 days.
If you’ve accidentally deleted a Git branch—especially one with unmerged changes—it can feel like a disaster. But if you act quickly, git reflog
gives you a powerful way to recover it. Here’s how.

What Is Git Reflog?
git reflog
(short for "reference log") tracks changes to the tips of branches and other references in your local repository. It records when branches are updated, checked out, or deleted—even if those changes aren’t part of the commit history. This makes it a lifesaver for recovering lost work.
The reflog is local only—it doesn’t exist on remote repositories—so this method only works if the branch was deleted from your own machine.

Step 1: Run Git Reflog to Find the Deleted Branch
Start by listing the recent reference changes:
git reflog
Or, to make it easier to read, use:

git reflog --date=local
Look through the output for any entry related to the deleted branch. You’ll see lines like:
abc1234 (HEAD -> main) HEAD@{0}: checkout: moving from feature/login to main def5678 (feature/login) HEAD@{1}: commit: Add login validation ghi9012 HEAD@{2}: branch: Deleted branch feature/login
Even though the branch was deleted, the commits are still referenced in the log.
Alternatively, if you remember the branch name, you can search directly:
git reflog | grep feature/login
Step 2: Recreate the Branch from the Last Commit
Once you’ve found the commit hash (like def5678
) where the branch was last active, recreate the branch pointing to that commit:
git checkout -b feature/login def5678
Now your branch is restored with all its previous commits.
? If you get an error that the branch already exists (maybe you tried recovery before), use
-B
instead of-b
to force recreate:git checkout -b feature/login def5678
Alternative: Use Reflog with Branch Reference
If the branch existed recently, Git keeps a reference like refs/heads/feature/login
in the reflog, even after deletion. You can check its history directly:
git reflog show feature/login
This shows only the activity on that specific branch. Then recover it the same way:
git branch feature/login def5678
No need to switch branches immediately—just recreate it first.
A Few Important Notes
Reflog entries expire: Git automatically prunes old reflog entries (usually after 90 days). So act fast.
Only local recovery: Reflog doesn’t sync with remotes. If the branch was never pushed, and you’re on a new machine, it’s likely gone.
Push the recovered branch: Once restored, push it to remote to back it up:
git push origin feature/login
Prevention tip: Get in the habit of pushing feature branches early, even if unfinished.
Recovering a deleted branch with git reflog
is straightforward once you know where to look. It’s not magic—just Git quietly keeping track of your every move. As long as the reflog still has a reference to the branch’s last commit, you’re probably fine.
Basically: run git reflog
, find the commit, and reattach the branch. Not glamorous, but it saves hours of work.
The above is the detailed content of How to Recover a Deleted Branch with Git Reflog. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To view Git commit history, use the gitlog command. 1. The basic usage is gitlog, which can display the submission hash, author, date and submission information; 2. Use gitlog--oneline to obtain a concise view; 3. Filter by author or submission information through --author and --grep; 4. Add -p to view code changes, --stat to view change statistics; 5. Use --graph and --all to view branch history, or use visualization tools such as GitKraken and VSCode.

To delete a Git branch, first make sure it has been merged or no retention is required. Use gitbranch-d to delete the local merged branch. If you need to force delete unmerged branches, use the -D parameter. Remote branch deletion uses the gitpushorigin-deletebranch-name command, and can synchronize other people's local repositories through gitfetch-prune. 1. To delete the local branch, you need to confirm whether it has been merged; 2. To delete the remote branch, you need to use the --delete parameter; 3. After deletion, you should verify whether the branch is successfully removed; 4. Communicate with the team to avoid accidentally deleting shared branches; 5. Clean useless branches regularly to keep the warehouse clean.

The five most valuable stablecoins in 2025 are Tether (USDT), USD Coin (USDC), Dai (DAI), First Digital USD (FDUSD) and TrueUSD (TUSD).

The "Dogcoin" in the currency circle usually refers to newly issued cryptocurrencies with extremely low market value, opaque project information, weak technical foundation or even no practical application scenarios. These tokens often appear with high-risk narratives.

As an important cornerstone of the crypto world, stablecoins provide the market with value anchoring and hedging functions. This article lists the top ten stablecoin projects with current market value and influence: 1. Tether (USDT) has become a market leader with its extensive liquidity and trading depth; 2. USD Coin (USDC) is known for its compliance and transparency, and is the first choice for institutional investors; 3. Dai (DAI) is the core of decentralized stablecoin, generated by the MakerDAO protocol; 4. First Digital USD (FDUSD) has risen rapidly due to Binance support; 5. TrueUSD (TUSD) emphasizes transparency in third-party audits; 6. Frax (FRAX) adopts collateral

To identify fake altcoins, you need to start from six aspects. 1. Check and verify the background of the materials and project, including white papers, official websites, code open source addresses and team transparency; 2. Observe the online platform and give priority to mainstream exchanges; 3. Beware of high returns and people-pulling modes to avoid fund traps; 4. Analyze the contract code and token mechanism to check whether there are malicious functions; 5. Review community and media operations to identify false popularity; 6. Follow practical anti-fraud suggestions, such as not believing in recommendations or using professional wallets. The above steps can effectively avoid scams and protect asset security.

This article has selected several top Python "finished" project websites and high-level "blockbuster" learning resource portals for you. Whether you are looking for development inspiration, observing and learning master-level source code, or systematically improving your practical capabilities, these platforms are not to be missed and can help you grow into a Python master quickly.

As a pioneer in the digital world, Bitcoin’s unique code name and underlying technology have always been the focus of people’s attention. Its standard code is BTC, also known as XBT on certain platforms that meet international standards. From a technical point of view, Bitcoin is not a single code style, but a huge and sophisticated open source software project. Its core code is mainly written in C and incorporates cryptography, distributed systems and economics principles, so that anyone can view, review and contribute its code.
