如何合并Git本地与Bitbucket远程分支并找回丢失文件
Hey there, I know losing files mid-development is super stressful—let’s get your work back. We’ll start by digging into Git’s hidden history (it tracks way more actions than you might realize) and recover those missing files based on whether you’ve already pushed the bad merge to Bitbucket or not.
First, Diagnose the Damage
Before jumping into fixes, let’s pinpoint exactly where things went wrong:
- Open your terminal and run
git reflog—this shows every single action you’ve taken in Git, including merges, resets, and even commits that don’t show up in your standard log. Look for the entry right before your failed merge (it’ll look likemerge branch 'feature/your-branch'). Jot down the 7-character commit hash next to theHEAD@{n}entry that marks the state before the merge. - Run
git log --oneline --graphto visualize the problematic merge commit. Note its hash too, in case we need it later.
Recovering Your Files
Scenario 1: You haven’t pushed the messed-up merge to Bitbucket yet
This is the simpler case—Git hasn’t synced the bad state to the remote, so we can safely roll back without disrupting anyone else:
- Option 1: Full rollback to pre-merge state
If you don’t want to keep any of the merge changes, run:
This resets your local branch to exactly how it was before the merge. Warning: This will discard any uncommitted changes, so save anything important first.git reset --hard <pre-merge-commit-hash> - Option 2: Restore specific missing files
If you want to keep some merge fixes but recover deleted files, target individual files with:
This pulls the specific files from the pre-merge commit and restores them to your working directory.git restore --source=<pre-merge-commit-hash> -- path/to/missing/file1 path/to/missing/file2
Scenario 2: You already pushed the bad merge to Bitbucket
This is trickier because the bad state is now public. We avoid rewriting shared Git history (it causes chaos for collaborators), so we’ll use revert instead of reset:
- Grab the hash of the bad merge commit from
git log --oneline. - Run the revert command, using
-m 1to tell Git to revert to the parent branch (your main branch before the merge):
This creates a new commit that undoes all changes from the bad merge—including restoring deleted files—without erasing any existing history.git revert -m 1 <merge-commit-hash> - Push this revert commit to Bitbucket:
If you need to redo the merge later, check out your feature branch again, fix conflicts carefully, and merge it properly.git push origin <your-branch-name>
Pro Tips to Avoid This Next Time
- Always run
git statusbefore merging to make sure you don’t have uncommitted changes that could get lost in the process. - Use
git merge --no-commitfirst—this lets you review all merge changes before committing, so you can spot accidental file deletions or weird conflicts early. - If you’re on a team, never force-push to shared branches unless everyone agrees—it rewrites history and breaks your teammates’ local repos.
内容的提问来源于stack exchange,提问作者Sethuraman Srinivasan

