从 git 中提取特定的提交/文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8921739/
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
pull specific commit/file from git
提问by Dau
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
- In second commit file B is committed
- 在第一个提交文件 A 被提交
- 在第二个提交文件 B 被提交
now on the other development server I want to pull only the first commit or FILE A from git server. How to do this ?
现在在另一台开发服务器上,我只想从 git 服务器中提取第一个提交或文件 A。这该怎么做 ?
回答by Will Pragnell
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. It can be very useful, but should be used with caution (see below).
Cherry 选择第一个提交 - 这只是从另一个分支/存储库“提取”所选提交并将其应用于您当前的本地分支。它可能非常有用,但应谨慎使用(见下文)。
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).
或者,拉取所有内容,然后硬重置您想要的提交(在本例中是 HEAD 之前的提交)。硬重置有效地将您的本地分支及时回退到所选提交,将所有文件的状态更改为当时的状态(因此在这种情况下,文件 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. 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.
这将从存储库中获取所有提交,然后在获取内容的 HEAD 之前将本地存储库检出到提交中。这里唯一的缺点是您将处于“分离头”状态,并且必须在本地创建一个新分支,但这不是大问题。
回答by Adrian Cornish
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