Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Documendous
Documendous

Posted on

     

How to Get Back a Lost Git Branch

Losing a feature branch in Git can be frustrating, but there’s usually a way to recover it. Whether it was deleted, force-pushed, or just seems to have disappeared, here’s how to bring it back.


1. Check Git Reflog

Git keeps track of changes to your branches withgit reflog. This can help find lost commits:

 git reflog
Enter fullscreen modeExit fullscreen mode

Look through the list to find the commit hash where your work was last intact.

Once you’ve found the right commit, you can restore it.


2. Restore Your Work

Option 1: Create a new branch from the lost commit

Use the commit hash fromreflog to make a new branch:

 git checkout-b recovery-branch <commit-hash>
Enter fullscreen modeExit fullscreen mode

Now your work is back in a safe place.

Option 2: Reset your branch

If your feature branch still exists but is missing changes, you can reset it:

 git reset--hard <commit-hash>
Enter fullscreen modeExit fullscreen mode

Be careful: This will erase any changes since that commit.


3. Check the Remote Repository

If the branch was changed or deleted on the remote, check for lost commits:

 git fsck--lost-found
Enter fullscreen modeExit fullscreen mode

If this shows lost commits, you can inspect them:

 git show <commit-hash>
Enter fullscreen modeExit fullscreen mode

If the branch was force-pushed, you might still find its history:

 git reflog origin/feature-branch
Enter fullscreen modeExit fullscreen mode

4. Merge or Push Your Changes Back

Once you’ve recovered your work, you can merge it back:

 git checkout feature-branch git merge recovery-branch
Enter fullscreen modeExit fullscreen mode

Or, if the branch was deleted:

 git branch feature-branch recovery-branch git checkout feature-branch
Enter fullscreen modeExit fullscreen mode

Then push it:

 git push origin feature-branch
Enter fullscreen modeExit fullscreen mode

Conclusion

Git keeps track of more than you might think, so there’s usually a way to recover lost work. The key is to act quickly and checkreflog before assuming your work is gone.

Have you ever lost a branch and brought it back? Let me know how you did it!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Documendous, the creator of DocrepoX.DocrepoX is OSS (LGPL-v3) Enterprise Content Management (ECM) and Digital Asset Management (DAM) platform for storing, organising and retrieving digital assets.
  • Location
    Colorado USA
  • Work
    Documendous
  • Joined

More fromDocumendous

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp