Как скачать репозиторий с github linux
Перейти к содержимому

Как скачать репозиторий с github linux

  • автор:

Download a file from github using Linux commands

Abhishek Verma

wget command is a commonly used CLI to download files from the internet. curl is another command used to transfer data to or from a server.

Say, you’ve a LAMP setup in your Linux machine and want to download a php file form Github. Below is a simple php file that displays the details of an AWS EC2 instance.

If you use this command to download the file, and then open it in a browser. It will just be a github HTML page and not the actual php file.

This is because github typically serves up files with an html wrapper around them. This includes the context and operations you can perform on the file.

Commands like wget and curl just download whatever the server sends them.

In order to get the actual file, you can get a raw file from github instead.

Copy the URL of the raw file and then use the wget or curl command to download the file.

Как скачать репозиторий с github linux

Git clone illustration

The git clone command is used to create a copy of a specific repository or branch within a repository.

Git is a distributed version control system. Maximize the advantages of a full repository on your own machine by cloning.

What Does git clone Do?

When you clone a repository, you don’t get one file, like you may in other centralized version control systems. By cloning with Git, you get the entire repository — all files, all branches, and all commits.

Cloning a repository is typically only done once, at the beginning of your interaction with a project. Once a repository already exists on a remote, like on GitHub, then you would clone that repository so you could interact with it locally. Once you have cloned a repository, you won’t need to clone it again to do regular development.

The ability to work with the entire repository means that all developers can work more freely. Without being limited by which files you can work on, you can work on a feature branch to make changes safely. Then, you can:

  • later use git push to share your branch with the remote repository
  • open a pull request to compare the changes with your collaborators
  • test and deploy as needed from the branch
  • merge into the main branch.

How to Use git clone

Common usages and options for git clone

  • git clone [url] : Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits.
  • git clone —mirror : Clone a repository but without the ability to edit any of the files. This includes the refs, or branches. You may want to use this if you are trying to create a secondary copy of a repository on a separate remote and you want to match all of the branches. This may occur during configuration using a new remote for your Git hosting, or when using Git during automated testing.
  • git clone —single-branch : Clone only a single branch
  • git clone —sparse : Instead of populating the working directory with all of the files in the current commit recursively, only populate the files present in the root directory. This could help with performance when cloning large repositories with many directories and sub-directories.
  • `git clone —recurse-submodules[=<pathspec]: After the clone is created, initialize and clone submodules within based on the provided pathspec. This may be a good option if you are cloning a repository that you know to have submodules, and you will be working with those submodules as dependencies in your local development.

You can see all of the many options with git clone in git-scm’s documentation.

Examples of git clone

The most common usage of cloning is to simply clone a repository. This is only done once, when you begin working on a project, and would follow the syntax of git clone [url] .

git clone A Branch

git clone —single-branch : By default, git clone will create remote tracking branches for all of the branches currently present in the remote which is being cloned. The only local branch that is created is the default branch.

But, maybe for some reason you would like to only get a remote tracking branch for one specific branch, or clone one branch which isn’t the default branch. Both of these things happen when you use —single-branch with git clone .

This will create a clone that only has commits included in the current line of history. This means no other branches will be cloned. You can specify a certain branch to clone, but the default branch, usually main , will be selected by default.

To clone one specific branch, use:

git clone [url] —branch [branch] —single-branch

Cloning only one branch does not add any benefits unless the repository is very large and contains binary files that slow down the performance of the repository. The recommended solution is to optimize the performance of the repository before relying on single branch cloning strategies.

git clone With SSH

Depending on how you authenticate with the remote server, you may choose to clone using SSH.

If you choose to clone with SSH, you would use a specific SSH path for the repository instead of a URL. Typically, developers are authenticated with SSH from the machine level. This means that you would probably clone with HTTPS or with SSH — not a mix of both for your repositories.

  • git branch : This shows the existing branches in your local repository. You can also use git branch [banch-name] to create a branch from your current location, or git branch —all to see all branches, both the local ones on your machine, and the remote tracking branches stored from the last git pull or git fetch from the remote.
  • git pull : Updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull is a combination of git fetch and git merge .
  • git push : Uploads all local branch commits to the remote.
  • git remote -v : Show the associated remote repositories and their stored name, like origin .

Get started with git and GitHub

Review code, manage projects, and build software alongside 40 million developers.

How to download a GitHub repo as .zip using command line

I am trying to download a .zip file from GitHub using the command line in Ubuntu. I am using wget command for it on a remote Ubuntu system.

I run wget <link> where <link> is the address bar link of the file which I want to download. It ends with archive.zip?ref=master .

Now, when I am executing the command, it is downloading a file with text/html type and not the .zip file which I want.

Please tell me how to get the link to be given as the parameter of wget . Right now, I am just copying the link address of the button (using right click) and writing that as a wget parameter.

4 Answers 4

It does work, if you use the correct URL.

For a GitHub repo, there’s a zip at https://github.com/<user>/<repo>/archive/<branch>.zip , so you can download it with:

This downloads the zipped repo for a given branch. Note that you can also replace the branch by a commit hash.

Using cURL

cURL’s -L flag follows redirects — it’s a default in wget.

Download a .tgz instead of .zip

You can also download a tarball with:

From the comments I saw you actually speak about GitHub.

It won’t work like this because:

Downloading a project on GitHub causes the GitHub server to first pack your project as zip and than forwarding you to a temporary link where you get your zip ..

this link will only work for a certain time and than GitHub will delete your zip file from their servers..

So what you get with wget is just the html page which would forward you as soon as your zip file is generated.

How do I download a tarball from GitHub using cURL?

I am trying to download a tarball from GitHub using cURL, but it does not seem to be redirecting:

Note: wget works for me:

However I want to use cURL because ultimately I want to untar it inline with something like:

I found that the URL after redirecting turned out to be https://download.github.com/pinard-Pymacs-v0.24-beta1-0-gcebc80b.tar.gz, but I would like cURL to be smart enough to figure this out.

Peter Mortensen's user avatar

5 Answers 5

Use the -L option to follow redirects:

The modernized way of doing this is:

Replace user-or-org , repo , and sha1-or-ref accordingly.

If you want a zip file instead of a tarball, specify .zip instead of .tar.gz suffix.

You can also retrieve the archive of a private repo, by specifying -u token:x-oauth-basic option to curl. Replace token with a personal access token.

You can also use wget to »untar it inline«. Simply specify stdout as the output file ( -O — ):

Peter Mortensen's user avatar

knittl's user avatar

All the other solutions require specifying a release/version number which obviously breaks automation.

This solution- currently tested and known to work with Github API v3— however can be used programmatically to grab the LATEST release without specifying any tag or release number and un-TARs the binary to an arbitrary name you specify in switch —one-top-level="pi-ap" . Just swap-out user f1linux and repo pi-ap in below example with your own details and Bob’s your uncle:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *