从 git 中提取特定的提交/文件

发布于 2024-12-28 04:35:48 字数 197 浏览 0 评论 0原文

我已经在我的 git 存储库中进行了两次提交,并将它们推送到我的 git 服务器

这两个提交是

  • 在第一个提交文件 A 中已提交
  • 在第二个提交文件 B 中

现在已在我想要的其他开发服务器上提交仅从 git 服务器中提取第一个提交或文件 A。如何做到这一点?

I have made two commits in my git repository and push them to my git server

the two commits are

  • In first commit file A is committed
  • In second commit file B is committed

now on the other development server I want to pull only the first commit or FILE A from git server. How to do this ?

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

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

发布评论

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

评论(2

别挽留 2025-01-04 04:35:48

首先,在你的开发服务器上,你需要从 git 服务器获取提交列表,如下所示:

git fetch origin master (or whatever branch you need)

然后有几个选项可以实现你想要的:

Cherry pick the first commit - 这只是“提取”所选的提交来自另一个分支/存储库并将其应用到您当前的本地分支。它可能非常有用,但应谨慎使用(见下文)。

git cherry-pick  <hash-of-commit-you-want>

在这种特殊情况下,您可以执行

git cherry-pick FETCH_HEAD^ (gets commit before the HEAD of what's been fetched)

Or,拉取所有内容,然后对您想要的提交进行硬重置(在本例中是 HEAD 之前的提交)。硬重置有效地将本地分支及时倒回到所选提交,将所有文件的状态更改为当时的状态(因此在这种情况下,文件 B 要么被删除,要么回到之前的状态)提交,取决于它以前是否存在)。

git pull
git reset --hard HEAD^ (or git reset --hard <hash-of-commit-you-want>)

我更喜欢第二种选择,因为如果你不小心的话,挑选可能会产生一些连锁反应。我相信它会为提交创建一个新的哈希,因此精心挑选的提交和原始提交并不相同。恐怕我现在没有时间阅读并确认到底有哪些陷阱,但如果您决定使用它,我强烈建议您亲自调查一下。

编辑 - 另一个解决方案(假设这是一个实时服务器,并且文件 B 在任何时候出现在服务器上都是不可接受的)是执行以下操作:

git fetch origin master
git checkout FETCH_HEAD^

这会从存储库中获取所有提交,然后检查本地存储库到所获取内容的 HEAD 之前的提交。这里唯一的缺点是,您将处于“分离头”状态,并且必须在本地创建一个新分支,但这不是一个大问题。

First, on your development server, you'll need to fetch the list of commits from the git server like this:

git fetch origin master (or whatever branch you need)

Then there are a few options to achieve what you want:

Cherry pick the first commit - this simply 'plucks' the chosen commit from another branch/repo and applies it to your current local branch. It can be very useful, but should be used with caution (see below).

git cherry-pick  <hash-of-commit-you-want>

In this particular case, you could do

git cherry-pick FETCH_HEAD^ (gets commit before the HEAD of what's been fetched)

Or, pull everything and then do a hard reset to the commit you want (in this case the one just before HEAD). A hard reset effectively rewinds your local branch back in time to the chosen commit, changing the state of all files to how they were at that time (so in this case File B would either be deleted, or go back to how it was before either commit, depending on whether or not it previously existed).

git pull
git reset --hard HEAD^ (or git reset --hard <hash-of-commit-you-want>)

I would prefer the second option as cherry-picking can have some knock on effects if you're not careful with it. I believe it creates a new hash for the commit, so the cherry-picked commit and the original commit aren't identical. I'm afraid I don't have time right now to read up on this and confirm exactly what the pitfalls are, but I would strongly recommend that you investigate it for yourself if you decide to use it.

EDIT - Another solution (given that this is a live server and that it's not acceptable for File B to appear on the server at any point) is to do the following:

git fetch origin master
git checkout FETCH_HEAD^

This fetches all commits from the repo, and then checkes out your local repo to the commit before the HEAD of what was fetched. The only downside here is that you will then be in 'detached head' state and will have to create a new branch locally, but that's not a big problem.

戏舞 2025-01-04 04:35:48

这没有多大意义 - 如果您提交了文件,无论如何您都会将它们保存在您的存储库中。

也许这就是你想要的

git checkout -- FileAOnly

或者

git checkout origin/master -- FileAonly

This doesnt make to much sense - if you commited the files you have them in your repo anyway.

Maybe this is what you want

git checkout -- FileAOnly

Or

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