简体   繁体   English

Gerrit-当前分支和远程分支已经分开

[英]Gerrit - Current branch and remote branch have diverged

Firstly, I push my commit to GERRIT for review 首先,我将对GERRIT的承诺进行审查

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 第三,我与命令git fetch同步,当我检查状态时发现我当前的分支和远程分支已经分开

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. 在分支客户端上,您的分支和'origin / remote_branch'已经分开,分别具有1和1个不同的提交。

(use "git pull" to merge the remote branch into yours) (使用“ git pull”将远程分支合并到您的分支中)

Root Cause 根本原因

With the info we have it's impossible to know but, probably, your commit was automerged or autorebased by Gerrit. 有了我们掌握的信息,这是不可能的,但是很可能您的提交已由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: 要了解项目正在使用哪种提交策略,请执行以下操作:

  1. Click on Projects > List 单击项目 > 列表
  2. Find the project 查找项目
  3. Click on project name 点击项目名称
  4. Click on tab General 单击选项卡常规
  5. See the Submit Type field 请参阅提交类型字段

How to avoid issues with this 如何避免这个问题

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. 在将提交合并到远程分支(在Gerrit中提交)之后,忘记本地分支(删除它们),并始终启动一个新分支以进行新更改。

To work on change 1 致力于变革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: 如果您想进行变更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: 当变更1在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. 如果您的分支分歧,则有两个选择。

  1. 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. 这是git pull的默认行为,但这将导致创建合并提交,并使分支的历史变得混乱。

  2. Rebase your local branch onto the remote one ( git rebase origin/the-branch ). 将本地分支重新建立到远程分支( 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 git pull is equivalent to git fetch + git merge . git pull等效于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 fetch ,因此您可以手动调用git merge (假设当前分支为master ):

git merge origin/master

Or you can forcibly move the branch: 或者,您可以强制移动分支:

git reset --hard origin/master

Or you can just run pull : 或者,您可以运行pull

git pull origin master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM