Git:管理分支

发布于 2024-12-26 06:04:23 字数 262 浏览 0 评论 0原文

我想将远程分支与 head“同步”,这样当我最终合并它时,就不会很头疼了。因此,我想尝试将头部更改拉到我的分支中,看看它有多么不同。

如何在 Git 中完成以下工作流程?

  1. 签出远程分支。
  2. 一旦我检查出来,就将更改从头脑中拉入其中。
  3. 编辑分支
  4. ,将分支(现在与 HEAD 非常相似)推送回同一分支的远程版本(不影响 head)。

任何有关完成相同任务的更好工作流程的提示都会非常有用。

I want to "sync" a remote branch up with head, so that when I finally merge it, it won't be a headache. Thus, I want to try pulling head changes into my branch to see how different it is.

How can I accomplish the following workflow in Git?

  1. checkout a remote branch.
  2. Once I check it out, pull changes from head into it.
  3. edit the branch some
  4. push the branch, which now is pretty similar to HEAD, back to the remote version of the same branch (without affecting head).

Any tips on a better workflow that accomplishes the same thing would be very useful.

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

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

发布评论

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

评论(1

半窗疏影 2025-01-02 06:04:23

这些都是非常基本的东西:

# make sure your notion of the remote is up to date
git fetch origin
# create and check out a branch, at the same place as the remote master branch
git checkout -b origin-master origin/master
# merge your local master
git merge master
# test, edit away, hack hack hack
git add ...
git commit ...
# push back to origin
git push origin origin-master:master

术语注释:

  • 拉动是获取和合并的组合。当您使用本地分支进行操作时,无需获取,因此您进行的是合并,而不是拉取。

  • HEAD 的意思并不像你想象的那样。 (也许您是 cvs/svn 人员。)HEAD 只是当前签出的提交(通常通过分支名称引用)。所以你不是在合并 HEAD,而是在合并那个分支。我在这里称其为master。

至于你关于更好的工作流程来完成同样的事情的问题......嗯,这很难回答。你的目标有点模糊。你说“慢慢地”同步并指“最终合并它”,但你概述的步骤是一次性完成的,所以......好吧,它全部合并了。以后就没什么可做的了。如果您想增量地执行此操作,您可以简单地重复我给出的步骤,每次选择历史记录中的中间提交进行合并。还有一点不清楚您想要合并到哪个方向。也许您实际上想从您的分支开始,并将远程内容合并到其中?

 git checkout -b master-merging master
 git fetch origin
 git merge origin/master
 # test, hack, commit, push...
 git push origin master-merging:master

或者使用增量合并:

 git checkout -b master-merging master
 git fetch origin
 git merge origin/master~20    # 20 commits before origin/master
 # test, hack, commit
 git merge origin/master~10    # 10 commits before origin/master
 # test, hack, commit
 git merge origin/master
 # test, hack, commit, push...

This is all pretty basic stuff:

# make sure your notion of the remote is up to date
git fetch origin
# create and check out a branch, at the same place as the remote master branch
git checkout -b origin-master origin/master
# merge your local master
git merge master
# test, edit away, hack hack hack
git add ...
git commit ...
# push back to origin
git push origin origin-master:master

Terminology notes:

  • Pulling is a combination of fetching and merging. When you're operating with local branches, there's no need to fetch, so you're merging, not pulling.

  • HEAD doesn't mean what you think it means. (Maybe you're a cvs/svn person.) HEAD is simply the currently checked-out commit (usually referred to via a branch name). So you're not merging HEAD, you're merging that branch. I called it master here.

As for your question about better workflows to do the same thing... well, that's pretty hard to answer. Your goals are a little ambiguous. You said "slowly" sync up and refer to "finally merging it", but the steps you outline do it all at once, so... well, it's all merged. There's nothing to do later. If you wanted to do it incrementally, you could simply repeat the steps I gave, picking intermediate commits in the history to merge each time. It's also a little unclear which direction you want the merging in. Maybe you actually wanted to start from your branch, and merge remote stuff into it?

 git checkout -b master-merging master
 git fetch origin
 git merge origin/master
 # test, hack, commit, push...
 git push origin master-merging:master

Or with incremental merges:

 git checkout -b master-merging master
 git fetch origin
 git merge origin/master~20    # 20 commits before origin/master
 # test, hack, commit
 git merge origin/master~10    # 10 commits before origin/master
 # test, hack, commit
 git merge origin/master
 # test, hack, commit, push...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文