将主要分支提交给其他分支

发布于 2025-01-17 10:07:02 字数 272 浏览 2 评论 0 原文

假设有4个文件a,b,c和d,其中a,b,c的文件在主分支上进行。

现在,我创建一个新的分支(我们称其为子分支),做 git Checkout sub-Branch 并进行更改,并仅提交文件D。

但是,当我通过运行 git push -U Origin sub -Branch 将文件D上的更改推到远程存储库(github)时,甚至将文件a,b,c的提交添加到这个分支。

我只想在新创建的子分支中反映在文件D上所做的更改,我该如何完成?

Let's say there are 4 files A, B, C, and D, of which the files A, B, C are committed on the main branch.

I now create a new branch (let's call it sub-branch), do git checkout sub-branch and make changes, and commit file D only.

But, when I push the changes committed on the file D onto the remote repository(Github) by running git push -u origin sub-branch, even the commits of files A, B, C are added to this branch.

I'd want only the changes committed on file D to be reflected in the newly created sub-branch, how do I accomplish this?

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

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

发布评论

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

评论(4

凝望流年 2025-01-24 10:07:02

分支只是指向提交的指针。假设分支 main 包含这些提交:

X-A-B-C

并且 main 指向提交 C。当您创建新分支时,您从 main 开始,这相当于从提交 C 开始:

git switch -c sub-branch main
# since main points to C this is the same as
git switch -c sub-branch C

相反,听起来您只需要从较早的提交创建新分支即可:

git switch -c sub-branch X

如果您完成了子分支,最终会得到XD。由于您已经创建了分支,因此您可以通过将其重置回 X 并选择 D 来修复它:

git switch sub-branch
git reset --hard X
git cherry-pick D

Branches are just pointers to commits. Suppose branch main contains these commits:

X-A-B-C

And main is pointing to commit C. When you created your new branch you started from main which is equivalent to starting from commit C:

git switch -c sub-branch main
# since main points to C this is the same as
git switch -c sub-branch C

Instead it sounds like you just need to create your new branch from an earlier commit instead:

git switch -c sub-branch X

Had you done that sub-branch would have ended up with X-D. Since you've already made the branch though, you can fix it by resetting it back to X and cherry picking D:

git switch sub-branch
git reset --hard X
git cherry-pick D
分分钟 2025-01-24 10:07:02

但是,提交已经被推到远程主分支。

新的子分支将包括一个仅对D的更改。

但是,其父委员会将来自分支机构(以及在此之前的父母提交),

这是一条提交链:a diredected acyclic图(dag)

当我创建了从叉子仓库的子分支到原始仓库主分支的PR时,它表明4个文件是更改的,而不是1个。

那是因为目标分支尚未有这些文件。

如上所述, git log -oneline -graph -decorate -all -branches 将向您显示该图。

我想知道如何获得x

的哈希

也可以使用 git Merge-base

git merge-base main sub-branch

However, the commits have already been pushed to the remote main branch.

The new sub-branch will include one commit with only changes for D.

Its parent commit, however, will be from branch main (and the parent commit before that too)

That is what a chain of commits is: a directed acyclic graph (DAG)

When I created a PR from forked repo's sub-branch to the original repo's main branch, it showed that 4 files were changed instead of 1.

That would be because the target branch does not have those files yet.

As commented, a git log --oneline --graph --decorate --all --branches will show you that graph.

I'd want to know how do I get the hash of X

You can also use git merge-base:

git merge-base main sub-branch
游魂 2025-01-24 10:07:02

几个选项是:

  1. 从远程签出(假设尚未推送提交)。
  2. 在主分支中进行更改 A、B、C 之前检查分支,然后在新创建的分支中进行更改 D。
  3. 在进行更改 A、B、C 之前从特定提交中签出 git checkout -b ;

A few options are:

  1. Checkout from remote (assuming the commits haven't been pushed).
  2. Checkout the branch before making changes A, B, C in main, then make change D in your newly created branch.
  3. Checkout from the particular commit before you made changes A, B, C git checkout -b <new_branch_name> <sha1>
静水深流 2025-01-24 10:07:02

在向新分支结帐之前,请确保所有更改均已提交。

如果您不想进行重置或藏匿更改。

这将解决您的问题。

现在,如果您在分支D中提交任何内容,它将仅推动这些更改。

Before you checkout to a new branch make sure all of your changes are committed.

In case you do not want to commit either reset or stash your changes.

This will solve your problem.

And now If you commit anything in the branch D it will push only those changes.

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