本文翻译自:Your branch is ahead of 'origin/master' by 3 commits

I am getting the following when running git status 运行git status时我得到以下内容

Your branch is ahead of 'origin/master' by 3 commits.

I have read on some other post the way to fix this is run git pull --rebase but what exactly is rebase, will I lose data or is this simple way to sync with master? 我已经阅读了其他一些文章修复此问题的方法是运行git pull --rebase但究竟什么是rebase,我会丢失数据还是这种与master同步的简单方法?


#1楼

参考:https://stackoom.com/question/16liw/你的分支在-次提交之前领先于-origin-master


#2楼

There is nothing to fix. 没有什么可以解决的。 You simply have made 3 commits and haven't moved them to the remote branch yet. 您只是进行了3次提交,但尚未将它们移动到远程分支。 There are several options, depending on what you want to do: 根据您的要求,有几种选择:

  • git push : move your changes to the remote (this might get rejected if there are already other changes on the remote) git push :将您的更改移动到远程(如果遥控器上已有其他更改,则可能会被拒绝)
  • do nothing and keep coding, sync another day 什么都不做,继续编码,同步另一天
  • git pull : get the changes (if any) from the remote and merge them into your changes git pull :从远程获取更改(如果有)并将它们合并到您的更改中
  • git pull --rebase : as above, but try to redo your commits on top of the remote changes git pull --rebase :如上所述,但尝试在远程更改之上重做您的提交

You are in a classical situation (although usually you wouldn't commit a lot on master in most workflows). 你处于一个经典的情况(虽然通常你不会在大多数工作流程中对主人做很多贡献)。 Here is what I would normally do: Review my changes. 以下是我通常会做的事情:查看我的更改。 Maybe do a git rebase --interactive to do some cosmetics on them, drop the ones that suck, reorder them to make them more logical. 也许做一个git rebase --interactive来对它们做一些化妆品,丢掉那些吮吸它们,重新排序它们以使它们更合乎逻辑。 Now move them to the remote with git push . 现在用git push将它们移动到遥控器。 If this gets rejected because my local branch is not up to date: git pull --rebase to redo my work on top of the most recent changes and git push again. 如果这被拒绝,因为我的本地分支不是最新的: git pull --rebase重做我的工作在最近的更改和git push再次。


#3楼

This message from git means that you have made three commits in your local repo, and have not published them to the master repository. 来自git这条消息意味着您已在本地仓库中进行了三次提交,并且尚未将它们发布到master存储库。 The command to run for that is git push {local branch name} {remote branch name} . 为此运行的命令是git push {local branch name} {remote branch name}

The command git pull (and git pull --rebase ) are for the other situation when there are commit on the remote repo that you don't have in your local repo. 命令git pull (和git pull --rebase )是针对另一种情况,当你在本地git pull --rebase中没有提交远程git pull --rebase时。 The --rebase option means that git will move your local commit aside, synchronise with the remote repo, and then try to apply your three commit from the new state. --rebase选项意味着git会将您的本地提交移到一边,与远程--rebase同步,然后尝试从新状态应用您的三个提交。 It may fail if there is conflict, but then you'll be prompted to resolve them. 如果存在冲突,它可能会失败,但随后会提示您解决它们。 You can also abort the rebase if you don't know how to resolve the conflicts by using git rebase --abort and you'll get back to the state before running git pull --rebase . 如果你不知道如何通过使用git rebase --abort解决冲突,你也可以中止rebase ,你将在运行git pull --rebase之前回到状态。


#4楼

You get that message because you made changes in your local master and you didn't push them to remote. 您收到该消息是因为您在本地主服务器中进行了更改,而您没有将它们推送到远程服务器。 You have several ways to "solve" it and it normally depends on how your workflow looks like: 您有几种方法可以“解决”它,通常取决于您的工作流程如何:

  • In a good workflow your remote copy of master should be the good one while your local copy of master is just a copy of the one in remote. 在良好的工作流程中,您的远程主副本应该是好的,而您的本地主副本只是远程副本的副本。 Using this workflow you'll never get this message again. 使用此工作流程,您将永远不会再次收到此消息。
  • If you work in another way and your local changes should be pushed then just git push origin assuming origin is your remote 如果你以另一种方式工作并且应该推送你的本地更改,那么只需git push origin假设origin是你的远程
  • If your local changes are bad then just remove them or reset your local master to the state on remote git reset --hard origin/master 如果您的本地更改不好,则只需删除它们或将本地主服务器重置为远程git reset --hard origin/master

#5楼

Use these 4 simple commands 使用这4个简单的命令

Step 1 : git checkout <branch_name> 第1步git checkout <branch_name>

This is obvious to go into that branch. 进入那个分支是显而易见的。

Step 2 : git pull -s recursive -X theirs 第2步git pull -s recursive -X theirs

Take remote branch changes and replace with their changes if conflict arise. 如果发生冲突,请进行远程分支更改并替换其更改。 Here if you do git status you will get something like this your branch is ahead of 'origin/master' by 3 commits. 在这里,如果您执行git status您将得到类似这样的内容, 您的分支在3次提交之前超过“origin / master”。

Step 3 : git reset --hard origin/<branch_name> 第3步git reset --hard origin/<branch_name>

Step 4 : git fetch 第4步git fetch

Hard reset your branch. 硬重置您的分支。

Enjoy. 请享用。


#6楼

Came across this issue after I merged a pull request on Bitbucket. 我在Bitbucket上合并了一个pull请求后遇到了这个问题。

Had to do 不得不做

git fetch

and that was it. 就是这样。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