When i use SourceTree ( a git client tool ) to manage my source code, i meet a use case which there are both code need to push and pull. One local repository committed code need to be pushed to remote repository ( bitbucket ), two remote code need to be pulled down to local repository as below picture.
But when i click the Push button, there are error messages : 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.
So i follow the instruction in the error message, execute git pull action by click 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.
Now push or pull action all can not work on current code, they locked each other, what should i do.
As you can see the red line in below 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 remote repository ( bitbucket sever ) branch, there are two blue point which has been committed and pushed to bitbucket server by others.
So before continue i should merge the code between local and remote repository.
1. How To Merge The Code Between Local And Remote Repository?
Finally i find follow 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 OK button in the popup Confirm Backout dialog to reverse the selected changes.
- After above action, you 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 to rebase your local code branch.
- Click OK button in the confirm rebase dialog.
- Below is the code after rebase action.
- Now you can click Pull button to get server changes to your local repository and merge, then click Push button to push you local repository changes to bitbucket server.