我如何从一个git回购签出一个文件?


当前回答

在GIT 1.7.2.2中工作

例如,你有一个带有分支branch1和branch32的远程some_remote

所以要签出一个特定的文件,你可以调用这些命令:

git checkout remote/branch path/to/file

举个例子,大概是这样的

git checkout some_remote/branch32 conf/en/myscript.conf
git checkout some_remote/branch1 conf/fr/load.wav

这个检出命令将整个文件结构conf/en和conf/fr复制到当前目录,在这里您调用这些命令(当然,我假设您在之前的某个时候运行了git init)

其他回答

这里是一个完整的解决方案,只拉和推一个特定的文件在git仓库:

首先,你需要克隆git存储库,并给出一个特殊提示——不签出

git clone --no-checkout <git url>

下一步是用下面的命令清除索引中的非暂存文件:

git reset

现在你可以开始拉出你想要修改的文件了:

git checkout origin/master <path to file>

现在,存储库文件夹包含您可以立即开始编辑的文件。编辑完成后,您需要执行简单而熟悉的命令序列。

git add <path to file>
git commit -m <message text>
git push

在GIT 1.7.2.2中工作

例如,你有一个带有分支branch1和branch32的远程some_remote

所以要签出一个特定的文件,你可以调用这些命令:

git checkout remote/branch path/to/file

举个例子,大概是这样的

git checkout some_remote/branch32 conf/en/myscript.conf
git checkout some_remote/branch1 conf/fr/load.wav

这个检出命令将整个文件结构conf/en和conf/fr复制到当前目录,在这里您调用这些命令(当然,我假设您在之前的某个时候运行了git init)

非常简单:

git checkout from-branch-name -- path/to/the/file/you/want

这将不会签出from-branch-name分支。您将停留在您所在的分支上,并且只有该文件将从指定的分支签出。

下面是git-checkout手册的相关部分

git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...
       When <paths> or --patch are given, git checkout does not switch
       branches. It updates the named paths in the working tree from the
       index file or from a named <tree-ish> (most often a commit). In
       this case, the -b and --track options are meaningless and giving
       either of them results in an error. The <tree-ish> argument can be
       used to specify a specific tree-ish (i.e. commit, tag or tree) to
       update the index for the given paths before updating the working
       tree.

向Ariejan de Vroom致敬,他在这篇博客文章中教会了我这些。

是的,你可以通过这个命令下载一个特定的文件

wget -o <DesiredFileName>  <Git FilePath>\?token\=<personalGitToken>

例子

wget -o javascript-test-automation.md https://github.com/akashgupta03/awesome-test-automation/blob/master/javascript-test-automation.md\?token\=<githubPersonalTone>

Git checkout branch_or_version——path/file

示例:git checkout HEAD——main.c