简体   繁体   English

如何将开发分支带入从功能分支克隆的远程仓库?

[英]How to bring dev branch into remote repo that was cloned off a feature branch?

I created a new branch in my remote repository that was cloned off a feature branch using:我在远程存储库中创建了一个新分支,该分支使用以下功能从功能分支中克隆出来:

git clone <url> --branch <feature-branch-name> --single-branch <repo-name>

I now need to merge the latest changes from my dev branch, but I'm unable to clone it into my existing local repository and I'm unable to see it when I do git branch .我现在需要合并来自我的dev分支的最新更改,但是我无法将它克隆到我现有的本地存储库中,并且当我执行git branch时我无法看到它。

Any ideas on how I'll be able to do this?关于我将如何做到这一点的任何想法?

I've tried regular cloning into the existing directory using the standard way and using the --branch dev --single-branch method, but no luck.我尝试使用标准方式和使用--branch dev --single-branch方法定期克隆到现有目录,但没有运气。

UPDATE, found this simple alternative更新,找到了这个简单的替代方案

Create a new origin创建一个新的原点

git remote add *NewOriginName* *repositoryURL*

then create a new orphan branch然后创建一个新的孤立分支

git checkout --orphan *myNewBranch*

this new branch will have no commits but all the files from the branch you started with, so you probably will want to delete all files on this new branch, then这个新分支将没有提交,但是你开始使用的分支中的所有文件,所以你可能想要删除这个新分支上的所有文件,然后

git pull *NewOriginName* *branchOfInterestOnRepo* --allow-unrelated-histories

and thats it, you could need a new commit depending on the files you left on the new orphan branch就是这样,您可能需要一个新的提交,具体取决于您留在新孤儿分支上的文件


That sounds like a submodule这听起来像一个子模块

https://git-scm.com/book/en/v2/Git-Tools-Submodules https://git-scm.com/book/en/v2/Git-Tools-Submodules

But if you dont want to keep the previous commits you can just download the files from the commit you need但是如果你不想保留以前的提交,你可以从你需要的提交中下载文件

If you prefer to use the console the way will be with如果您更喜欢使用控制台,方式将是

git clone -b <branchname> --single-branch <remote-repo-url>

This will not create a branch, this will get the files on your active branch, in a new directory, this directory with the name of the brach from the repository这不会创建分支,这将获取活动分支上的文件,在一个新目录中,该目录具有存储库中分支的名称

You will want to un-single-branch-ize your repository.您将希望取消单分支化您的存储库。 You have two obvious paths to go by here.你有两条明显的路径到 go 到这里。 One option is to undo the single branch aspect: see How do I "undo" a --single-branch clone?一种选择是撤消单分支方面:请参阅如何“撤消”--single-branch 克隆? (for which this would be a duplicate of that question). (这将是该问题的重复)。 The other requires that you read the git remote documentation , paying particular attention to the set-branches sub-command with its --add sub-option.另一个要求您阅读git remote文档,特别注意带有--add子选项的set-branches子命令。

Once you've done this, run git fetch , then git checkout branch-name or git switch branch-name .完成此操作后,运行git fetch ,然后运行git checkout branch-namegit switch branch-name Git will create the new branch. Git 将创建新分支。 Or, use the remote-tracking name origin/ branch-name .或者,使用远程跟踪名称origin/ branch-name See below for more about remote-tracking names.有关远程跟踪名称的更多信息,请参见下文。

Background (optional reading, but a good idea)背景(可选阅读,但一个好主意)

It's important to realize that your base assumption here is subtly wrong though:重要的是要意识到您在这里的基本假设是微妙的错误:

my remote repository... was cloned off a feature branch [and] I'm unable to clone [ dev ] into my existing local repository我的远程存储库...已从功能分支克隆 [并且] 我无法将 [ dev ] 克隆到我现有的本地存储库中

