Git:从克隆更改为远程分支

发布于 2024-12-07 10:29:17 字数 1085 浏览 3 评论 0原文

我有一个 git 工作流程问题,但我不完全理解所有术语来了解要搜索的内容。

简短版本:当本地存储库作为远程存储库的克隆启动时,是否可以将本地 git 目录“切换”到远程存储库的分支?我对 git 很陌生,所以我仍在快速了解这里的术语;如果我说了一些看似不对/错误/做出假设的事情,请告诉我。

长版。

我有一个本地存储库,它是“中央”存储库的克隆。该存储库有多个分支,每个分支都比原始存储库的主分支提前许多修订。

$ git branch
  foo
* master
  universal_imports    

我知道我可以使用 git 的 checkout 命令“切换”到我的本地分支之一

$ git checkout foo
$ git checkout universal_imports
etc.

我还有一些遥控器

$ git remote
origin
upstream

upsteam 的 master 分支是我们的事实来源。它是产品,即前面提到的中央存储库。如前所述,我的本地主存储库是作为该存储库的克隆开始的。 (实际上,我的本地存储库是 origin 远程存储库的克隆,它是上游主分支的 github 分支。我不确定这是否相关)

这是我试图解决的场景。我一直在我的分支机构进行新的开发。我比原版大师早了很多版本。但是,我刚刚收到修复生产中某些问题的请求。我的思维模型是

  1. 切换我的本地代码,使其与生产代码匹配
  2. 进行修复并提交
  3. 将我的本地代码合并到真实来源分支
  4. 将我的本地代码切换回我的“真实”分支,然后继续新的分支功能开发的

目标是避免在准备好之前将任何新代码投入生产。

除了在某个地方重新克隆中央存储库之外,是否有 git 工作流程?理想情况下,我想要像我所描述的“切换到远程分支”那样的东西,但如果这是无稽之谈,有没有一种方法可以让我使用 git 来处理这种情况。 (在新工作上继续前进,但主要是解决历史上更早的问题的能力)

I've got a git workflow question, and I don't fully understand all the terms to know what to google for.

The short version: Is it possible to "switch" your local git directory to a branch from a remote repository, when your local repositories started as clones of the remote repository? I'm newish to git, so I'm still coming up to speed on the terminology here; if I've said something that seems off/wrong/making-an-assumption please let me know.

The long version.

I have a local repository that's a clone of "the central" repository. This repository has several branches, each of which is many revisions ahead of the original repository's master branch.

$ git branch
  foo
* master
  universal_imports    

I know I can "switch" to one of my local branches by using git's checkout command

$ git checkout foo
$ git checkout universal_imports
etc.

I also have a few remotes

$ git remote
origin
upstream

The upsteam's master branch is our source of truth. It is the product, the aforementioned central repository. As previously stated, my local master repository started as a clone of this repository. (actually, my local repository is a clone of the origin remote, which is a github fork of the upstream's master branch. I'm unsure if that's relevant)

Here's the scenario I'm trying to solve. I've been doing new development on my branches. I'm many versions ahead of the original master. However, I've just received a request to fix something in production. My mental model here would be to

  1. Switch my local code so it matches the production code
  2. Do the fix and commit it
  3. Merge my local code into the source-of-truth branch
  4. Switch my local code back to my "real" branches, and continue new feature development

The goal is to avoid putting any of my new code into production before it's ready.

Short of re-cloning the central repository somewhere, is there a git workflow for this? Ideally I'd like something like i described "switch to a remote branch", but if that's nonsense talk is there a way I could have worked with git that would have allowed me to handle this situation. (blaze ahead on new work, but main the ability to work off a point further back in the history)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2024-12-14 10:29:17

您可以创建一个新分支来跟踪远程分支,如下所示(此处本地分支称为“bugfix”):

git checkout -b bugfix upstream/master

一旦您有了本地分支跟踪并与所需的远程分支同步,您就可以进行更改,然后 git commitgit Push,然后签出您的原始工作分支。

最后,再次检查您的工作分支后,您需要将错误修复更改合并到您的工作分支中,如下所示:

git merge bugfix

):

git rebase -i bugfix

或者像这样变基(变基创建更干净的历史记录,但在解决冲突方面可能需要更多工作 可以继续并将该分支留在那里(在这种情况下,您可能希望将其称为比“bugfix”更永久的名称,可能是“upstream-master”或“live-product”或其他名称)。或者,如果您不再需要本地分支,可以删除它,如下所示:

git branch -D bugfix

You can create a new branch to track a remote branch like this (here the local branch is called 'bugfix'):

git checkout -b bugfix upstream/master

Once you have a local branch tracking and synced with the desired remote branch, you can just make your changes, then git commit and git push, then checkout your original working branch.

Finally, after checking out your working branch again, you'll want to either merge your bugfix change into your working branch like this:

git merge bugfix

Or rebase it like this (rebasing creates a more clean history but can take more work in resolving conflifcts):

git rebase -i bugfix

You can go ahead and leave that branch there (in which case you'll probably want to call it something more permanent than 'bugfix', maybe something like 'upstream-master', or 'live-product', or whatever). Or you can delete the local branch if you don't want it anymore, like this:

git branch -D bugfix
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文