简体   繁体   English

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

[英]pull specific commit/file from git

I have made two commits in my git repository and push them to my git server 我已经在我的git存储库中进行了两次提交并将它们推送到我的git服务器

the two commits are 两个提交是

  • In first commit file A is committed 在第一个提交文件A中提交
  • In second commit file B is committed 在第二个提交文件B中提交

now on the other development server I want to pull only the first commit or FILE A from git server. 现在在另一个开发服务器上我想从git服务器只提取第一个提交或FILE A. How to do this ? 这该怎么做 ?

First, on your development server, you'll need to fetch the list of commits from the git server like this: 首先,在您的开发服务器上,您需要从git服务器获取提交列表,如下所示:

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. Cherry选择第一个提交 - 这只是从另一个分支/ repo“摘取”所选提交并将其应用于当前的本地分支。 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). 或者,拉出所有内容,然后对您想要的提交进行硬重置(在本例中是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). 硬重置有效地将您的本地分支回溯到所选的提交,将所有文件的状态更改为当时的状态(因此在这种情况下,文件B将被删除,或者返回到之前的状态提交,取决于它以前是否存在)。

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

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. 这将从repo中获取所有提交,然后在获取内容的HEAD之前检查您的本地repo到提交。 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. 这里唯一的缺点是你将处于“超级头”状态,并且必须在本地创建一个新的分支,但这不是一个大问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM