简体   繁体   English

如何从另一个分支签出文件,然后覆盖当前分支

[英]How to checkout files from another branch and then overwrite the current branch

Based on http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/ , we can use the following command to merge changes from another branch to local branch. 基于http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/ ,我们可以使用以下命令合并来自另一个的更改分支到当地分公司。

For example: 例如:

$git checkout master -- hello.c

This command will merge the hello.c from master branch to current local branch. 此命令将hello.c从master分支合并到当前本地分支。

Question> What if I want to use the hello.c from master to overwrite my local branch file without any merge? 问题>如果我想使用master中的hello.c来覆盖我的本地分支文件而不进行任何合并,该怎么办? which option I should use? 我应该使用哪个选项?

Thank you 谢谢

-- Update -- - 更新 -

$ pwd
/sandbox/projectOne

$ git branch
  feature/F_Source
* feature/F_Dest
  master
$ git status -s
$
$ git diff feature/F_Source feature/F_Dest -- Messages/src/Hello.C | wc -l
165
$ git checkout feature/F_Source -- Messages/src/Hello.C
$
$ git diff feature/F_Source feature/F_Dest -- Messages/src/Hello.C | wc -l
165
$ git status -s
M  Messages/src/Hello.C
$

As you can see, before the checkout, there is a difference between F_Source Hello.C and F_Dest Hello.C. 如您所见,在结账之前,F_Source Hello.C和F_Dest Hello.C之间存在差异。 After the checkout, there is still a difference. 结账后,仍然存在差异。 So I assume the checkout is NOT a overwrite operation otherwise we should not see any difference. 所以我假设结账不是覆盖操作,否则我们不应该看到任何差异。

Note this quote from the link you posted: 请注意您发布的链接中的引用:

The code you need to grab is isolated to a handful of files, and those files don't yet exist in the master branch . 您需要抓取的代码与少数文件隔离,并且主分支中尚不存在这些文件。

So what he is calling a "merge" is not really a merge at all; 所以他称之为“合并”并不是真正的合并; it is simply copying the files over, no merging required. 它只是复制文件,不需要合并。 When you call git checkout master -- hello.c , it is indeed overwriting your local copy of hello.c with whatever is in master ; 当你调用git checkout master -- hello.c ,确实 master任何东西覆盖你的本地hello.c副本; it is not merging anything. 它没有合并任何东西。

Question> What if I want to use the hello.c from master to overwrite my local branch file without any merge? 问题>如果我想使用master中的hello.c来覆盖我的本地分支文件而不进行任何合并,该怎么办?

which option I should use? 我应该使用哪个选项?

As you already found out this is the way to do it (what you did so far) 正如您已经发现的那样,这是做到这一点的方式(到目前为止您做了什么)

git checkout <branch_name> -- <file path>


# On branch master - checkout different branch
git checkout <branch2>

# now checkout the desired file form different branch (master in this case)
git checkout master -- hello.c

Since you are checking out file from different branch there will be a merge. 由于您正在检查来自不同分支的文件,因此将进行合并。 If you want not to merge simply grab the content of the file you need and overwrite it over the current file so the content will be new and it will be a simple change. 如果你不想合并,只需抓取你需要的文件的内容,并覆盖当前文件,这样内容将是新的,这将是一个简单的改变。

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

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