简体   繁体   English

Git:将最后一次提交从分支移动到主分支

[英]Git: move last commit from branch to master

I have 2 commits: A and B. Both are independent from each other and contain different files. 我有2个提交:A和B.两个都是相互独立的,包含不同的文件。 I have 2 branches: master and branch1 : 我有2个分支: masterbranch1

master: A
branch 1: A, B

I need to remove branch 1 and to move commit B to master . 我需要删除branch 1并将提交B移动到master I tried to do that with 我试着这样做

git cherry-pick B , git cherry-pick B

but it just copied B into A, while I need it to have the same commit number and - most importantly - to save the comments that are there! 但它只是将B复制到A中,而我需要它具有相同的提交编号 - 最重要的是 - 保存那里的注释!

Is there any way? 有什么办法吗? I've looked through different answers and really unsure of what to do with my situation. 我看了不同的答案,真的不确定如何处理我的情况。

You have two options, a merge , or a rebase . 您有两个选项, 合并rebase

Merge 合并

Merging branch1 into master is the simplest operation to understand, and the option I would recommend. 将branch1合并到master中是最容易理解的操作,也是我建议的选项。 However, it will appear as a new commit on the master, with a different commit id . 但是,它将显示为主服务器上的新提交, 具有不同的提交ID

We will take the new commits from branch1 and attempt to merge them into master as a new changeset which we can then commit. 我们将从branch1获取新的提交,并尝试将它们合并为master作为我们可以提交的新变更集。 This will preserve the history of both branch1 and master. 这将保留branch1和master的历史记录。

From the command line, you would execute the following steps: 从命令行,您将执行以下步骤:

1) First checkout the branch you wish to merge into 1)首先签出分支您要合并

git checkout master

2) Now merge in your branch, using the --no-ff option 2)现在使用--no-ff选项合并到您的分支中

git merge --no-ff branch1

3) You can now delete the old branch if desired 3)如果需要,您现在可以删除旧分支

git branch -d branch1

Rebase 变基

The alternative approach is to perform a rebase. 另一种方法是执行rebase。

By rebasing your branch 1 onto master, you will effectively replay the commit history of branch1 onto the master. 通过将分支1重新定位到主服务器上,您将有效地将branch1的提交历史重播到主服务器上。 Afterwards, it will appear as though you had originally checked commit 'b' into master, preserving your commit id . 之后,它看起来好像你最初检查提交'b'到master, 保留你的提交ID

Again, from the command line, you would execute the following steps: 同样,从命令行,您将执行以下步骤:

1) First checkout the branch you want to rebase into 1)首先签出分支要变基

git checkout master

2) Now rebase your branch into the master 2)现在将你的分支重新加入主人

git rebase branch1

Note - there are some caveats with rebasing, and if you don't fully understand how it works, it's safer to use a merge instead. 注意 - 有一些关于变基的警告,如果你不完全理解它是如何工作的,那么使用合并更安全。

If in doubt - merge, don't rebase. 如果有疑问 - 合并,不要改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM