在Git中检出同名的多个文件 [英] Checking out multiple files of the same name in Git
问题描述
我尝试了一些沿着
这将返回
error:pathspec'* org。 eclipse.jdt.core.prefs与git已知的任何文件不匹配。
这是因为*?
问题出在这样一个事实,即 git checkout
不在子目录中搜索。新新旗新新新新旗新新旗新新旗新新旗旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新200新新旗新新旗2001-存储库中的位置。为了实现这一点,我们必须结合一些命令。
git ls-tree -r --name-only< branch> ; | grep< pattern>
这将返回名称中给定模式的所有文件的列表;我们将此命令的输出传递给 git checkout
,这将相应更新工作树。
git checkout< branch> - $(git ls-tree -r --name-only< branch> | grep< pattern>)
魔法完成。
为了更容易的使用,我建议您在全局< 200新新新新旗新新新旗新新旗旗哨位序200 200 200 200 200 200 200 200 200 200 200 200 CE 200 X- 20045 X- 20045 X- $ 1 - $(git ls-tree -r --name-only $ 1 | grep$ 2); }; f'
此后,您可以使用 git super-结帐< branch>新新新新旗新新新旗新新新旗新新旗新新旗200新新新新旗新新旗200新新新新新旗新新旗200新新新新旗新新旗200新新新200新新新新旗新新款: eclipse.jdt.core.prefs
注意:你可以显然使用不同于超级结帐
的别名。
查看 git ls-tree
文档,以便更好地了解。
I merged from several other branches on a big project with multiple eclipse projects, and ended up with a bunch of org.eclipse.jdt.core.prefs settings being changed, that I'd like to revert on my branch. Is there magic command I can use to 'reset' only these files, over all the eclipse projects in my git repo?
I tried something along the lines of
git checkout origin/BRANCH_WITH_CORRECT_FILES -- *org.eclipse.jdt.core.prefs
This returns
error: pathspec '*org.eclipse.jdt.core.prefs' did not match any file(s) known to git.
Is this because of the *? How can I get around it?
The problem arises from the fact that git checkout
doesn't search in subdirectories. To checkout files using asterisks you have to specify the folder where git can find them.
Nethertheless it's possible to checkout all files with a specific string in their name, regardless of their location in the repository. To achieve this we have to combine a number of commands.
git ls-tree -r --name-only <branch> | grep <pattern>
This will return a list of all files with the given pattern in their name; we pass the output of this command to git checkout
which will update the working tree accordingly.
git checkout <branch> -- $(git ls-tree -r --name-only <branch> | grep <pattern>)
And the magic is done.
For easier usage I would suggest creating an alias in your global gitconfig.
git config --global alias.super-checkout '!f() { git checkout $1 -- $(git ls-tree -r --name-only $1 | grep "$2"); }; f'
After this you can take use of the command with git super-checkout <branch> <pattern>
, or in your case:
git super-checkout origin/BRANCH_WITH_CORRECT_FILES org.eclipse.jdt.core.prefs
Note: You can obviously use a different alias than super-checkout
.
Take a look at the git ls-tree
documentation for better understanding.
这篇关于在Git中检出同名的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!