场景:
我在本地对单个文件进行了一些更改并运行 git add、git commit 和 git push 文件被推送到远程源主存储库我有另一个本地存储库,它通过 Capistrano 使用远程存储库中的“remote_cache”方法部署现在我不想部署整个应用程序,而只是更新/签出该单个文件。
这对git有可能吗?我找不到任何可行的方法,也无法弄清楚。使用 SVN,我只是做了 svn up file
,瞧。
git restore -s origin/master -- path/to/file
。请参阅my answer below。
可以做(在部署的存储库中)
git fetch
git checkout origin/master -- path/to/file
提取将下载所有最近的更改,但不会将其放入您当前签出的代码(工作区)中。
检出将使用下载的更改 (origin/master
) 中的特定文件更新工作树。
至少这对我来说适用于那些小的错字修复,在那里创建分支等只是为了更改文件中的一个单词感觉很奇怪。
以下代码对我有用:
git fetch
git checkout <branch from which file needs to be fetched> <filepath>
git fetch <remote>; git checkout <remote>/<branch> -- <file>
用于非来源遥控器
使用 Git 2.23(2019 年 8 月)和新的(仍处于试验阶段)命令 git restore
(在“How to reset all files from working directory but not from staging area?”中可见),将是:
git fetch
git restore -s origin/master -- path/to/file
这个想法是:git restore
只处理文件,而不像 git checkout
那样处理文件和分支。
请参阅“Confused by git checkout
”:这就是 git switch
强>进来)
就我而言,我想从上游(我从中分叉)获取数据。所以就改成: git restore -s upstream/master -- path/to/file
git restore -s upstream/master -- path/to/file
restore
在我的测试中更好,因为它把文件放在工作区而不是像 checkout
那样的暂存区。
git archive --format=zip --remote=ssh://<user>@<host>/repos/<repo name> <tag or HEAD> <filename> > <output file name>.zip
git archive --remote=https://github.com/git/git.git master:git/contrib/completion git-completion.bash | tar -x
给我错误消息:fatal: Operation not supported by protocol.
tar
:s --to-stdout
、git archive --remote="gitolite3@<host>:<repo>" <tag> <file> | tar xf - --to-stdout
结合使用很好
简直对我有用
git checkout origin/develop file_name.php
你可以做的是:
更新本地 git repo: git fetch 构建本地分支并在其上结帐: git branch pouet && git checkout pouet 在此分支上应用您想要的提交: git cherry-pick abcdefabcdef (abcdefabcdef 是您要应用的提交的 sha1 )
git checkout -b pouet
。
或者在你所在的分支上 git stash(如果你有更改),结帐 master,拉取最新的更改,将该文件抓取到你的桌面(或整个应用程序)。检查您所在的分支。 Git stash 应用回您所处的状态,然后手动修复更改或将其拖动以替换文件。
这种方式不是很酷,但如果你们想不出其他任何东西,它就可以正常工作。
我给你的中文步骤很烂,我给你的步骤是:
git checkout master
git pull --rebase
git checkout dev
git checkout master ./app/file.py
机器翻译:如果是在dev分支中开发,替换文件后合并master分支并push。例如,替换 app/file.py。
我想我找到了一个简单的破解方法。
删除本地存储库中的文件(要从远程服务器中的最新提交更新的文件)
然后执行 git pull
因为文件被删除了,所以不会有冲突
git checkout origin/master -- path/to/folder/*
(注意末尾的 *)。