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


当前回答

这听起来像是你试图从集中式版本控制中继承一个想法,而git本质上不是这样的——它是分布式的。如果你想使用git存储库,你可以克隆它。然后,您就拥有了工作树的所有内容和所有历史记录(好吧,至少是指向当前分支顶端的所有内容),而不仅仅是单个文件或单个提交的快照。

 git clone /path/to/repo
 git clone git://url/of/repo
 git clone http://url/of/repo

其他回答

非常简单:

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致敬,他在这篇博客文章中教会了我这些。

如果你已经有了一个git回购的副本,你可以使用git日志签出一个文件的版本来找出哈希id(例如3cdc61015724f9965575ba954c8cd4232c8b42e4),然后你只需输入:

git checkout hash-id path-to-file

下面是一个实际的例子:

git checkout 3cdc61015724f9965575ba954c8cd4232c8b42e4 /var/www/css/page.css

这里是一个完整的解决方案,只拉和推一个特定的文件在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

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

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 <other-branch>—<single-file>适用于我的Git .2.37.1。 然而,该文件是(git-魔术)提交的,我不能看到正确的git diff。 然后运行git restore——staging db/structure。SQL来取消它。

这样我就有了我想要的确切版本的文件,我可以看到该文件与其他版本的区别。