Git:我没有在 origin/master 之前承诺的分支如何

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/850473/
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-19 03:40:29  来源:igfitidea点击:

Git: how is a branch I'm not committing to ahead of origin/master

gitversion-control

提问by araqnid

I work 100% with a branch that I made off of the master branch. Now that a colleague has pushed back to origin/master, I tried to push those changes into my personal branch. When I do a 'git checkout master' followed by a 'git status' I get the following:

我 100% 使用我从 master 分支制作的分支。现在一位同事已经推回了 origin/master,我尝试将这些更改推送到我的个人分支。当我做一个 'git checkout master' 然后是一个 'git status' 时,我得到以下信息:

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

How is a branch I never commit to ahead by 2 commits? What's the best way to find out what commits they are and essentially undo them? I don't wish to push anything back to origin/master as that might cause unknown conflicts.

我从未承诺过 2 次提交的分支如何?找出它们是什么提交并基本上撤消它们的最佳方法是什么?我不想将任何东西推回原点/主节点,因为这可能会导致未知的冲突。

回答by araqnid

To see the commits you have in HEAD that are not in origin/master:

要查看 HEAD 中不在 origin/master 中的提交:

git log origin/master..

To blow them away and make your HEAD the same as origin/master:

将它们吹走并使您的 HEAD 与 origin/master 相同:

git reset --hard origin/master

How did you push the changes to your own repository? I notice you mentioned "push"... Is origin a central repo? Your colleague's repo? I suspect what you actually wanted to do was pull your colleague's changes in, either directly or from a central staging point, rather than pushing. It might simply be that the 2 changes you have ahead of origin/master are in fact your colleague's changes, but the origin/master tracking branch is stale.

您是如何将更改推送到您自己的存储库的?我注意到你提到了“推送”...... origin 是中央回购吗?你同事的回购?我怀疑您实际上想要做的是直接或从中央集结点拉入同事的更改,而不是推动。可能只是您在 origin/master 之前的 2 个更改实际上是您同事的更改,但是 origin/master 跟踪分支已经过时。

回答by dbr

You're working on a branch of master, and you've made two commits which are not in origin/master.

您正在 master 的一个分支上工作,并且您已经进行了两次不在origin/master.

The message # Your branch is ahead of 'origin/master' by 2 commits.is saying:

消息# Your branch is ahead of 'origin/master' by 2 commits.是这样说的:

# Your branch 'mybranch' has two commits not in 'origin/master'

Imagining git users SVN-like revision numbers, you're branch has commits 1, 2, 3, 4, 5 - but origin/masteronly has 1, 2, 3. So the revision history looks something like the following crappy ASCII diagram..

想象一下 git 用户类似 SVN 的修订号,你的分支提交了 1、2、3、4、5 - 但origin/master只有 1、2、3。所以修订历史看起来像下面蹩脚的 ASCII 图表。

your branch                                   -- [commit 4]--[commit 5]
                                             /                    /\ HEAD
master --[commit 1]--[commit 2]--[commit 3]-/
                                       /\ origin/master

To display the last two commits in the log, you can do..

要在日志中显示最后两次提交,您可以执行..

git log HEAD..HEAD~2

回答by Lucy Bain

You might need to make sure your origin/master is up to date with your repo version of master. Try running:

您可能需要确保您的 origin/master 与您的 master 的 repo 版本保持同步。尝试运行:

git fetch origin master

so your local origin/master is the same as the repo's master. Then when you run

所以你的本地来源/主人与回购的主人相同。然后当你跑

git pull origin master

you should get an accurate display of how many commits ahead your local master is. After that you can run araqnid's commandto see which commits actually are different.

您应该准确显示您的本地 master 有多少提交。之后,您可以运行araqnid 的命令来查看哪些提交实际上是不同的。

回答by MighMoS

Have you done any git rebase'ing on the branch you areworking on?

git rebase在你正在工作的分支上做过任何事情吗?

If not you could try copying your branch (git checkout -b testrebase) and issuing a git rebase masterto see if that works. This will unravel all commits you made relative to master and then try to apply them back (makes the history make sense, basically). If it doesn't work just delete testrebase.

如果没有,您可以尝试复制您的分支 ( git checkout -b testrebase) 并发出 agit rebase master以查看是否有效。这将解开您相对于 master 所做的所有提交,然后尝试将它们应用回来(基本上使历史变得有意义)。如果它不起作用,只需删除 testrebase。