Git:如何从远程源主机更新/检出单个文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3334475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:49:24  来源:igfitidea点击:

Git: How to update/checkout a single file from remote origin master?

gitgit-checkout

提问by foresth

The scenario:

场景:

  1. I make some changes in a single file locally and run git add, git commitand git push
  2. The file is pushed to the remote origin master repository
  3. I have another local repository that is deployed via Capistrano with the "remote_cache" method from that remote repository
  4. Now I don't want to deploy the whole application but just update/checkout that single file.
  1. 我在本地的单个文件中进行了一些更改并运行git addgit commit然后git push
  2. 文件被推送到远程源主存储库
  3. 我有另一个本地存储库,通过 Capistrano 使用该远程存储库中的“remote_cache”方法进行部署
  4. 现在我不想部署整个应用程序,而只想更新/检出该单个文件。

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 fileand voila.

这对 git 有什么可能吗?我找不到任何可行的方法,也无法弄清楚。使用 SVN 我刚刚做到了svn up file,瞧。

回答by qzio

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).

fetch 将下载所有最近的更改,但不会将其放入您当前检出的代码(工作区)中。

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.

至少这对我来说适用于那些小的拼写错误修复,在那里创建分支等只是为了更改文件中的一个单词感觉很奇怪。

回答by Shagun Pruthi

Following code worked for me:

以下代码对我有用:

     git fetch
     git checkout <branch from which file needs to be fetched> <filepath> 

回答by Dmitry R

git archive --format=zip --remote=ssh://<user>@<host>/repos/<repo name> <tag or HEAD> <filename> > <output file name>.zip

回答by VonC

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:

使用 Git 2.23(2019 年 8 月)和新的(仍然是实验性的)命令git restore,见“如何从工作目录而不是从暂存区重置所有文件?”,这将是:

git fetch
git restore -s origin/master -- path/to/file

The idea is: git restoreonly deals with files, not files andbranches as git checkoutdoes.
See "Confused by git checkout": that is where git switchcomes in)

这个想法是:git restore只处理文件,而不是文件分支git checkout
见“困惑git checkout”:这就是git switch进来的地方)



codersamadds in the comments:

codersam在评论中添加:

in my case I wanted to get the data from my upstream(from which I forked).
So just changed to:

git restore -s upstream/master -- path/to/file

就我而言,我想从上游从中分叉)获取数据
所以只是改为:

git restore -s upstream/master -- path/to/file

回答by jag

What you can do is:

你可以做的是:

  1. Update your local git repo:

    git fetch

  2. Build a local branch and checkout on it:

    git branch pouet && git checkout pouet

  3. Apply the commit you want on this branch:

    git cherry-pick abcdefabcdef

    (abcdefabcdef is the sha1 of the commit you want to apply)

  1. 更新您的本地 git 仓库:

    git fetch

  2. 建立一个本地分支并在其上结帐:

    git branch pouet && git checkout pouet

  3. 在此分支上应用您想要的提交:

    git cherry-pick abcdefabcdef

    (abcdefabcdef 是您要应用的提交的 sha1)

回答by jahrichie

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.

或者在您所在的分支上 git stash (如果您有更改),结帐 master,拉取最新更改,将该文件抓取到您的桌面(或整个应用程序)。结帐您所在的分支。Git stash 应用回您所在的状态,然后手动修复更改或拖动它替换文件。

This way is not sooooo cool but it def works if you guys can't figure anything else out.

这种方式不是很酷,但如果你们想不出其他任何东西,它绝对有效。

回答by Viswadeep Sarangi

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

因为文件被删除了,不会有冲突