Git

Git Download Repository Example

1. Introduction

In this post, we feature a comprehensive Git Download Repository Example. Git is an open source distributed version control system designed to handle everything from small to large projects with speed and efficiency. It was first released on 12-15-2005. There are lots of Git Hosting Services available today. GitHub is one of the most popular ones. In this article, I will demonstrate three ways to download a repository from GitHub. The three ways are:

  1. Git clone command
  2. Download via GitHub
  3. IDE with Git Support

2. Technologies and Tools Used

The technologies and tools used in this article are:

  • Git 2.16.*
  • Eclipse Mars
  • IntelliJ IDEA 14.*
  • GitHub

3. Git Clone Command

If you have not yet installed Git, click here to do so. Git clone command is used to clone a repository from one location to another. Here is the syntax of the clone command:

git clone

git clone [--template=<template_directory>]
	  [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
	  [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
	  [--dissociate] [--separate-git-dir <git dir>]
	  [--depth <depth>] [--[no-]single-branch] [--no-tags]
	  [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
	  [--jobs <n>] [--] <repository> [<directory>]

Please click here for more details about the clone command. In this step, we will clone a repository from https://github.com/maryzheng01/Deadlock-demo:

  1. Navigate to C:\Users\shu.shan\GIT_WORK
  2. Enter git clone https://github.com/maryzheng01/Deadlock-demo

Commands Outputs

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\shu.shan>cd \

C:\>cd GIT_WORK\

C:\GIT_WORK>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 3A10-C6D4

 Directory of C:\GIT_WORK

05/19/2018  08:05 AM    <DIR>          .
05/19/2018  08:05 AM    <DIR>          ..
05/16/2018  03:04 PM    <DIR>          change-saints
02/09/2018  10:31 AM    <DIR>          rest-service-springboot-archetype
02/09/2018  11:31 AM    <DIR>          rest-service-template
               0 File(s)              0 bytes
               5 Dir(s)  59,034,894,336 bytes free

C:\GIT_WORK>git clone https://github.com/maryzheng01/Deadlock-demo.git
Cloning into 'Deadlock-demo'...
remote: Counting objects: 110, done.
remote: Compressing objects: 100% (38/38), done.
remote: Total 110 (delta 14), reused 50 (delta 4), pack-reused 50
Receiving objects: 100% (110/110), 19.31 KiB | 988.00 KiB/s, done.
Resolving deltas: 100% (22/22), done.

C:\GIT_WORK>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 3A10-C6D4

 Directory of C:\GIT_WORK

05/31/2018  12:37 PM    <DIR>          .
05/31/2018  12:37 PM    <DIR>          ..
05/16/2018  03:04 PM    <DIR>          change-saints
05/31/2018  12:37 PM    <DIR>          Deadlock-demo
02/09/2018  10:31 AM    <DIR>          rest-service-springboot-archetype
02/09/2018  11:31 AM    <DIR>          rest-service-template
               0 File(s)              0 bytes
               6 Dir(s)  59,034,976,256 bytes free

C:\GIT_WORK>

The clone command downloaded a zip file to C:\Users\shu.shan\GIT_WORK\Deadlock-demo which contains all the files in the repository hosted at https://github.com/maryzheng01/Deadlock-demo.

4. Download via GitHub

GitHub is a popular web-based Git repository hosting service. GitHub has three ways to download a repository:

  1. Download via the GitHub web site
  2. Download via the GitHub desktop application
  3. Download via the GitHub API

4.1 GitHub Web site

GitHub web site provides a button to download a repository. In this step, I will download a repository through the GitHub web site:

  1. Open a browser and go to https://github.com/maryzheng01/Deadlock-demo.
  2. Select the desired branch, e.g. master.
  3. Click Clone or download, and then click Download ZIP.

It will download a zip file which contains all the files in the repository.

4.2 GitHub Desktop Application

GitHub desktop application allows users to download/synchronize a repository. In this step, I will show you how to clone a repository from GitHub desktop application.

  1. Open GitHub desktop application
  2. Login with your GitHub credential
  3. Click Clone and enter the repository location
     

    Git Download Repository - GitHub Desktop
    Figure 1 GitHub Desktop

GitHub desktop also provides a Pull request button to download any differences from a remote repository after the initial download. The user just need to select the branch name and click the Send pull request button.

4.3 GitHub API

GitHub provides a content API to download the repository’s content. In this example, I will show you how to download from a GitHub repository.

Based on the GitHub API definition, the repository’s URL is defined as https://api.github.com/users/{username}/repos. For this example, I will download my repository from https://api.github.com/users/maryzheng01/repos because my GitHub user name is maryzheong01.

Open a browser and navigate to https://api.github.com/users/maryzheng01/repos. It will return a Json array with the repository’s information. I only have one repository, so we will only see one repository. We will capture the repository’s content_url from the highlighted row.

Maryzheng01 Repo Response Data

[
  {
    "id": 33957535,
    "node_id": "MDEwOlJlcG9zaXRvcnkzMzk1NzUzNQ==",
    "name": "Deadlock-demo",
    "full_name": "maryzheng01/Deadlock-demo",
    "owner": {
      "login": "maryzheng01",
      "id": 11510587,
      "node_id": "MDQ6VXNlcjExNTEwNTg3",
      "avatar_url": "https://avatars2.githubusercontent.com/u/11510587?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/maryzheng01",
      "html_url": "https://github.com/maryzheng01",
      "followers_url": "https://api.github.com/users/maryzheng01/followers",
      "following_url": "https://api.github.com/users/maryzheng01/following{/other_user}",
      "gists_url": "https://api.github.com/users/maryzheng01/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/maryzheng01/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/maryzheng01/subscriptions",
      "organizations_url": "https://api.github.com/users/maryzheng01/orgs",
      "repos_url": "https://api.github.com/users/maryzheng01/repos",
      "events_url": "https://api.github.com/users/maryzheng01/events{/privacy}",
      "received_events_url": "https://api.github.com/users/maryzheng01/received_events",
      "type": "User",
      "site_admin": false
    },
    "private": false,
    "html_url": "https://github.com/maryzheng01/Deadlock-demo",
    "description": "initial check in",
    "fork": false,
    "url": "https://api.github.com/repos/maryzheng01/Deadlock-demo",
    "forks_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/forks",
    "keys_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/keys{/key_id}",
    "collaborators_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/collaborators{/collaborator}",
    "teams_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/teams",
    "hooks_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/hooks",
    "issue_events_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/issues/events{/number}",
    "events_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/events",
    "assignees_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/assignees{/user}",
    "branches_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/branches{/branch}",
    "tags_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/tags",
    "blobs_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/blobs{/sha}",
    "git_tags_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/tags{/sha}",
    "git_refs_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/refs{/sha}",
    "trees_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/trees{/sha}",
    "statuses_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/statuses/{sha}",
    "languages_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/languages",
    "stargazers_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/stargazers",
    "contributors_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contributors",
    "subscribers_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/subscribers",
    "subscription_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/subscription",
    "commits_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/commits{/sha}",
    "git_commits_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/commits{/sha}",
    "comments_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/comments{/number}",
    "issue_comment_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/issues/comments{/number}",
    "contents_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/{+path}",
    "compare_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/compare/{base}...{head}",
    "merges_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/merges",
    "archive_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/{archive_format}{/ref}",
    "downloads_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/downloads",
    "issues_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/issues{/number}",
    "pulls_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/pulls{/number}",
    "milestones_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/milestones{/number}",
    "notifications_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/notifications{?since,all,participating}",
    "labels_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/labels{/name}",
    "releases_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/releases{/id}",
    "deployments_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/deployments",
    "created_at": "2015-04-14T21:17:27Z",
    "updated_at": "2018-05-19T13:48:08Z",
    "pushed_at": "2018-05-24T21:35:45Z",
    "git_url": "git://github.com/maryzheng01/Deadlock-demo.git",
    "ssh_url": "git@github.com:maryzheng01/Deadlock-demo.git",
    "clone_url": "https://github.com/maryzheng01/Deadlock-demo.git",
    "svn_url": "https://github.com/maryzheng01/Deadlock-demo",
    "homepage": null,
    "size": 21,
    "stargazers_count": 0,
    "watchers_count": 0,
    "language": "Java",
    "has_issues": true,
    "has_projects": true,
    "has_downloads": true,
    "has_wiki": true,
    "has_pages": false,
    "forks_count": 0,
    "mirror_url": null,
    "archived": false,
    "open_issues_count": 0,
    "license": null,
    "forks": 0,
    "open_issues": 0,
    "watchers": 0,
    "default_branch": "master"
  }
]

Navigate to the Deadlock-demo‘s content_urlhttps://api.github.com/repos/maryzheng01/Deadlock-demo/contents/. It will display an array of objects. The highlighted rows contain the url to download the file and content url to get encoded content.

Maryzheng01 Content API Response

[
  {
    "name": ".gitattributes",
    "path": ".gitattributes",
    "sha": "bdb0cabc87cf50106df6e15097dff816c8c3eb34",
    "size": 378,
    "url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/.gitattributes?ref=master",
    "html_url": "https://github.com/maryzheng01/Deadlock-demo/blob/master/.gitattributes",
    "git_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/blobs/bdb0cabc87cf50106df6e15097dff816c8c3eb34",
    "download_url": "https://raw.githubusercontent.com/maryzheng01/Deadlock-demo/master/.gitattributes",
    "type": "file",
    "_links": {
      "self": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/.gitattributes?ref=master",
      "git": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/blobs/bdb0cabc87cf50106df6e15097dff816c8c3eb34",
      "html": "https://github.com/maryzheng01/Deadlock-demo/blob/master/.gitattributes"
    }
  },
  {
    "name": ".gitignore",
    "path": ".gitignore",
    "sha": "3599352fbd68470467949b5c7e6ad64a7be59cb3",
    "size": 631,
    "url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/.gitignore?ref=master",
    "html_url": "https://github.com/maryzheng01/Deadlock-demo/blob/master/.gitignore",
    "git_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/blobs/3599352fbd68470467949b5c7e6ad64a7be59cb3",
    "download_url": "https://raw.githubusercontent.com/maryzheng01/Deadlock-demo/master/.gitignore",
    "type": "file",
    "_links": {
      "self": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/.gitignore?ref=master",
      "git": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/blobs/3599352fbd68470467949b5c7e6ad64a7be59cb3",
      "html": "https://github.com/maryzheng01/Deadlock-demo/blob/master/.gitignore"
    }
  },
  {
    "name": "README.md",
    "path": "README.md",
    "sha": "12caff302f3689c40e2bd11104e317aafb85bb1b",
    "size": 277,
    "url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/README.md?ref=master",
    "html_url": "https://github.com/maryzheng01/Deadlock-demo/blob/master/README.md",
    "git_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/blobs/12caff302f3689c40e2bd11104e317aafb85bb1b",
    "download_url": "https://raw.githubusercontent.com/maryzheng01/Deadlock-demo/master/README.md",
    "type": "file",
    "_links": {
      "self": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/README.md?ref=master",
      "git": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/blobs/12caff302f3689c40e2bd11104e317aafb85bb1b",
      "html": "https://github.com/maryzheng01/Deadlock-demo/blob/master/README.md"
    }
  },
  {
    "name": "pom.xml",
    "path": "pom.xml",
    "sha": "653adfab73fda0d7a88a6645b750a6060048ea85",
    "size": 2102,
    "url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/pom.xml?ref=master",
    "html_url": "https://github.com/maryzheng01/Deadlock-demo/blob/master/pom.xml",
    "git_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/blobs/653adfab73fda0d7a88a6645b750a6060048ea85",
    "download_url": "https://raw.githubusercontent.com/maryzheng01/Deadlock-demo/master/pom.xml",
    "type": "file",
    "_links": {
      "self": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/pom.xml?ref=master",
      "git": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/blobs/653adfab73fda0d7a88a6645b750a6060048ea85",
      "html": "https://github.com/maryzheng01/Deadlock-demo/blob/master/pom.xml"
    }
  },
  {
    "name": "src",
    "path": "src",
    "sha": "365efa9c6603654aa7b2a03ac983a873eb28a0e5",
    "size": 0,
    "url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/src?ref=master",
    "html_url": "https://github.com/maryzheng01/Deadlock-demo/tree/master/src",
    "git_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/trees/365efa9c6603654aa7b2a03ac983a873eb28a0e5",
    "download_url": null,
    "type": "dir",
    "_links": {
      "self": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/src?ref=master",
      "git": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/trees/365efa9c6603654aa7b2a03ac983a873eb28a0e5",
      "html": "https://github.com/maryzheng01/Deadlock-demo/tree/master/src"
    }
  }
]
  • lines 7, 23, 43, 55, 71: We can use the urlto get the file content as a base64 encoded value.
  • lines 10, 26, 42, 58, 74: We can use download_url to download a file.
  • lines 11, 27, 43, 59, 75: Shows whether or not the object is a file or a directory.

Navigate to the download_urlhttps://raw.githubusercontent.com/maryzheng01/Deadlock-demo/master/README.md. It will display the file content.

README.md

This is a Maven project to demo:
1) how to use Spring JDBC XML configuration to connect to two data sources.
2) how to use the Transaction manager to avoid the deadlock.
   2.1) Guard the Transaction at each Dao method level
   2.2) Guard the Transaction at the service level

