git:你的分支领先于 X 次提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2432579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:01:53  来源:igfitidea点击:

git: Your branch is ahead by X commits

gitgit-commit

提问by SeanJA

How does this actually come about?

这实际上是如何发生的?

I am working in one repo by myself at the moment, so this is my workflow:

我目前正在一个仓库中工作,所以这是我的工作流程:

  1. Change files
  2. Commit
  3. Repeat 1-2 until satisfied
  4. Push to master
  1. 更改文件
  2. 犯罪
  3. 重复1-2直到满意
  4. 推到主人

Then when I do a git statusit tells me that my branch is ahead by X commits(presumably the same number of commits that I have made). Is it because when you push the code it doesn't actually update your locally cached files (in the .git folders)? git pullseems to 'fix' this strange message, but I am still curious why it happens, maybe I am using git wrong?

然后,当我执行 a 时,git status它告诉我我的分支领先 X 个提交(大概与我所做的提交数量相同)。是因为当您推送代码时,它实际上并没有更新您本地缓存的文件(在 .git 文件夹中)?git pull似乎“修复”了这个奇怪的消息,但我仍然很好奇它为什么会发生,也许我使用 git 错了?



including what branch is printed in the message

包括消息中打印的分支

My local branch is ahead of master

我的本地分支领先于 master

where do you push/pull the current branch

你在哪里推/拉当前分支

I am pushing to GitHub and pulling to whichever computer I happen to be working on at that point in time, my local copy is always fully up to date as I am the only one working on it.

我正在推送到 GitHub 并拉到我当时碰巧正在使用的任何一台计算机上,我的本地副本始终是完全最新的,因为我是唯一一个使用它的人。

it doesn't actually check the remote repo

它实际上并不检查远程仓库

That is what I thought, I figured that I would make sure my understanding of it was correct.

这就是我的想法,我想我会确保我对它的理解是正确的。

are you passing some extra arguments to it?

你是否向它传递了一些额外的参数?

Not ones that I can see, maybe there is some funny config going on on my end?

不是我能看到的,也许我这边有一些有趣的配置?

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)

回答by Rich

If you get this message after doing a git pull remote branch, try following it up with a git fetch. (Optionally, run git fetch -pto prune deleted branches from the repo)

如果您在执行 a 后收到此消息git pull remote branch,请尝试使用git fetch. (可选地,运行git fetch -p以从 repo 中修剪已删除的分支)

Fetch seems to update the local representation of the remote branch, which doesn't necessarily happen when you do a git pull remote branch.

Fetch 似乎更新了远程分支的本地表示,这在您执行git pull remote branch.

回答by Marian Zburlea

Use

git pull --rebase

The --rebase option means that git will move your local commit aside, synchronise with the remote and then try to apply your commits from the new state.

--rebase 选项意味着 git 会将您的本地提交移到一边,与远程同步,然后尝试从新状态应用您的提交。

回答by Abhishek Goel

Use these 3 simple commands

使用这3个简单的命令

Step 1: git checkout <branch_name>

第 1 步git checkout <branch_name>

Step 2: git pull -s recursive -X theirs

第 2 步git pull -s recursive -X theirs

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

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

More details : https://stackoverflow.com/a/39698570/2439715

更多详情:https: //stackoverflow.com/a/39698570/2439715

Enjoy.

享受。

回答by Josh Lee

I think you're misreading the message — your branch isn't ahead of master, it ismaster. It's ahead of origin/master, which is a remote tracking branchthat records the status of the remote repository from your last push, pull, or fetch. It's telling you exactly what you did; you got ahead of the remote and it's reminding you to push.

我想你误读消息-你的分支是不是超前master,它master。它是领先的origin/master,这是一个远程跟踪分支,它记录从上次远程仓库的状态pushpullfetch。它准确地告诉你你做了什么;你领先于遥控器,它提醒你推动。

回答by Fake Code Monkey Rashid

Someone said you might be misreading your message, you aren't. This issue actually has to do with your <project>/.git/configfile. In it will be a section similar to this:

有人说你可能误读了你的信息,你没有。这个问题实际上与您的<project>/.git/config文件有关。它将是一个类似于此的部分:

[remote "origin"]
    url = <url>
    fetch = +refs/heads/*:refs/remotes/origin/*

If you remove the fetch line from your project's .git/config file you'll stop the "Your branch is ahead of 'origin/master' by Ncommits." annoyance from occurring.

如果您从项目的 .git/config 文件中删除 fetch 行,您将停止“您的分支通过N提交领先于 'origin/master'” 。烦恼的发生。

Or so I hope. :)

或者我希望如此。:)

回答by Anatolii Pazhyn

I had this issue on my stage server where I do only pulls. And hard reset helped me to clean HEAD to the same as remote.

我在我的舞台服务器上遇到了这个问题,我只做拉。硬重置帮助我将 HEAD 清理到与遥控器相同的位置。

git reset --hard origin/master

So now I have again:

所以现在我又有了:

On branch master
Your branch is up-to-date with 'origin/master'.

回答by Али Палитаев

This worked for me

这对我有用

git reset --hard origin/master

git reset --hard origin/master

The output must look like

输出必须看起来像

On branch dev HEAD is now at ae1xc41z Last commit message

On branch dev HEAD is now at ae1xc41z Last commit message

回答by AxCoder

In my case it was because I switched to master using

就我而言,这是因为我使用

 git checkout -B master

Just to pull the new version of it instead of

只是为了拉取它的新版本而不是

 git checkout master

The first command resets the head of masterto my latest commits

第一个命令将 master 的负责人重置为我的最新提交

I used

我用了

git reset --hard origin/master

To fix that

解决这个问题

回答by Ryan Erwin

I went through every solution on this page, and fortunately @anatolii-pazhyn commented because his solution was the one that worked. Unfortunately I don't have enough reputationto upvote him, but I recommend trying his solution first:

我浏览了此页面上的所有解决方案,幸运的是 @anatolii-pazhyn 发表了评论,因为他的解决方案是有效的。不幸的是,我没有足够的声誉来支持他,但我建议先尝试他的解决方案:

git reset --hard origin/master

Which gave me:

这给了我:

HEAD is now at 900000b Comment from my last git commit here

I also recommend:

我也推荐:

git rev-list origin..HEAD
# to see if the local repository is ahead, push needed

git rev-list HEAD..origin
# to see if the local repository is behind, pull needed

You can also use:

您还可以使用:

git rev-list --count --left-right origin/master...HEAD
# if you have numbers for both, then the two repositories have diverged

Best of luck

祝你好运

回答by The Code Father

I had this same problem on a Windows machine. When I ran a git pull origin mastercommand, I would get the "ahead of 'origin/master' by X commits" warning. I found that if I instead ran git pull originand did NOT specify the branch, then I would no longer receive the warning.

我在 Windows 机器上遇到了同样的问题。当我运行一个git pull origin master命令时,我会收到“在 X 提交之前'origin/master'”警告。我发现如果我改为运行git pull origin并且没有指定分支,那么我将不再收到警告。