Git仅将单个文件从master合并到当前分支 [英] Git Merge only single file from master into current branch

查看:77
本文介绍了Git仅将单个文件从master合并到当前分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用git.我从母公司创建了分公司A.我已经在A分支中创建了文件abc.txt,并将A分支成功合并到master中.现在,我在分支A中使用相同的文件,并希望将主文件(abc.txt)合并到分支A(abc.txt)文件中.我尝试了"git checkout master abc.txt",但是它用master的abc.txt替换了branch-A文件.

I have just started using git. I created branch-A from master. I have created the file abc.txt in branch-A and merged branch-A into master successfully. Now I am working same file in branch-A and want to merge master's (which abc.txt) file into branch-A (abc.txt) file. I tried "git checkout master abc.txt" but it replaces branch-A file with master's abc.txt.

我知道"git merge master"将完成工作,但是如果有人可以向我展示如何仅将单个文件合并到当前分支中.

I know "git merge master" will do the work, but if someone can show me how to merge single file ONLY into current branch.

预先感谢

推荐答案

首先,请确保您的工作目录是干净的.

Firstly, make sure your working directory is clean.

然后您发现了...

git checkout master abc.txt

...将替换整个文件,但是您可能还没有意识到,如果您重置,现在可以根据这些差异制作补丁.所以之后检出文件:

...will replace the whole file, but what you might not have realized is that now you could make a patch from those differences if you reset. So after checking out the file:

git reset --mixed
git add --patch abc.txt

现在,您可以有选择地选择要包括的内容,然后提交,然后通过丢弃不想要的差异而很难重置以达到最终状态.

Now you can selectively choose the changes you want to include, commit, and then reset hard to arrive at that final state by discarding the unwanted differences.

git commit -m "merge changes to abc.txt"
git reset --hard

这篇关于Git仅将单个文件从master合并到当前分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