210

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...

Can someone please tell me how to do this?

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

1

12 Answers 12

282

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

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 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.

5
  • 4
    At this point you can also git export if you just want the files.
    – Xeoncross
    Sep 26, 2012 at 16:29
  • 1
    git checkout -q <commit#> silences warning stderr during git checkout. Apr 19, 2019 at 22:11
  • 1
    When I do git checkout 441b034635240bc873700473af3e98533e94a0b5, this gives an error: fatal: reference is not a tree: 441b034635240bc873700473af3e98533e94a0b5
    – ttb
    Mar 13, 2020 at 17:35
  • @Xeoncross "git: 'export' is not a git command." Jan 9, 2021 at 20:06
  • Just as a note git clone git://blar does not download the complete history. It downloads the history of active branches. It's a very subtle difference in git.
    – WORMSS
    Feb 4, 2021 at 10:23
166

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 commits tab when viewing a repository. Then instead of clicking on the commit name, click on Browse the repository at this point in the history button with <> symbol to the right side of the commit name/message, and finally on the Download ZIP button that comes when you click Clone or Download button.

I hope it helps you guys.

6
  • 6
    This is the only way to recover "lost commits" which are commits that github does have a history of, but were erased from the "official" git timeline. Using this, you can get a zip snapshot of a commit that was rebased into oblivion, push that commit to a new branch, fetch that branch into an existing repo, and cherry-pick it back into existence.
    – Ajax
    Dec 15, 2014 at 16:18
  • 1
    @Ajax That's not true. You can still get the sha using git reflog, and then checkout the detached commit Mar 16, 2016 at 18:54
  • 1
    @AaronBrager sure, reflog will work, IF you are on the same machine that had initially had the commit. The OP already knows the commit, so they would not need reflog and could just checkout directly. However, I am talking about cases where you don't have the commit locally, and it isn't on any branches that are still in the public repo. In these cases, github may still have a record of the commit, which you can download as a zip to be able to recover said commit.
    – Ajax
    Mar 24, 2016 at 20:33
  • @Ajax Ah, thanks for the clarification. As an alternative, you can hit the GitHub API (api.github.com/repos/facebook/facebook-ios-sdk/events) and create a branch from the orphaned commit (instructions here) Mar 24, 2016 at 20:42
  • Very nice. +1 I didn't know you could query github's reflog like that!
    – Ajax
    Mar 24, 2016 at 22:49
43

Sivan's answer in gif enter image description here

1.Click on commits in github

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

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

3
  • Hello suraj can you please tell me how did you created the gif of your screen? Dec 6, 2015 at 18:49
  • 1
    use quick time player to record a video of screen.Do conversion of video to gif using any available tool .Google it.@ Rahul Satal Dec 7, 2015 at 4:03
  • 1
    Thanks @SurajKThomas for the gif Jul 14, 2016 at 6:54
16

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

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.

11

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.

  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 pull the new branch down to local
2
  • This answer is more general than the accepted one. I met this situation that the commit I wanted already disappeared on master branch, and Github will not keep every commits on non existing branches. So this approach will create a new branch from that commit on Github and make it pull-able.
    – Qi Luo
    Dec 27, 2016 at 22:58
  • This let me to recover a commit (with original SHA intact) from a deleted a fork that I did a PR from. After creating a new fork, I opened the PR commit on the upstream repo then did "browse files." I then copied that URL and replaced the username with my own. Then clicked the Tree: dropdown and created a new branch in my fork. Worked great!
    – Allen Luce
    Mar 9, 2017 at 18:59
10

Try the following command sequence:

$ git fetch origin <copy/past commit sha1 here>
$ git checkout FETCH_HEAD
$ git push origin master
2
  • 4
    This is by far the best answer I have seen if the commit still exists on github but the PR has been squash-merged and the original branch has been deleted.
    – WORMSS
    Feb 4, 2021 at 10:29
  • 1
    Note that you seem to need the full commit sha1, not the abbreviated sha1. Jun 7, 2022 at 23:30
4

The question title is ambiguous.

4

Instead of navigating through the commits, you can also hit the y key (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>.

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

1
  • 1
    For Tags use https://github.com/<user>/<repository>/archive/refs/tags<Tag>.zip
    – SePeF
    Oct 27, 2022 at 11:59
3

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
1
  • 2
    This is dangerous, it makes more sense to clone the repo and checkout a version Jul 11, 2017 at 22:01
1

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.

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

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 before the master. So 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.

0

To use the Github webpage to do this, here's what I did exactly: go to the commit list -> click the commit number I need to download -> click "Browse Files" on the upper-right of the page -> click the dropdown button "Code" and then click "Download ZIP".

I don't have git terminal and don't want to set up one only for downloading a commit, Sivanand Kheti'S answer helped me to download the commit using webpage browsing, and hope my steps help the same users as myself.

-1

write this to see your commits

git log --oneline

copy the name of the commit you want to go back to. then write:

git checkout "name of the commit"

when you do this, the files of that commit will be replaced with your current files. then you can do whatever you want to these and once you're done, you can write the following command to extract the current files into another newly created branch so whatever you make doesn't have any danger for the previous branch that you extracted a commit from

git checkout -b "name of a branch to extract the files to"

right now, you have the content of a specified commit, into another branch .

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.