There are a number of different ways to download one or more files from Git or GitHub.
- Download files using the git checkout command
- Download files using the git fetch command
- Download files using the git pull command (this article)
- Download a single file from GitHub
Before you can checkout, pull or fetch files, you'll need to use the git clone command to clone an origin Git repository to a directory on your local PC.
The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.
Branches are used as an isolated way to make changes to files in a repository. A common example would be to create a new branch using the git branch or git checkout command, switch to the new branch using the git checkout command, make a change to a file, commit the change using the git commit command, and then merge the branch to the master branch using the git merge command.
The git checkout, git pull and git fetch commands can be used to download files in a repository to the currently selected branch of the cloned repository.
AVOID TROUBLE
If your branch already contains a file that is also in the repository, and there are differences between the files, git pull will attempt to integrate and merge the differences into the files in your branch.
git fetch will download but not merge or integrate any differences between similar files
The git pull command without any options will pull files from the origin repository to the currently selected branch of the cloned repository. The git remote command with the -v or --verbose flag can be used to display the URL of the origin Git repository.
~]# git remote --verbose
origin ssh://git@example.com:7999/path/to/example.git (fetch)
origin ssh://git@example.com:7999/path/to/example.git (push)
In this example, the foo.txt file in the origin repository is different than the foo.txt file in your currently selected branch of the cloned repository, thus the foo.txt was pulled from the origin repository to your currently selected branch.
~]# git pull
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 3), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From ssh://git@example.com:7999/path/to/example
f944172..d6628da master -> origin/master
Updating f944172..d6628da
Fast-forward
path/to/foo.txt | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 71 insertions(+)
create mode 100644 path/to/foo.txt
In this example, no files in the origin Git repository have changed since the last pull.
~]$ git pull
Already up-to-date.
Let say something like this is being returned after entering the git pull command. This means there are one or more files in the origin Git repository that are different than the files in your currently selected branch of the cloned repository. If you proceed, the files in the origin Git repository will be merged, meaning the files will be downloaded from the origin Git repository to your currently selected branch of the cloned repository. You may want to also check out my article Merge a branch using the git merge command.
Merge branch 'master' of ssh://git@example.com:7999/path/to/example.git
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
After allowing the merge, the files that were downloaded from the origin Git repository to your currently selected branch of the cloned repository will be listed. In this example, 5 new lines were added or updated in foo.txt and 1 line was added/update and 1 line was removed from bar.txt. The git show command can be used to display the changes that were made to the files.
Merge made by the 'recursive' strategy.
path/to/foo.txt | 5 +++++
path/to/bar.txt | 2 +-
3 files changed, 6 insertions(+), 1 deletions(-)
Or you can include origin followed by the name of a branch you want to pull to, such as master.
git pull origin master
Or some other branch, such as staging.
git pull origin staging
The prior command will return "Already up-to-date" if your the files in your branch are the same revision as the files in the origin Git repository.
Already up-to-date.
The -q or --quiet option can be used to suppress output.
git pull --quiet
Did you find this article helpful?
If so, consider buying me a coffee over at