简体   繁体   English

Git:如何在不添加合并提交的情况下从另一个存储库中提取

[英]Git: How to pull from another repository without adding a merge commit

This is a project where multiple developers are working.这是一个由多个开发人员共同工作的项目。 Assume that all work is done on the master branch, and that there is a remote repo that has the following history today:假设所有工作都在 master 分支上完成,并且今天有一个具有以下历史记录的远程仓库:

A --- B --- C

Today I went ahead and cloned this repository into one of my sandboxes (let's call this work_sb01 ).今天我继续将这个存储work_sb01到我的一个沙箱中(我们称之为work_sb01 )。 I did some work, and have committed my changes locally (there could be just one commit or multiple commits).我做了一些工作,并在本地提交了我的更改(可能只有一次提交或多次提交)。 The Git history for this sandbox looks as shown below:此沙箱的 Git 历史记录如下所示:

A --- B --- C --- X

Two days from today, I clone the same repository into another sandbox (let's call it work_sb02 ).两天后,我将同一个存储库克隆到另一个沙箱中(我们称之为work_sb02 )。 As expected, the repo HEAD has moved, so the history might now look something like this:正如预期的那样,repo HEAD已经移动,因此历史现在可能看起来像这样:

A --- B --- C --- D --- E

I need to be able to pull the changes that I made in work_sb01 and bring them into work_sb02 .我需要能够提取我在work_sb01所做的更改并将它们带入work_sb02 I'm currently doing this with the following command:我目前正在使用以下命令执行此操作:

cd <path_to_work_sb02>
git pull <path_to_work_sb01> master

This gets the job done, but it creates a merge commit.这完成了工作,但它创建了一个合并提交。 I have now a requirement to avoid adding merge commits into the project Git log.我现在需要避免将合并提交添加到项目 Git 日志中。 How can I pull the changes from work_sb01 and make them the most recent commit in the history?如何从work_sb01提取更改并使它们成为历史记录中的最新提交? In other words, the local history for work_sb02 should look like the one shown below:换句话说, work_sb02的本地历史记录应如下所示:

A --- B --- C --- D --- E --- X

Here is the right way to do this from work_sb02:这是从 work_sb02 执行此操作的正确方法:

cd <path_to_work_sb02>
git remote add sb01 <path_to_work_sb01> 
git fetch sb01 
git checkout master 
git rebase --onto master master sb01/master

If doing in path_to_work_sb02 a pull --rebase path_to_work_sb01 does not give you the right order, you could... consider doing the opposite:如果在path_to_work_sb02执行pull --rebase path_to_work_sb01没有给你正确的顺序,你可以......考虑做相反的事情:

  • pull --rebase path_to_work_sb02 from path_to_work_sb01 path.path_to_work_sb01路径中拉出 --rebase path_to_work_sb02
  • rename path_to_work_sb01 as path_to_work_sb02path_to_work_sb01重命名为path_to_work_sb02
  • change its remote origin URL更改其远程origin URL
  • go on working from there.从那里继续工作。

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

相关问题 如何使用git合并来自不同存储库的拉取请求(或提交)? - How to merge a pull request (or commit) from a different repository, using git? Git从另一个没有历史记录的存储库中拉 - Git pull from another repository without history 如何在Git历史记录中合并拉取请求而不获取合并提交 - How to merge a pull request without getting a merge commit in the Git history 如何从远程git存储库一次提取一个提交? - How to pull one commit at a time from a remote git repository? 如何使下一个git提交从另一个提交准确表示项目状态而不合并? - How to make next git commit to exactly represent project state from another commit without merge? 将 Git 提交合并到具有不同目录的另一个存储库中 - Merge a Git commit into another repository with different directory Git 从另一个仓库拉取 - Git pull from another repository 如何在不添加Remote的情况下将提交推送到git存储库? - How to push commit to git repository without adding Remote? 如何在不丢失任何提交的情况下将 git 存储库作为另一个 git 存储库的子目录导入? - How to import a git repository as a subdirectory of another git repository without losing any commit? 如何在不添加分支的完整提交历史记录的情况下合并另一个仓库中的分支? - How can I merge a branch from another repo without adding the branch's full commit history?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM