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

发布于 2024-09-11 13:47:59 字数 301 浏览 5 评论 0原文

场景:

  1. 我在本地对单个文件进行一些更改,然后运行 ​​git add 、 git commit 和 git push 将
  2. 文件推送到远程原始主存储库
  3. 我有另一个本地存储库,它是通过 Capistrano 部署的,使用该远程存储库中的“remote_cache”方法
  4. 现在我不想部署整个应用程序,而只是更新/签出该单个文件。

这对于 git 来说是可能的吗?我找不到任何有用的东西,也无法弄清楚。使用 SVN,我只需执行 svn up file 即可。

The scenario:

  1. I make some changes in a single file locally and run git add, git commit and 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.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

快乐很简单 2024-09-18 13:47:59

可以(在部署的存储库中)执行

git fetch
git checkout origin/master -- path/to/file

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

签出将使用下载的更改中的特定文件(origin/master)更新工作树。

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

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

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.

梦中楼上月下 2024-09-18 13:47:59

使用 Git 2.23(2019 年 8 月)和新的(仍处于实验阶段)命令 git Restore,参见“如何重置工作目录中的所有文件而不是暂存区域中的所有文件? ”,那就是:

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

这个想法是: git Restore 只处理文件,而不是像 git checkout 那样处理文件分支.
请参阅“git checkout 感到困惑”:这就是git switch 出现)


codersam 添加 在评论中

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

git Restore -s上游/master --路径/到/文件

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 fetch
git restore -s origin/master -- path/to/file

The idea is: git restore only deals with files, not files and branches as git checkout does.
See "Confused by git checkout": that is where git switch comes in)


codersam adds in the comments:

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
蘸点软妹酱 2024-09-18 13:47:59

以下代码对我有用:

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

Following code worked for me:

git fetch
git checkout <branch from which file needs to be fetched> <filepath> 
笙痞 2024-09-18 13:47:59

简单,它对我有用

git checkout origin/develop file_name.php

Simply It works for me

git checkout origin/develop file_name.php
丶视觉 2024-09-18 13:47:59
git archive --format=zip --remote=ssh://<user>@<host>/repos/<repo name> <tag or HEAD> <filename> > <output file name>.zip
git archive --format=zip --remote=ssh://<user>@<host>/repos/<repo name> <tag or HEAD> <filename> > <output file name>.zip
七七 2024-09-18 13:47:59

您可以做的是:

  1. 更新您的本地 git 存储库:

    git fetch

  2. 构建本地分支并在其上签出:

    git 分支 pouet && git checkout pouet

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

    gitcherry-pick abcdefabcdef

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

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)

只为守护你 2024-09-18 13:47:59
git checkout master
git pull --rebase
git checkout dev
git checkout master ./app/file.py

如果是在dev分支开发,替换文件后合并master分支并push。
例如,替换 app/file.py。

git checkout master
git pull --rebase
git checkout dev
git checkout master ./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.

茶底世界 2024-09-18 13:47:59

或者在您所在的分支上 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.

恬淡成诗 2024-09-18 13:47:59

我想我已经找到了一个简单的破解方法。

删除本地存储库上的文件(您想要从远程服务器中最新提交更新的文件),

然后执行 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文