When I use SourceTree ( a git client tool ) to manage my source code, I meet a use case that there are both source codes that need to push and pull. One local repository committed code need to be pushed to the remote repository ( Bitbucket ), two remote committed code need to be pulled down to local repository as below picture.
But when I click the Push button to push my commits, there are error messages in the Pushing To origin popup dialog window, the error message is: Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes (e.g. ‘git pull …’) before pushing again. See the ‘Note about fast-forwards’ in ‘git push –help’ for details.
So I follow the instruction in the error message, execute the git pull action by clicking the Pull button. But this time I meet another error message: You have not concluded your merge (MERGE_HEAD exists). Please, commit your changes before merging. Exiting because of unfinished merge.
Now push or pull action all can not work on the current source code commits, they locked each other, what should I do? As you can see the red line in the above picture is my local repository branch, there has one red point which has been committed but not pushed to bitbucket ( remote repository ). The blue line is the remote repository ( bitbucket server ) branch, there are two blue points which has been committed and pushed to the bitbucket server by others. So before continue I should merge the code between local and remote repositories.
1. How To Merge The Code Between Local And Remote Repository?
Finally, I find the following steps to merge local and remote repository code.
- Right-click the conflict committed changes, then click Reset master to this commit menu item in the popup menu list.
- Click the OK button in the popup Confirm Backout dialog to reverse the selected changes.
- After the above action, your local repository commits will separate from remote repository commits.
- Now you can right-click your last local committed but not pushed code changes, then click Rebase menu item in the popup menu list to rebase your local code branch.
- Click the OK button in the Confirm Rebase dialog.
- Below is the code after the rebase action.
- Now you can click the Pull button to get server changes to your local repository and merge, then click the Push button to push your local repository changes to the bitbucket server.