本文翻译自:Git: How to update/checkout a single file from remote origin master?
The scenario: 场景:
- I make some changes in a single file locally and run
git add
,git commit
andgit push
我在本地单个文件中进行了一些更改并运行git add
,git commit
和git 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 我有另一个本地存储库,通过Capistrano与该远程存储库中的“remote_cache”方法一起部署
- Now I don't want to deploy the whole application but just update/checkout that single file. 现在我不想部署整个应用程序,只需更新/签出该单个文件。
Please, is this somehow possible with git? 拜托,这对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. 使用SVN我只是做svn up file
和瞧。
I'll be glad for any help, thanks! 我会很高兴得到任何帮助,谢谢!
#1楼
参考:https://stackoom.com/question/DzRr/Git-如何从远程源主服务器更新-签出单个文件
#2楼
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). 或者你正在使用的分支上的git stash(如果你有更改),checkout master,拉取最新的更改,将该文件抓取到你的桌面(或整个应用程序)。 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. Git stash应用回您所处的状态,然后手动修复更改或拖动替换文件。
This way is not sooooo cool but it def works if you guys can't figure anything else out. 这种方式并不太酷,但如果你们不能解决任何问题,那么def就可以了。
#3楼
git archive --format=zip --remote=ssh://<user>@<host>/repos/<repo name> <tag or HEAD> <filename> > <output file name>.zip
#4楼
What you can do is: 你能做的是:
Update your local git repo: 更新您当地的git仓库:
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) (abcdefabcdef是您要应用的提交的sha1)
#5楼
It is possible to do (in the deployed repository) 可以这样做(在已部署的存储库中)
git fetch
git checkout origin/master -- path/to/file
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
). 结帐将使用下载的更改( 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. 至少这对我来说对于那些小小的错字修复,在创建分支等只是为了改变文件中的一个单词感觉很奇怪。
#6楼
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
然后做一个git pull
Because the file is deleted, there will be no conflict 由于文件已删除,因此不会发生冲突