简体   繁体   English

使用git将分支的一部分合并到master中

[英]Merge part of a branch into master with git

I'll explain my scenario: 我将解释我的情况:

  1. I have two git branches say master , branch1 我有两个git分支说masterbranch1
  2. My master branch has following files and commits associated with it - it's like after each file i created i did git commit : 我的master分支具有以下文件和与之关联的提交-就像在我创建每个文件之后我都执行git commit

     master-file1.html has masterfile1-commit-message master-file2.html - masterfile2-commit-message master-file3.html - masterfile3-commit-message 
  3. My branch1 branch has following files and commits associated with it: 我的branch1分支具有以下文件和与之关联的提交:

     branch1-file1.html has branch1file1-commit-message branch1-file2.html - branch1file2-commit-message branch1-file3.html - branch1file3-commit-message branch1-file4.html - branch1file4-commit-message branch1-file5.html - branch1file5-commit-message 

So i don't want to merge branch1 completely to master branch, i just want partial merge. 所以我不想将branch1完全合并到master分支,我只想部分合并。 So at the final i want is something like this: 所以最终我想要的是这样的:

  1. My master branch should be like this: 我的master分支应该是这样的:

     master-file1.html has masterfile1-commit-message master-file2.html - masterfile2-commit-message master-file3.html - masterfile3-commit-message branch1-file1.html has branch1file1-commit-message branch1-file2.html - branch1file2-commit-message 
  2. My branch1 branch should be unchanged: 我的branch1分支应该保持不变:

     branch1-file1.html has branch1file1-commit-message branch1-file2.html - branch1file2-commit-message branch1-file3.html - branch1file3-commit-message branch1-file4.html - branch1file4-commit-message branch1-file5.html - branch1file5-commit-message 

Can i achieve this in git? 我可以在git中实现吗? If so then how? 如果是这样,那又如何?

First, find the SHA1 hash for the commit with branch1-file2.html . 首先,使用branch1-file2.html查找提交的SHA1哈希。 You can do this with the following commands: 您可以使用以下命令执行此操作:

git checkout branch1
git log --oneline

Look for the hex string at the beginning of each line. 在每行的开头查找十六进制字符串。 Now once you have the hash, just merge master to it: 现在,一旦有了哈希,就可以将master合并到其中:

git checkout master
git merge <SHA1 hash>

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

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