Navigate to the url: https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/README.md?ref=master. It will display the file content as a base64 encoded value.

README Encoded Content

{
  "name": "README.md",
  "path": "README.md",
  "sha": "12caff302f3689c40e2bd11104e317aafb85bb1b",
  "size": 277,
  "url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/README.md?ref=master",
  "html_url": "https://github.com/maryzheng01/Deadlock-demo/blob/master/README.md",
  "git_url": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/blobs/12caff302f3689c40e2bd11104e317aafb85bb1b",
  "download_url": "https://raw.githubusercontent.com/maryzheng01/Deadlock-demo/master/README.md",
  "type": "file",
  "content": "ClRoaXMgaXMgYSBNYXZlbiBwcm9qZWN0IHRvIGRlbW86CjEpIGhvdyB0byB1\nc2UgU3ByaW5nIEpEQkMgWE1MIGNvbmZpZ3VyYXRpb24gdG8gY29ubmVjdCB0\nbyB0d28gZGF0YSBzb3VyY2VzLgoyKSBob3cgdG8gdXNlIHRoZSBUcmFuc2Fj\ndGlvbiBtYW5hZ2VyIHRvIGF2b2lkIHRoZSBkZWFkbG9jay4KICAgMi4xKSBH\ndWFyZCB0aGUgVHJhbnNhY3Rpb24gYXQgZWFjaCBEYW8gbWV0aG9kIGxldmVs\nCiAgIDIuMikgR3VhcmQgdGhlIFRyYW5zYWN0aW9uIGF0IHRoZSBzZXJ2aWNl\nIGxldmVsCg==\n",
  "encoding": "base64",
  "_links": {
    "self": "https://api.github.com/repos/maryzheng01/Deadlock-demo/contents/README.md?ref=master",
    "git": "https://api.github.com/repos/maryzheng01/Deadlock-demo/git/blobs/12caff302f3689c40e2bd11104e317aafb85bb1b",
    "html": "https://github.com/maryzheng01/Deadlock-demo/blob/master/README.md"
  }
}

