Fix a local Bitbucket Cloud repository commit history with duplicated or divergent commits
Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.
Summary
The purpose of this article is to address certain situations where your local repository has a messed-up commit history (ie duplicated commits, divergent history) yet the remote repository is intact with no issues
The outlined process will help you either start again from scratch or, if that's not possible, identify the list of local branches, remove them, and then sync them with their respective remote branches. This involves fetching the latest copy from the remote and resetting the local head to the latest remote head.
Environment
Bitbucket Cloud repository
Diagnosis
Execute the git log command on your local repository within branches and compare this to the commit history within your remote repository, if you notice that these don't match - this is a good indicator that your local repository history will need to be repaired before you may continue working
Similarly, if you are having issues pushing to your remote repository, but no issues pulling from your remote - this is another indicator that there is a problem with your local commit history
Cause
Typically, rewriting repository history through force pushing or rebasing has the potential to cause issues with a repository commit history if this is not performed properly - it is always recommended to backup your repository locally before attempting such operations
Solution
There are two ways to address this problem, the first is simple however may not be suitable for all users - particularly when there are unpushed changes that would be removed when the deletion is performed.
Solution 1: Delete Local and Clone Remote
You can repair your local repository commit history by simply deleting your local repository, then performing a git clone from the remote repository
This will give you a fresh copy of the remote repository and will allow you to continue working
Solution 2: Delete Local and Repair From Remote
You will need to first identify your local branches, remove these local branches - then sync your local branches with their respective remote branches by fetching the latest copy from the remote and resetting the local head to the latest remote commit
You will need to perform the following steps in a GIT terminal on each local branch to delete these local branches, sync these with the remote branches, and then reset the local head to the current remote commit
NOTE: Some_other_branch refers to any other branch than the one you are currently fixing, this is because you can't delete a branch while you're already inside that branch
Perform git branch command to get a list of your local branches in your local repository and copy this list somewhere, you will need to run the commands below for each branch on that list
Now that you have a list of the branches, you can begin fixing their commit history (commands are case sensitive):
1 2 3 4 5 6 7 8 9
git checkout some_other_branch git branch -D branch1 git checkout -b branch1 origin/branch1 git pull origin branch1 git reset --hard origin/branch1
Verify that the local commit history matches your remote commit history before proceeding:
1
git log -5
Repeat the above steps for all of the local branches and then try to push to the repo once you have finished all of these, you should see the following output which means that the local and remote branches are synced properly:
1 2 3
git push --force > Everything up-to-date
Was this helpful?