GIT:结帐到特定文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4479960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GIT: Checkout to a specific folder
提问by Rafid
I want to use something similar to:
我想使用类似的东西:
git checkout -- <path>/<file>
but I want to checkout the file to some folder I choose, rather than the overwriting the local <path>/<file>
.
但我想将文件签出到我选择的某个文件夹,而不是覆盖本地<path>/<file>
.
Any idea?
任何的想法?
采纳答案by hasen
As per Do a "git export" (like "svn export")?
根据做一个“git export”(比如“svn export”)?
You can use git checkout-index
for that, this is a low level command, if you want to export everything, you can use -a
,
您可以使用git checkout-index
它,这是一个低级命令,如果您想导出所有内容,您可以使用-a
,
git checkout-index -a -f --prefix=/destination/path/
To quote the man pages:
引用手册页:
The final "/" [on the prefix] is important. The exported name is literally just prefixed with the specified string.
最后的“/”[在前缀上] 很重要。导出的名称实际上只是以指定的字符串作为前缀。
If you want to export a certain directory, there are some tricks involved. The command only takes files, not directories. To apply it to directories, use the 'find' command and pipe the output to git.
如果要导出某个目录,则涉及一些技巧。该命令只需要文件,而不是目录。要将其应用于目录,请使用“find”命令并将输出通过管道传输到 git。
find dirname -print0 | git checkout-index --prefix=/path-to/dest/ -f -z --stdin
Also from the man pages:
同样来自手册页:
Intuitiveness is not the goal here. Repeatability is.
直觉不是这里的目标。重复性是。
回答by Adrian Macneil
Another solution which is a bit cleaner - just specify a different work tree.
另一个更简洁的解决方案 - 只需指定一个不同的工作树。
To checkout everything from your HEAD (not index) to a specific out directory:
要将 HEAD(不是索引)中的所有内容签出到特定输出目录:
git --work-tree=/path/to/outputdir checkout HEAD -- .
To checkout a subdirectory or file from your HEAD to a specific directory:
要将子目录或文件从 HEAD 签出到特定目录:
git --work-tree=/path/to/outputdir checkout HEAD -- subdirname
回答by Tobu
For a single file:
对于单个文件:
git show HEAD:abspath/to/file > file.copy
回答by proski
The above solutions didn't work for me because I needed to check out a specific tagged version of the tree. That's how cvs export
is meant to be used, by the way. git checkout-index
doesn't take the tag argument, as it checks out files from index. git checkout <tag>
would change the index regardless of the work tree, so I would need to reset the original tree. The solution that worked for me was to clone the repository. Shared clone is quite fast and doesn't take much extra space. The .git
directory can be removed if desired.
上述解决方案对我不起作用,因为我需要检查树的特定标记版本。cvs export
顺便说一下,这就是要使用的方式。 git checkout-index
不接受 tag 参数,因为它从 index.html 中检出文件。 git checkout <tag>
无论工作树如何,都会更改索引,因此我需要重置原始树。对我有用的解决方案是克隆存储库。共享克隆速度非常快,并且不会占用太多额外空间。.git
如果需要,可以删除该目录。
git clone --shared --no-checkout <repository> <destination>
cd <destination>
git checkout <tag>
rm -rf .git
Newer versions of git should support git clone --branch <tag>
to check out the specified tag automatically:
较新版本的 git 应该支持git clone --branch <tag>
自动检出指定的标签:
git clone --shared --branch <tag> <repository> <destination>
rm -rf <destination>/.git
回答by itsnikolay
If you're working under your feature and don't want to checkout back to master, you can run:
如果您正在使用您的功能并且不想结帐回到主人,您可以运行:
cd ./myrepo
git worktree add ../myrepo_master master
git worktree remove ../myrepo_master
It will create ../myrepo_master
directory with master
branch commits, where you can continue work
它将创建../myrepo_master
带有master
分支提交的目录,您可以在其中继续工作
回答by Shaun Luttin
Adrian's answer threw "fatal: This operation must be run in a work tree." The following is what worked for us.
Adrian 的回答是“致命的:此操作必须在工作树中运行”。以下是对我们有用的内容。
git worktree add <new-dir> --no-checkout --detach
cd <new-dir>
git checkout <some-ref> -- <existing-dir>
Notes:
笔记:
--no-checkout
Do not checkout anything into the new worktree.--detach
Do not create a new branch for the new worktree.<some-ref>
works with any ref, for instance, it works withHEAD~1
.- Cleanup with
git worktree prune
.
--no-checkout
不要将任何东西签出到新的工作树中。--detach
不要为新工作树创建新分支。<some-ref>
适用于任何 ref,例如,它适用于HEAD~1
.- 使用
git worktree prune
.
回答by fdietze
I'm using this alias for checking out a branch in a temporary directory:
我正在使用这个别名来检出临时目录中的一个分支:
[alias]
cot = "!TEMP=$(mktemp -d); f() { git worktree prune && git worktree add $TEMP && zsh -c \"cd $TEMP; zsh\";}; f" # checkout branch in temporary directory
Usage:
用法:
git cot mybranch
You are then dropped in a new shell in the temporary directory where you can work on the branch. You can even use git commands in this directory.
然后,您将被放置在临时目录中的一个新 shell 中,您可以在该目录中处理分支。您甚至可以在此目录中使用 git 命令。
When you're done, delete the directory and run:
完成后,删除目录并运行:
git worktree prune
This is also done automatically in the alias, before adding a new worktree.
在添加新工作树之前,这也在别名中自动完成。
回答by dqthe
Use git archive branch-index | tar -x -C your-folder-on-PC
to clone a branch to another folder. I think, then you can copy any file that you need
用于git archive branch-index | tar -x -C your-folder-on-PC
将分支克隆到另一个文件夹。我想,然后你可以复制你需要的任何文件
回答by cb0
I defined an git alias to achieve just this (before I found this question).
我定义了一个 git 别名来实现这一点(在我发现这个问题之前)。
It's a short bash function which saves the current path, switch to the git repo, does a checkout and return where it started.
这是一个简短的 bash 函数,它保存当前路径、切换到 git 存储库、进行结帐并返回它开始的位置。
git checkto develop ~/my_project_git
git checkto 开发 ~/my_project_git
This e.g. would checkout the develop branch into "~/my_project_git" directory.
例如,这会将开发分支检出到“~/my_project_git”目录中。
This is the alias code inside ~/.gitconfig
:
这是里面的别名代码~/.gitconfig
:
[alias]
checkTo = "!f(){ [ -z \"\" ] && echo \"Need to specify branch.\" && \
exit 1; [ -z \"\" ] && echo \"Need to specify target\
dir\" && exit 2; cDir=\"$(pwd)\"; cd \"\"; \
git checkout \"\"; cd \"$cDir\"; };f"