The bad assumption embedded here is that git clone command doesn't clone a branch .这里嵌入的错误假设是git clone命令不会克隆分支 Git's git clone command is a sort of convenience command, shorthand for running six other commands. Git 的git clone命令是一种方便的命令,是运行其他六个命令的简写。 Five of those six other commands are Git commands.这六个其他命令中有五个是 Git 命令。 For illustration, here are the six commands, in the order they are run:为了说明,这里有六个命令,按照它们运行的顺序:

  1. mkdir (or your OS's equivalent): creates a new, empty directory or folder (whichever term you prefer). mkdir (或您的操作系统的等效项):创建一个新的空目录或文件夹(无论您喜欢哪个术语)。 The remaining commands are run in this new directory, although once git clone finishes, you also have to move into this new folder yourself.其余命令在这个新目录中运行,尽管git clone完成后,您还必须自己移动到这个新文件夹中。 (There's one mode where this command gets skipped, when you tell git clone about an existing, empty directory that it should use.) (当您告诉git clone它应该使用现有的空目录时,会跳过此命令的一种模式。)

  2. git init : this creates a new, empty repository, with no commits. git init :这将创建一个新的空存储库,没有提交。 A repository that has no commits cannot have any branches, so it also has no branches.没有提交的存储库不能有任何分支,因此它也没有分支。

  3. git remote add origin url : this adds what Git calls a remote to the empty repository. git remote add origin url :这会将 Git 调用的远程添加到空存储库。 A remote is simply a short name—here, origin —that will hold a few things.遥控器只是一个简短的名称——这里是origin ——它可以容纳一些东西。 The main thing it holds is a URL, but it also holds instructions for future git fetch commands.它拥有的主要内容是 URL,但它还拥有未来git fetch命令的指令。

    The git remote add sub-command can take a -t option to specify a particular branch name of interest. git remote add子命令可以采用-t选项来指定感兴趣的特定分支名称。 The --single-branch option to git clone adds this -t option. git clone--single-branch选项添加了这个-t选项。 (The git remote add sub-command can also use a name other than origin . Using -o in git clone does this, but we'll assume that you did not use -o and hence use the name origin .) git remote add子命令也可以使用除origin以外的名称。在git clone中使用-o可以做到这一点,但我们假设您没有使用-o因此使用名称origin 。)

  4. Any other git config commands implied by -c options to git clone . git clone-c选项暗示的任何其他git config命令。 (These -c options must be at the right place in the command-line arguments: some -c options are handled by the git front end rather than the clone command.) If you did not use such options, git clone does nothing during this step as there is nothing to do. (这些-c选项必须在命令行 arguments 中的正确位置:一些-c选项由git前端而不是clone命令处理。)如果您没有使用这些选项, git clone期间不会执行任何操作一步,因为没有什么可做的。

  5. git fetch origin (or other name supplied via -o ): this obtains commits from the other Git, the one that responds to the URL you supplied. git fetch origin (或通过-o提供的其他名称):这从另一个 Git 获得提交,该 Git 响应您提供的 URL。 This does not create any branch names in your repository.这不会在您的存储库中创建任何分支名称。 Your new repository still has no branches in it at this point.此时,您的新存储库中仍然没有分支

  6. git checkout : this creates one branch in your new repository. git checkout :这会在您的新存储库中创建一个分支 The branch it creates is the one you supplied with the -b option.它创建的分支是您使用-b选项提供的分支。 If you did not supply a -b option, it creates the branch whose name is recommended by the other Git.如果您没有提供-b选项,它将创建名称由其他 Git 推荐的分支。

    If you use the -n or --no-checkout option, git clone skips this step.如果您使用-n--no-checkout选项, git clone跳过此步骤。

So whether or not you use --single-branch , step 6 of git clone creates only one branch.因此,无论您是否使用--single-branchgit clone的第 6 步只会创建一个分支。 You only have one branch in this new repository!您在这个新存储库中只有一个分支! A normal clone copies all commits from the source repository, and none of the branches , and then Git creates one branch as a last step.一个正常的克隆从源存储库复制所有提交,并且没有任何分支,然后 Git 创建一个分支作为最后一步。

What --single-branch does is just one thing, but has several effects. --single-branch所做的只是一件事,但有几个效果。 That's because of the way git fetch works.这是因为git fetch的工作方式。 At step 5, when git fetch runs, the fetch command connects to the other Git and its repository.在第 5 步,当git fetch运行时,fetch 命令连接到另一个 Git 及其存储库。 The other Git lists out all of its branch names and other names.另一个 Git 列出了它的所有分支名称和其他名称。 1 Your Git can choose to heed all of these names, or just a few of them (eg, just one branch name). 1您的 Git 可以选择注意所有这些名称,或者只注意其中的一些名称(例如,仅一个分支名称)。 Each of these names also lists a raw commit hash ID, and it's these hash IDs —not the names.—that Git needs, They are how Git finds commits, and other important objects. Each of these names also lists a raw commit hash ID, and it's these hash IDs —not the names.—that Git needs, They are how Git finds commits, and other important objects. and they are how your repository clones the other repository.它们是您的存储库克隆另一个存储库的方式。

In any case, having obtained the appropriate commits and other objects, your Git now takes their Git's branch names—the ones they listed to let your Git find commits—and changes them .在任何情况下,在获得适当的提交和其他对象后,您的 Git 现在会采用他们的 Git分支名称——他们列出的那些让你的 Git找到提交——并更改它们 Your Git stores them—or some selected subset of them, such as exactly one of them—in your own repository as remote-tracking names .您的 Git 将它们或它们的某些选定子集(例如其中一个)存储在您自己的存储库中作为远程跟踪名称

The --single-branch option tells your Git to leave instructions for fetch to create or update only the one remote-tracking name corresponding to the one branch name in their Git. --single-branch选项告诉您的fetch留下指令以仅创建或更新与 Git 中的一个分支名称对应的一个远程跟踪名称。 The default, of course, is to create or update a remote-tracking name for every branch that the other Git lists out, every time.当然,默认设置是每次都为另一个 Git 列出的每个分支创建或更新一个远程跟踪名称。 If you use git remote to add more of their branch names, each git fetch will create or update each of those names, provided that the other Git is listing them on each fetch.如果您使用git remote添加更多分支名称,则每个git fetch将创建或更新每个名称,前提是其他 Git 在每个提取中列出它们。 So by undoing the single-branch-ness, or adding more names, you can get your own Git to create more remote-tracking names.因此,通过撤消单分支或添加更多名称,您可以获得自己的 Git 以创建更多远程跟踪名称。


1 The data here can be painfully large—on the order of megabytes—in some repositories with tens of thousands of branch and tag names, and hence there's a way to limit it in the latest Git communication protocols. 1此处的数据在具有数万个分支和标签名称的某些存储库中可能非常大(大约为兆字节),因此在最新的 Git 通信协议中可以限制它。 Older Gits always get everything and the default, for a non-single-branch Git, is to get everything, so that it all still works right, but this does mean that if both your machine and your server have a newer Git version, the single-branch modes can be quite useful in some special cases today.较旧的 Gits 总是得到所有东西,对于非单分支 Git,默认值是得到所有东西,所以它仍然可以正常工作,但这确实意味着如果你的机器和服务器都有更新的 Git 版本,单分支模式在今天的某些特殊情况下非常有用。


Branch names and remote-tracking names分支名称和远程跟踪名称

A branch name is a name you make up yourself, that should have some meaning to you.分支名称是您自己编写的名称,对您应该有一些意义。 This simply stores the hash ID of one (1) commit.这只是存储一 (1) 个提交的 hash ID。 By definition, this one commit is the latest commit on the branch .根据定义,这一次提交是分支上的最新提交 This allows Git to find that commit, and commits in Git allow Git to find more commits in Git.这允许 Git 找到该提交,并且在 Git 中的提交允许 Git 在 Z0BCC70105AD277B663 中找到更多提交By finding the last one, Git can find all of the commits that go with that branch.通过找到最后一个,Git 可以找到 go 与该分支的所有提交。

(This means that the word branch is ambiguous: does it refer to the name , such as dev or main or whatever? Or does it refer to the collection of commits ended by the latest one? The answer is one, both, or neither, depending on who says it and what they meant when they said it. See also What exactly do we mean by "branch"? ) (这意味着分支这个词是模棱两可的:它是指名称,例如devmain还是其他什么?还是指以最新的提交结尾的提交集合?答案是一个,两者,或两者都不是,取决于谁说的以及他们说这句话时的意思。另请参阅我们所说的“分支”到底是什么意思?

A remote-tracking name is made up of the name of the remote— origin —plus a slash, followed by the original branch name.远程跟踪名称由远程名称 - origin - 加上一个斜线,后跟原始分支名称组成。 2 These names, like branch names, allow Git to find commits. 2这些名称与分支名称一样,允许 Git查找提交。 But they're not actually branch names.但它们实际上并不是分支名称。 You can tell the difference because:您可以区分,因为:

git checkout somebranch

tells you that you are now "on" the branch, and git branch and git status tell you that you are now on branch somebranch (using that exact phrase for git status ).告诉您您现在“在”分支上,并且git branchgit status告诉您您现在on branch somebranch (使用git status的确切短语)。 But:但:

git checkout origin/somebranch

tells you that you are now in what Git calls detached HEAD mode, and git branch and git status show that you are no longer on any branch.告诉您您现在处于 Git 所谓的分离 HEAD模式,并且git branchgit status表明您不再在任何分支上。 (This mode is fine for looking at commits, but not a good idea to stay in to do new work. To exit this mode, run git checkout with the name of one of your branches, as shown by git branch 's output.) (此模式适用于查看提交,但不适合继续进行新工作。要退出此模式,请使用您的一个分支的名称运行git checkout ,如git branch的 Z78E6221F6393D136566 所示。)

When you ask Git to switch to a branch named B , Git checks first to see if you have a branch named B .当您要求 Git 切换到名为B的分支时,Git 首先检查您是否一个名为B的分支。 If you do, Git switches to it.如果你这样做了,Git 会切换到它。 3 But if not, just before saying "I can't switch to that because it doesn't exist", the checkout or switch command (whichever one you used) will see if there's some obvious origin/ B that could be used to create B . 3但如果不是,就在说“我不能切换到那个,因为它不存在”之前,checkout 或 switch 命令(无论你使用哪个)都会查看是否有一些明显的origin/ B可以用来创建B If so, the Git command will create B using origin/ B , 4 then switch to it.如果是这样, Git 命令将使用origin/ B创建B4然后切换到它。 This is in fact how step 6 of git clone works.这实际上是git clone的第 6 步的工作原理。


2 Technically, these remote-tracking names are in a separate namespace . 2从技术上讲,这些远程跟踪名称位于单独的命名空间中。 The full spelling of a remote-tracking name begins with refs/remotes/ while the full spelling of a branch name begins with refs/heads/ .远程跟踪名称的完整拼写以refs/remotes/开头,而分支名称的完整拼写以refs/heads/开头。 Git usually strips off the refs/remotes/ and refs/heads/ parts. Git 通常会剥离refs/remotes/refs/heads/部分。 Oddly, under git branch -a , the remote-tracking names are listed with only refs/ stripped off.奇怪的是,在git branch -a下,远程跟踪名称仅列出了refs/剥离。

3 This assumes that said switching is possible. 3这假设所述切换是可能的。 It is possible if you're in a "clean" state, ie, have not modified anything.如果您处于“干净”的 state 中,即没有修改任何内容,则有可能。 It is sometimes possible even if not.即使没有,有时也是可能的。 See also Checkout another branch when there are uncommitted changes on the current branch另请参阅当当前分支上有未提交的更改时签出另一个分支

4 In fact, any remote-tracking name, even if it starts with something other than origin/ , works here. 4事实上,任何远程跟踪名称,即使它以origin/以外的其他名称开头,都可以在这里使用。 The "using" part means that your new branch is created using the same commit hash ID stored in your own repository's remote-tracking name. “使用”部分意味着您的新分支是使用存储在您自己的存储库的远程跟踪名称中的相同提交 hash ID创建的。

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

相关问题 git:克隆了一个远程仓库,签出了一个分支,提交了一个更改...如何在远程更新分支? - git : Cloned a remote repo, checked out a branch, committed a change…how to update branch on remote? 如何使功能分支与开发分支同步? - How to bring feature branch in sync with develop branch? 如何使 git “看到”一个远程分支,用于使用 --single-branch 克隆的仓库? - How to make git “see” a remote branch for a repo that was cloned with --single-branch? 如何在开发分支上重新设置功能分支 - How to rebase feature branch on dev branch 从错误的分支开发了一个新功能并将分支推送到远程 - Developed a new feature off of the wrong branch and pushed branch to remote 如何更改克隆的Git存储库的“ master”分支以指向您自己的遥控器? - How do you change the 'master' branch of a cloned Git repo to point to your own remote? 使用git clone --depth 1克隆repo后,如何获取远程分支 - How do fetch remote branch after I cloned repo with git clone --depth 1 当我从github远程仓库克隆时,远程仓库的哪个分支被克隆到本地仓库? - When I clone from github remote repo, which branch of remote repo is cloned to local repo? 如何从远程仓库拉到远程分支 - How to pull from remote repo to remote branch 如何更新克隆分支 - How to update a cloned branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM