Bitbucket: changes missing from a branch
Atlassian's knowledge base describes how a commit can vanish from a Bitbucket Cloud branch: one person pushes commit X; another, whose clone has not fetched X, commits on the older version and pushes with force. The branch now ends in the second person's commit, and X is no longer part of it (How previously introduced changes can go missing from a branch). Without force, Git would have refused that push with ! [rejected] ... (fetch first).
Confirm it
- Open the file as it was at a commit:
https://bitbucket.org/<workspace>/<repository>/src/<commit>/<path>(the knowledge base's check). If the lines are there at X and not at the branch's last commit, they were dropped after X. git log -S'the missing line' -- path/to/filelists the commits that added or removed that text.- A clone that fetches after a force push prints
(forced update)next to the branch.
To start from what is on Bitbucket: Pull Request Inbox for Bitbucket, a plugin for JetBrains IDEs, fetches before it checks out a pull request's branch, and merges the destination branch in the IDE when there are conflicts, with no force push.
Get the commit back
The commit is usually still in a clone:
- The person who made X usually still has it:
git logorgit reflogin their clone. - Any clone that fetched X before the force push has it in the remote-tracking branch's reflog:
git reflog show origin/feature/one.
Put it back on top of the branch as it is now, with a normal push:
git fetch git checkout -b restore-x origin/feature/one git cherry-pick <commit X> git push origin restore-x:feature/one
Or push restore-x as its own branch and open a pull request for it. If no clone has X, the ways to find a hash in restoring a deleted branch can help.
Stop it happening
- Branch restrictions: the knowledge base suggests restricting force pushes. In Repository settings, Branch restrictions, leave Allow rewriting branch history off for shared branches; a force push is then refused ("pre-receive hook declined").
--force-with-lease: when you must force push,git push --force-with-leaserefuses if the branch on Bitbucket has moved since your clone last fetched it. A tool that fetches in the background can defeat that check; Git 2.30 or later adds--force-if-includesfor it.- Fetch first, and work on your own branch: Atlassian suggests
git pull --allbefore new changes, and separate branches merged through reviewed pull requests over everyone on one branch.
Bitbucket pull requests in IntelliJ IDEA
Pull Request Inbox for Bitbucket, a plugin by Lesstep for JetBrains IDEs 2024.1 or later, lists the pull requests that wait for your review in an IDE tool window. Its Checkout button fetches from Bitbucket before it switches to the pull request's branch, and a branch new to your clone starts from Bitbucket's version. When a pull request has conflicts, Resolve in the IDE fetches, checks out the source branch and merges the destination branch into it, so you resolve them in the IDE's merge dialog and push a merge commit, not a rewritten history. Both are free, as are the review inbox, diffs, discussions and merge checks; creating, approving, commenting and merging are Pro, with a 30-day trial. It also works with Bitbucket Data Center (beta).
Get it on JetBrains Marketplace
Bitbucket and Atlassian are trademarks of Atlassian; IntelliJ IDEA is a trademark of JetBrains s.r.o. This page is not affiliated with or endorsed by Atlassian or JetBrains.
More: Restore a deleted branch · A pull request merged by itself · "pre-receive hook declined" · Merge conflicts · 日本語