Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

git - 主分支和'起源/主人'有分歧,如何'疏远'分支'?(master branch and 'origin/master' have diverged, how to 'undiverge' branches'?)

Somehow my master and my origin/master branch have diverged.

(不知何故,我的主人和我的起源/主人分支已经分道扬..)

I actually don't want them to be diverged.

(我实际上不希望它们分歧。)

How can I view these differences and 'merge' them?

(如何查看这些差异并“合并”它们?)

  ask by Frank translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can review the differences with a:

(您可以通过以下方式查看差异 :)

git log HEAD..origin/master

before pulling it (fetch + merge) (see also "How do you get git to always pull from a specific branch?" )

(在拉它之前(fetch + merge)(另请参阅“如何让git始终从特定分支中拉出来?” ))


When you have a message like:

(如果您有以下消息:)

"Your branch and 'origin/master' have diverged, # and have 1 and 1 different commit(s) each, respectively."

(“你的分支和'origin / master'分歧,#并分别有1个和1个不同的提交。”)

, check if you need to update origin .

(,检查是否需要更新origin 。)

If origin is up-to-date, then some commits have been pushed to origin from another repo while you made your own commits locally.

(如果origin是最新的,那么当您在本地进行自己的提交时,一些提交已被推送到另一个repo的origin 。)

... o ---- o ---- A ---- B  origin/master (upstream work)
                   
                    C  master (your work)

You based commit C on commit A because that was the latest work you had fetched from upstream at the time.

(您基于提交A提交C,因为这是您当时从上游获取的最新工作。)

However, before you tried to push back to origin, someone else pushed commit B.

(但是,在您尝试推回原点之前,其他人推送了提交B.)
Development history has diverged into separate paths.

(发展历史已分化为不同的路径。)

You can then merge or rebase.

(然后,您可以合并或变基。)

See Pro Git: Git Branching - Rebasing for details.

(有关详细信息,请参阅Pro Git:Git Branching - Rebasing 。)

Merge

(合并)

Use the git merge command:

(使用git merge命令:)

$ git merge origin/master

This tells Git to integrate the changes from origin/master into your work and create a merge commit.

(这告诉Git将来自origin/master的更改集成到您的工作中并创建合并提交。)
The graph of history now looks like this:

(历史图现在看起来像这样:)

... o ---- o ---- A ---- B  origin/master (upstream work)
                         
                    C ---- M  master (your work)

The new merge, commit M, has two parents, each representing one path of development that led to the content stored in that commit.

(新的merge,commit M,有两个父节点,每个父节点代表一条开发路径,导致存储在该提交中的内容。)

Note that the history behind M is now non-linear.

(请注意,M背后的历史现在是非线性的。)

Rebase

(变基)

Use the git rebase command:

(使用git rebase命令:)

$ git rebase origin/master

This tells Git to replay commit C (your work) as if you had based it on commit B instead of A.

(这告诉Git重放提交C(你的工作),好像你是基于提交B而不是A.)
CVS and Subversion users routinely rebase their local changes on top of upstream work when they update before commit.

(CVS和Subversion用户在提交之前更新时会定期在上游工作之上重新定义其本地更改。)
Git just adds explicit separation between the commit and rebase steps.

(Git只是在commit和rebase步骤之间添加了明确的分离。)

The graph of history now looks like this:

(历史图现在看起来像这样:)

... o ---- o ---- A ---- B  origin/master (upstream work)
                          
                           C'  master (your work)

Commit C' is a new commit created by the git rebase command.

(Commit C'是由git rebase命令创建的新提交。)
It is different from C in two ways:

(它在两个方面与C不同:)

  1. It has a different history: B instead of A.

    (它有不同的历史:B而不是A.)

  2. Its content accounts for changes in both B and C;

    (其内容考虑了B和C的变化;)

    it is the same as M from the merge example.

    (它与合并示例中的M相同。)

Note that the history behind C' is still linear.

(请注意,C'背后的历史仍然是线性的。)
We have chosen (for now) to allow only linear history in cmake.org/cmake.git .

(我们选择(目前)只允许在cmake.org/cmake.git使用线性历史记录。)
This approach preserves the CVS-based workflow used previously and may ease the transition.

(这种方法保留了之前使用的基于CVS的工作流程,可以简化过渡。)
An attempt to push C' into our repository will work (assuming you have permissions and no one has pushed while you were rebasing).

(尝试将C'推入我们的存储库将起作用(假设您有权限,并且在您重新定位时没有人推送)。)

The git pull command provides a shorthand way to fetch from origin and rebase local work on it:

(git pull命令提供了一种从源获取并在其上重新定义本地工作的简写方法:)

$ git pull --rebase

This combines the above fetch and rebase steps into one command.

(这将上面的fetch和rebase步骤合并为一个命令。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...