假设有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?
发布评论
评论(4)
分支只是指向提交的指针。假设分支
main
包含这些提交:并且
main
指向提交C
。当您创建新分支时,您从main
开始,这相当于从提交C
开始:相反,听起来您只需要从较早的提交创建新分支即可:
如果您完成了
子分支
,最终会得到XD
。由于您已经创建了分支,因此您可以通过将其重置回X
并选择D
来修复它:Branches are just pointers to commits. Suppose branch
main
contains these commits:And
main
is pointing to commitC
. When you created your new branch you started frommain
which is equivalent to starting from commitC
:Instead it sounds like you just need to create your new branch from an earlier commit instead:
Had you done that
sub-branch
would have ended up withX-D
. Since you've already made the branch though, you can fix it by resetting it back toX
and cherry pickingD
:新的子分支将包括一个仅对D的更改。
但是,其父委员会将来自分支机构(以及在此之前的父母提交),
这是一条提交链:a diredected acyclic图(dag)
那是因为目标分支尚未有这些文件。
如上所述,
git log -oneline -graph -decorate -all -branches
将向您显示该图。也可以使用
git Merge-base
: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)
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.You can also use
git merge-base
:几个选项是:
git checkout -b;
A few options are:
git checkout -b <new_branch_name> <sha1>
在向新分支结帐之前,请确保所有更改均已提交。
如果您不想进行重置或藏匿更改。
这将解决您的问题。
现在,如果您在分支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.