简体   繁体   English

Git仅检出分支目录中的新文件

[英]Git checkout only new files from branch directory

I have such situation. 我有这样的情况。 One developer is working in ./model directory and he is adding new files to it then he commit the new model_files and push to origin/master 一位开发人员正在./model目录中工作,他正在向其添加新文件,然后他提交新的model_files并推送到origin / master

Now second developer is writing models too in the same directory ./model (in his own working directory). 现在,第二个开发人员也在同一目录./model(在他自己的工作目录中)编写模型。

Suppose developer A create such models: 假设开发人员A创建了这样的模型:

./model/m1.php
./model/m2.php

Now the developer B create such models: 现在开发人员B创建了这样的模型:

./model/m3.php
./model/m4.php

Suppose developer A commit changes and push to the origin/master because developer B need m1.php and m2.php models to his work. 假设开发人员A提交更改并推送到origin / master,因为开发人员B需要m1.php和m2.php模型来完成他的工作。

The developer B also created many other changes/folders/files in project not only in ./models directory and his work is not finished yet. 开发人员B还在项目中创建了许多其他更改/文件夹/文件,不仅在./models目录中,他的工作尚未完成。 If he merge or pull then his work will lost. 如果他合并或拉动,那么他的工作就会失败。

How can the developer B retrieve the new model files in folder ./model only that developer A created. 开发人员B如何只检索开发人员A创建的文件夹./model中的新模型文件。 I tried something like this on developer B machine. 我在开发者B机器上尝试过这样的东西。 I assume that developer B didn't commit m3.php and m4.php because didn't end with that files yet. 我假设开发人员B没有提交m3.php和m4.php,因为还没有以那些文件结束。 He just need to have in ./models directory new files (m1.php and m2.php) from developer A. On developer B machine i tried: 他只需要在./models目录中有来自开发人员A的新文件(m1.php和m2.php)。在开发人员B机器上我试过:

git fetch origin
git checkout origin/master ./model/*

But unfortunately the error gives me 但不幸的是错误给了我

error: pathspec 'model/m3.php' did not match any file(s) known to git.
error: pathspec 'model/m4.php' did not match any file(s) known to git.

But if I execute: 但是如果我执行:

git checkout origin/master ./model/m1.php ./model/m2.php

Then it works but this assumes that I have to know exacly file names that I need to checkout from this branch. 然后它工作,但这假设我必须知道我需要从这个分支结帐的exacly文件名。

Can somebody help me with this? 有人可以帮我这个吗? Thanks. 谢谢。

Developer B should run 开发人员B应该运行

  1. git stash git stash
  2. git pull origin master git pull origin master
  3. git stash apply git stash适用

Line 1 stashes the developers work so their branch is free of changes before performing the pull 第1行阻止开发人员工作,因此在执行拉动之前,他们的分支没有变化

Line 2 performs a fetch and merge from the master directory on the remote. 第2行从远程主目录执行提取和合并。 This will get Developer A's changes. 这将获得开发人员A的更改。

Line 3 reapplies Developer B's work on the m3.php and m4.php files. 第3行重新应用了开发人员B对m3.php和m4.php文件的工作。

Obviously, do not include the numbers when executing the commands in git. 显然,在git中执行命令时不要包含数字。

You need to merge incoming changes into your working copy. 您需要将传入的更改合并到工作副本中。 In order to do so, just: 为了做到这一点,只需:

git fetch origin
git merge origin/master

In order to do so, you must first either commit or stage local changes in computer B. 为此,您必须首先在计算机B中提交或暂存本地更改。

I figured it out. 我想到了。 Almost everything was ok except this '*' at the end. 几乎一切都很好,除了最后的'*'。

The proper way to download files from remote branch to some project folder only adding new files to this folder: 将文件从远程分支下载到某个项目文件夹的正确方法只是将新文件添加到此文件夹:

git fetch origin
git checkout origin/master model/

Your shell expands 你的shell扩展了

git checkout origin/master ./model/* git checkout origin / master ./model/*

before git sees it. 在git看到它之前。 It is probably expanding to 它可能会扩展到

git checkout origin/master ./model/m1.php ./model/m2.php ./model/m3.php ./model/m4.php

and then you get the error from git that master is missing model/m3.php and model/m3.php . 然后你从git那里得到了master缺少model/m3.phpmodel/m3.php Telling the shell to not expand your * , like 告诉shell不要扩展你的* ,就像

git checkout origin/master './model/*'

seems to work, or just use the explicit path as per other solutions, like 似乎工作,或只是使用其他解决方案的显式路径,如

git checkout origin/master model/

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

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