Firstly, I push my commit to GERRIT for review
Secondly, my teammate verified my commit and submit to repo
Thirdly, I sync with command git fetch,and I found my current branch and remote branch have diverged when I check status
I want to know how does it happened and how to avoid it, can somebody help me
All command I have ran
git add app/Http/Controllers/StatisController.php
git commit
git push origin HEAD:refs/for/<remote_branch>
After the commit submitted
git fetch
git status
And I got the message as following:
On branch client Your branch and 'origin/remote_branch' have diverged, and have 1 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
With the info we have it's impossible to know but, probably, your commit was automerged or autorebased by Gerrit. Changes are automerged/autorebased depending of the submit strategy configured in each project. To know which submit strategy a project is using do the following:
Forget your local branches (remove them) after your commits are merged in the remote branch (submitted on Gerrit) and always start a new branch to work on new changes.
To work on change 1
git fetch
git checkout -b LOCAL-1 /origin/REMOTE
# Work on the change
git add .
git commit
git push origin HEAD:refs/for/REMOTE
If you want to work on change 2:
git fetch
git checkout -b LOCAL-2 /origin/REMOTE
# Work on the change
git add .
git commit
git push origin HEAD:refs/for/REMOTE
If you need to rework the change 1:
git checkout LOCAL-1
# Rework the change
git add .
git commit --amend
git push origin HEAD:refs/for/REMOTE
When change 1 is merged (submitted) on Gerrit:
git branch -D LOCAL-1
In resume: your local branches should not follow the remote branches.
You have two options if your branch has diverged.
Merge the remote branch into your local one. This is the default behaviour of git pull
but this will result creating a merge commit and clutters the history of your branch.
Rebase your local branch onto the remote one ( git rebase origin/the-branch
). This is my prefered option as it creates a clean easily readable commits history.
The command to sync the current branch is git pull
. git pull
is equivalent to git fetch
+ git merge
. You've already ran git fetch
, so you can call git merge
manually (let's say the current branch is master
):
git merge origin/master
Or you can forcibly move the branch:
git reset --hard origin/master
Or you can just run pull
:
git pull origin master
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.