5. IDE with Git Support

Common IDEs, such as Visual Studio, Eclipse, NetBeans, IntelliJ IDEA, etc, support Git from the box. In this step, we will download a repository from https://github.com/maryzheng01/Deadlock-demo via Eclipse and IntelliJ IDEA.

5.1 IntelliJ IDEA

IntelliJ IDEA supports Git. There are only few steps to download a repository:

  1. Open IntelliJ IDEA.
  2. Select Git from the drop down list of Check out from Version Control.
    Git Download Repository - IntelliJ Download 1
    Figure 1 IntelliJ Download 1

  3. Enter the Git Repository URL and the Parent Directory.

    Git Download Repository -IntelliJ Download 2
    Figure 1 IntelliJ Download 2
  4. Click Clone.
  5. Verify that the project is downloaded to the location specified.

5.2 EGit

Eclipse supports Git. There are two basic steps to download a repository:

  1. Open Eclipse as a Git Perspective.
  2. Click the icon to clone from a remote repository.

    Git Download Repository - Git Clone
    Figure 2 Git Clone

Please check out my other article here for more details.

6. Summary

In this example, I showed how to download a remote repository hosted at GitHub via the Git clone command, Eclipse, IntelliJ IDEA, GitHub web sites, GitHub desktop application, and GitHub API. Please note that Git clone command, Eclipse, and IntelliJ IDEA can download repositories from any Git Hosting services.

Mary Zheng

Mary has graduated from Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She works as a senior Software Engineer in the telecommunications sector where she acts as a leader and works with others to design, implement, and monitor the software solution.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button