git 如何从 GitHub 项目中获得某些提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7832770/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:07:28  来源:igfitidea点击:

How to get certain commit from GitHub project

gitgithub

提问by w00

I need to download the Facebook API from GitHub. Normally, I just click on the 'Downloads" tab to download the latest source code. In this case, I need an older commit: 91f256424531030a454548693c3a6ca49ca3f35a, but I have no idea how to get the entire project from that commit...

我需要从GitHub下载 Facebook API 。通常,我只需单击“下载”选项卡即可下载最新的源代码。在这种情况下,我需要一个较旧的提交:91f256424531030a454548693c3a6ca49ca3f35a,但我不知道如何从该提交中获取整个项目...

Can someone please tell me how to do this?

有人可以告诉我怎么做吗?

(BTW, im on a mac. Don't know if that makes any difference).

(顺便说一句,我在 mac 上。不知道这是否有任何区别)。

回答by Mark Longair

First, clone the repository using git, e.g. with:

首先,使用 git 克隆存储库,例如:

git clone git://github.com/facebook/facebook-ios-sdk.git

That downloads the complete history of the repository, so you can switch to any version. Next, change into the newly cloned repository:

这会下载存储库的完整历史记录,因此您可以切换到任何版本。接下来,切换到新克隆的存储库:

cd facebook-ios-sdk

... and use git checkout <COMMIT>to change to the right commit:

...并用于git checkout <COMMIT>更改为正确的提交:

git checkout 91f25642453

That will give you a warning, since you're no longer on a branch, and have switched directly to a particular version. (This is known as "detached HEAD" state.) Since it sounds as if you only want to use this SDK, rather than actively develop it, this isn't something you need to worry about, unless you're interested in finding out more about how git works.

这会给你一个警告,因为你不再在一个分支上,而是直接切换到一个特定的版本。(这被称为“分离的 HEAD”状态。)因为听起来好像您只想使用这个 SDK,而不是积极开发它,所以这不是您需要担心的,除非您有兴趣了解更多关于 git 如何工作的信息。

回答by Sivan

I don't know if it was there when you had posted this question, but the best and easiest way to download a commit is to click on the commitstab when viewing a repository. Then instead of clicking on the commit name, click on Browse the repository at this point in the historybutton with <>symbol to the right side of the commit name/message, and finally on the Download ZIPbutton that comes when you click Clone or Downloadbutton.

我不知道当你发布这个问题时它是否在那里,但下载提交的最好和最简单的方法是在查看存储库时单击提交选项卡。然后,而不是单击提交名称,此时单击历史按钮中的浏览存储库,提交名称/消息右侧带有<>符号,最后单击单击克隆时出现的下载 ZIP按钮或下载按钮。

I hope it helps you guys.

我希望它可以帮助你们。

回答by Suraj K Thomas

Sivan's answer in gif enter image description here

Sivan 的 gif 回答 在此处输入图片说明

1.Click on commits in github

1.点击github中的commits

2.Select Browse code on the right side of each commit

2.选择每次提交右侧的浏览代码

3.Click on download zip , which will download source code at that point of time of commit

3.点击下载zip,会在提交时下载源代码

回答by user1767754

To just download a commit using the 7-digit SHA1 short form do:

要使用 7 位 SHA1 简短格式下载提交,请执行以下操作:

Working Example:

工作示例:

https://github.com/python/cpython/archive/31af650.zip  

Description:

描述:

 `https://github.com/username/projectname/archive/commitshakey.zip`

If you have the long hash key 31af650ee25f65794b75d4dfefed6fe4758781c1, just get the first 7 chars 31af650. It's the default for GitHub.

如果您有长哈希键31af650ee25f65794b75d4dfefed6fe4758781c1,只需获取前 7 个字符31af650。这是 GitHub 的默认设置。

回答by Jeff

The easiest way that I found to recover a lost commit (that only exists on github and not locally) is to create a new branch that includes this commit.

我发现恢复丢失提交(仅存在于 github 上而不是本地)的最简单方法是创建一个包含此提交的新分支。

  1. Have the commit open (url like: github.com/org/repo/commit/long-commit-sha)
  2. Click "Browse Files" on the top right
  3. Click the dropdown "Tree: short-sha..." on the top left
  4. Type in a new branch name
  5. git pullthe new branch down to local
  1. 打开提交(网址如:github.com/org/repo/commit/long-commit-sha)
  2. 点击右上角的“浏览文件”
  3. 点击左上角的下拉菜单“Tree: short-sha...”
  4. 输入新的分支名称
  5. git pull新分支到本地

回答by Id2ndR

The question title is ambiguous.

问题标题不明确。

回答by Benjamin Basmaci

As addition to the accepted answer:

除了已接受的答案:

To see the hashes you need to use the suggested command "git checkout hash", you can use git log. Hoewever, depending on what you need, there is an easier way than copy/pasting hashes.

要查看您需要使用建议的命令“git checkout hash”的哈希值,您可以使用git log. 然而,根据您的需要,有一种比复制/粘贴哈希更简单的方法。

You can use git log --onelineto read many commit messages in a more compressed format.

您可以使用git log --oneline更压缩的格式读取许多提交消息。

Lets say you see this a one-line list of the commits with minimal information and only partly visible hashes:

假设您看到这是一个提交的单行列表,其中包含最少的信息和只有部分可见的哈希值:

hash111 (HEAD -> master, origin/master, origin/HEAD)
hash222 last commit
hash333 I want this one
hash444 did something
....

If you want last commit, you can use git checkout master^. The ^gives you the commit beforethe master. So hash222.

如果你愿意last commit,你可以使用git checkout master^. 在^给你承诺之前的主人。所以hash222

If you want the n-th last commit, you can use git checkout master~n. For example, using git checkout master~2would give you the commit hash333.

如果你想要第 n 个最后一次提交,你可以使用git checkout master~n. 例如, usinggit checkout master~2会给你 commit hash333

回答by handle

Instead of navigating through the commits, you can also hit the ykey (Github Help, Keyboard Shortcuts) to get the "permalink" for the current revision / commit.
This will change the URL from https://github.com/<user>/<repository>(master / HEAD) to https://github.com/<user>/<repository>/tree/<commit id>.

除了浏览提交之外,您还可以点击y键(Github 帮助键盘快捷键)来获取当前修订版/提交的“永久链接”。
这会将 URL 从https://github.com/<user>/<repository>(master / HEAD) 更改为https://github.com/<user>/<repository>/tree/<commit id>.

In order to download the specific commit, you'll need to reload the page from that URL, so the Clone or Downloadbutton will point to the "snapshot" https://github.com/<user>/<repository>/archive/<commit id>.zipinstead of the latest https://github.com/<user>/<repository>/archive/master.zip.

为了下载特定提交,您需要从该 URL 重新加载页面,因此Clone or Download按钮将指向“快照”https://github.com/<user>/<repository>/archive/<commit id>.zip而不是最新的https://github.com/<user>/<repository>/archive/master.zip.

回答by Shubham Verma

If you want to go with any certain commit or want to code of any certain commit then you can use below command:

如果您想进行任何特定提交或想要对任何特定提交进行编码,那么您可以使用以下命令:

git checkout <BRANCH_NAME>
git reset --hard  <commit ID which code you want>
git push --force

Example:

例子:

 git reset --hard fbee9dd 
 git push --force