有没有办法从 master 中签出落后于许多 Git 提交的单个文件?

发布于 2024-11-14 17:07:54 字数 531 浏览 4 评论 0原文

我有一个代码工作流程,依赖于将开发机器上的存储库克隆的更改拉入存储库主控,然后使用“git fetch”和“git merge”之类的方法更新生产机器上的存储库主控的副本。

我遇到了一个问题,我已经提交了一些东西来掌握,但由于代码冻结,这些更改从未被拉入生产机器中。为了摆脱代码冻结,我需要更新一个也已提交给 master 的文件。

此时,生产机器都落后于主存储库大约 10 次提交。这只是我想拉入生产机器的最新提交。当我需要的只是快进一个文件时,“git fetch && git merge”将快进我的整个存储库。

挑选单个文件的最佳方法是什么?如果我“git checkout file.ext”,那么它会从本地存储库副本更新,而不是更新的主版本。我想让我的本地副本“了解”文件的最新版本,而不更新其他所有内容。

我的想法是执行“git fetch”来更新存储库,然后执行“git checkout file.ext”来更新一个文件。这是做我想做的事情的最佳方式吗?如果没有,有人可以建议我如何从落后于许多 Git 提交的 master 中签出单个更新的文件吗?

I've got a code work flow dependent on pulling changes from a repo clones on dev machines into a repo master, then updating copies of that repo master on prod machines using something like a "git fetch" followed by a "git merge."

I've run into an issue where I've committed things to master, but due to a code freeze those changes were never pulled into the prod machines. To get out of the code freeze, I need to update a single file that's also been commited to master.

At this point, the prod machines are all behind the master repo by something like 10 commits. It is only the most recent commit that I want to pull into the prod machines. A "git fetch && git merge" is going to fast-forward my whole repo when all I need is a single file fast-forwarded.

What's the best way to cherry-pick that single file? If I "git checkout file.ext" then it updates from the local repo copy and not the updated master. I'd like to get my local copy to "know" about the most recent version of the file without updating everything else.

My idea was to do a "git fetch" to update the repo and then a "git checkout file.ext" to update just one file. Is that the best way of doing what I want to do? If not, can someone please advise me on a way to checkout a single updated file from a master that's behind on many Git commits?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

拍不死你 2024-11-21 17:07:54

尝试类似的东西

git fetch
git checkout master -- src/your/file.txt

Try something like

git fetch
git checkout master -- src/your/file.txt
静谧 2024-11-21 17:07:54

您只需要在提交哈希之后引用该文件即可。要获得所需的哈希值,您可以运行

git log remoteRepo/remotebranch

而不是包含要合并到本地存储库的单个文件的版本的提交的哈希值。当你有了这个哈希值而不是运行 git fetch && git merge 你应该运行:

git fetch && git checkout <HASH> <PATH_TO_SINGLE_FILE>

现在你的工作分支中应该有更新的文件。

问候,
勒内

you just need to reference the file after the commit hash. To get the desired hash you can run

git log remoteRepo/remotebranch

and not the hash of the commit which contains the version of the single file you want to merge into your local repository. And after you have this hash instead of running git fetch && git merge you should run:

git fetch && git checkout <HASH> <PATH_TO_SINGLE_FILE>

now you should have the updated file in your working branch.

regards,
René

谈下烟灰 2024-11-21 17:07:54

注意:此解决方案不使用任何 git 特定命令。
可能有一个 git 命令可以完成所有这些工作。

在版本控制之外的某个地方制作更新后的单个文件的副本。
转到旧版本(生产机器所在的版本)。
将更新后的单个文件复制回修订控制。
签入将创建一个新分支。
检查生产机器的新分支。

Note : this solution does not use any of git specific commands.
There may be a single git command to do all this work.

Make a copy of the updated single file somewhere outside revision control.
Go to old revision (the one that production machines are on).
Copy the the updated single file back to revision control.
Checking in will create a new branch.
Checkout new branch to the production machines.

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