从git中提取特定的提交/文件 [英] pull specific commit/file from git

查看:151
本文介绍了从git中提取特定的提交/文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的git仓库中做了两次提交,并将它们推送到我的git服务器上。


$ b 这两个提交都是




  • 第一个提交文件A已提交

  • 第二个提交文件B已提交



现在在另一个开发服务器上,我只想从git服务器中提取第一个提交或FILE A.如何做到这一点?

解决方案

首先,在您的开发服务器上,您需要从git中获取提交列表服务器是这样的:

$ g $ f $ g $ g $ fetch origin master(或者你需要的任何分支)



然后有几个选项可以实现您想要的功能:



Cherry选择第一个提交 - 这只是'从另一个分支/回购'中选择提交并将其应用到当前的本地分支。它可以是非常有用的,但应谨慎使用(见下文)。

  git cherry-pick< hash-of -commit任您想> 

在这种情况下,您可以做

  git cherry-pick FETCH_HEAD ^(在获取内容的头部之前获取提交)

或者,拉出所有内容,然后重新执行所需的提交(在这种情况下,就是在HEAD之前)。硬重置有效地将本地分支倒回到选定的提交中,将所有文件的状态更改为当时的状态(因此,在这种情况下,文件B将被删除,或返回到之前的状态提交,取决于它以前是否存在)。

  git pull 
git reset --hard HEAD ^(或者git reset --hard<提交你想要的散列>)

我会更喜欢第二种选择,因为如果您不小心,樱桃采摘可能会产生一些敲击效果。我相信它会为提交创建一个新的哈希,所以樱桃选择的提交和原始提交并不完全相同。恐怕我现在没有时间来阅读这些内容,并确切地确定陷阱是什么,但是我强烈建议您自己调查一下,如果您决定使用它。



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

  git fetch origin master 
git checkout FETCH_HEAD ^

这将从回购中提取所有提交,然后在提取内容的HEAD之前检出本地回购。这里唯一的缺点是,你将处于独立头状态,并且必须在本地创建一个新分支,但这不是一个大问题。


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 ?

解决方案

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.

这篇关于从git中提取特定的提交/文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