[英]Git Maintaining history Streamlined
I have taken a branch(Branch A) from Master and done some commits on the Branch A, in the meantime, some Commits are merged on Master from Another Branch B Problem is: I have to Push my commits and Merge to Master so which approach will be best : 我已经从Master取得了一个分支(分支A),并在分支A上做了一些提交,与此同时,一些Commit从另一个Branch B合并到Master上了。最好:
1)I Push My commits to remote Branch A, then checkout master, then take a pull and then merge the Branch A in master and Push the merged code in Master. 1)我推送我的提交到远程分支A,然后检出master,然后进行拉取,然后将Branch A合并到master中,然后将合并的代码推送到Master中。
2)I check out master and git pull with rebasing the master and then Checkout Branch A and Push my commits to Branch A and then Checkout Master and merge Branch A in Master and Push the Master. 2)我检出master和git pull并重新设置master,然后检出Branch A,然后将提交提交到Branch A,然后检出Master,然后将Branch A合并到Master中,然后推入Master。
Push branchA
to remote: 将
branchA
推branchA
远程:
$ git push origin branchA
Checkout to master
, update master
with remote master
. 结帐到
master
,更新master
与remote master
。 Pull branchA
into master
, then push to remote: 将
branchA
拉入master
,然后推送到remote:
$ git checkout master # master branch
$ git pull origin master # update local master history = remote master history
$ git pull origin branchA # pull 'branchA' changes into 'master' branch
$ git push origin master # update remote master with branchA changes
You need neither, assuming that you are working in a pure git environment (ie, no "external" workflow management like Github's "pull requests" or something like that). 无需假设您在纯git环境中工作(即,无需像Github的“拉取请求”之类的“外部”工作流管理)。
Simply merge your branch locally, and then push those of the two that you want to have remote (ie, pushing branchA
is only necessary if you want to keep that branch long-term, and also want to have it remotely viewable for posteriority). 只需在本地合并分支,然后将要远程的两个分支推送即可(即,仅当您希望长期保留该分支并且还希望远程查看该分支以实现
branchA
,才需要推送branchA
)。
git checkout master
git pull origin master # just in case someone else changed it meanwhile...
git merge branchA
git push origin master branchA
Explanation: 说明:
First, you checkout master
. 首先,您结帐
master
。 This means your local "current" branch is master
, and the working directory represents its current file tree. 这意味着您本地的“当前”分支为
master
,工作目录代表其当前文件树。
Next, you pull the master
from origin
. 接下来,从
origin
拉出master
。 This is useful if someone else, meanwhile, committed something onto the remote master; 如果其他人同时将某些东西提交到远程主服务器上,这将很有用; it means that you're now up to date.
这意味着您现在处于最新状态。 If you know that nobody could have done that (because you're the only one working on it), then you can skip this step.
如果您知道没有人能做到这一点(因为您是唯一从事此工作的人),那么可以跳过此步骤。
Thirdly, you merge your branchA
into your local master
. 第三,将您的
branchA
合并到本地master
。 I assume that you own branchA
, that is, there is no chance that there could be a more recent branchA
on origin
, hence no further push/pull is necessary. 我假设您拥有
branchA
,也就是说,没有机会在origin
上有一个更新的branchA
,因此不需要进一步的推/拉操作。
Finally, you push both master
and branchA
back to origin
. 最后,将
master
和branchA
都推回origin
。 The remote (and your local) master
will now contain the merge commit that includes all changes originating in branchA
. 远程(和您本地的)
master
现在将包含合并提交,该提交包含源自branchA
所有更改。 branchA
will be unchanged and stick around for either historical purposes, or for further changes. branchA
将保持不变,并且出于历史目的或进一步更改而保留。
Some people never rebase, but always merge. 有些人从不变基,但总是合并。 Other people like to rebase.
其他人喜欢变本加厉。
You commented that you know about the difference, so I'll not write much. 您评论说,您知道其中的区别,所以我不会写太多。 But for this particular usecase (getting changes from a feature branch back into
master
), merging is the correct operation, and there should not be a rebase
involved. 但是对于这个特定的用例(从功能分支将更改返回到
master
),合并是正确的操作,并且不应该涉及任何rebase
。
If you were asking how to get changes from master
back into branchA
, then we would talk about whether you would use rebase
or merge
. 如果您询问如何将
master
更改返回到branchA
, 那么我们将讨论您将使用rebase
还是merge
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.