Git:如何从远程源主机更新/签出单个文件?
场景:
- 我在本地对单个文件进行一些更改,然后运行 git add 、 git commit 和 git push 将
- 文件推送到远程原始主存储库
- 我有另一个本地存储库,它是通过 Capistrano 部署的,使用该远程存储库中的“remote_cache”方法
- 现在我不想部署整个应用程序,而只是更新/签出该单个文件。
这对于 git 来说是可能的吗?我找不到任何有用的东西,也无法弄清楚。使用 SVN,我只需执行 svn up file
即可。
The scenario:
- I make some changes in a single file locally and run
git add
,git commit
andgit push
- The file is pushed to the remote origin master repository
- I have another local repository that is deployed via Capistrano with the "remote_cache" method from that remote repository
- Now I don't want to deploy the whole application but just update/checkout that single file.
Is this somehow possible with git? I wasn't able to find anything that would work nor was I able to figure it out. With SVN I just did svn up file
and voila.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
可以(在部署的存储库中)执行
fetch 将下载所有最近的更改,但不会将其放入您当前签出的代码(工作区域)中。
签出将使用下载的更改中的特定文件(
origin/master
)更新工作树。至少这对我来说适用于那些小的拼写错误修复,在这些修复中,创建一个分支等只是为了更改文件中的一个单词感觉很奇怪。
It is possible to do (in the deployed repository)
The fetch will download all the recent changes, but it will not put it in your current checked out code (working area).
The checkout will update the working tree with the particular file from the downloaded changes (
origin/master
).At least this works for me for those little small typo fixes, where it feels weird to create a branch etc just to change one word in a file.
使用 Git 2.23(2019 年 8 月)和新的(仍处于实验阶段)命令
git Restore
,参见“如何重置工作目录中的所有文件而不是暂存区域中的所有文件? ”,那就是:这个想法是:
git Restore
只处理文件,而不是像git checkout
那样处理文件和分支.请参阅“对
git checkout
感到困惑”:这就是git switch
出现)codersam 添加 在评论中:
With Git 2.23 (August 2019) and the new (still experimental) command
git restore
, seen in "How to reset all files from working directory but not from staging area?", that would be:The idea is:
git restore
only deals with files, not files and branches asgit checkout
does.See "Confused by
git checkout
": that is wheregit switch
comes in)codersam adds in the comments:
以下代码对我有用:
Following code worked for me:
简单,它对我有用
Simply It works for me
您可以做的是:
更新您的本地 git 存储库:
git fetch
构建本地分支并在其上签出:
git 分支 pouet && git checkout pouet
在此分支上应用您想要的提交:
gitcherry-pick abcdefabcdef
(abcdefabcdef 是您要应用的提交的 sha1)
What you can do is:
Update your local git repo:
git fetch
Build a local branch and checkout on it:
git branch pouet && git checkout pouet
Apply the commit you want on this branch:
git cherry-pick abcdefabcdef
(abcdefabcdef is the sha1 of the commit you want to apply)
如果是在dev分支开发,替换文件后合并master分支并push。
例如,替换 app/file.py。
If you are developing in the dev branch, merge the master branch after replacing the file and push it.
For example, replace app/file.py.
或者在您所在的分支上 git stash (如果有更改),签出 master,拉取最新更改,将该文件抓取到您的桌面(或整个应用程序)。查看您所在的分支。 Git stash 应用回您所处的状态,然后手动修复更改或拖动它替换文件。
这种方法不太酷,但如果你们想不出其他办法,它肯定有效。
Or git stash (if you have changes) on the branch you're on, checkout master, pull for the latest changes, grab that file to your desktop (or the entire app). Checkout the branch you were on. Git stash apply back to the state you were at, then fix the changes manually or drag it replacing the file.
This way is not sooooo cool but it def works if you guys can't figure anything else out.
我想我已经找到了一个简单的破解方法。
删除本地存储库上的文件(您想要从远程服务器中最新提交更新的文件),
然后执行
git pull
因为文件被删除,所以不会有冲突
I think I have found an easy hack out.
Delete the file that you have on the local repository (the file that you want updated from the latest commit in the remote server)
And then do a
git pull
Because the file is deleted, there will be no conflict