You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何合并Git本地与Bitbucket远程分支并找回丢失文件

Fixing Lost Files After a Botched Bitbucket Branch Merge

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 like merge branch 'feature/your-branch'). Jot down the 7-character commit hash next to the HEAD@{n} entry that marks the state before the merge.
  • Run git log --oneline --graph to 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:
    git reset --hard <pre-merge-commit-hash>
    
    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.
  • Option 2: Restore specific missing files
    If you want to keep some merge fixes but recover deleted files, target individual files with:
    git restore --source=<pre-merge-commit-hash> -- path/to/missing/file1 path/to/missing/file2
    
    This pulls the specific files from the pre-merge commit and restores them to your working directory.

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 1 to tell Git to revert to the parent branch (your main branch before the merge):
    git revert -m 1 <merge-commit-hash>
    
    This creates a new commit that undoes all changes from the bad merge—including restoring deleted files—without erasing any existing history.
  • Push this revert commit to Bitbucket:
    git push origin <your-branch-name>
    
    If you need to redo the merge later, check out your feature branch again, fix conflicts carefully, and merge it properly.

Pro Tips to Avoid This Next Time

  • Always run git status before merging to make sure you don’t have uncommitted changes that could get lost in the process.
  • Use git merge --no-commit first—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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:47:46