Bootstrap FreeKB - Git (Version Control) - Download files, commits and references (refs) using the git fetch command
Git (Version Control) - Download files, commits and references (refs) using the git fetch command

Updated:   |  Git (Version Control) articles

There are a number of different ways to download one or more files from Git or 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 git fetch command can be used to download the files, commits, and references (refs) in branches of a remote repository to the currently selected branch of a cloned repository. Fetching does NOT require you to merge a commit into the default branch of the repo and has no effect on your local commits. git fetch is the "safe" version of the git pull command since fetched content has to be checkout out using the git checkout command to start the process of updating your local copy of a file in the repo making fetch a safe way to review commits before integrating a commit into your local branch.

The most basic way to use Git is to use the git clone command to clone a 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.

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 branch command with the -a or --all flag can be used to display all of the branches in the repository. The wildcard character is used to identify the branch you are currently using (main in this example).

~]# git branch --all
  feature/foo
  feature/bar
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/feature/foo
  remotes/origin/feature/bar
  remotes/origin/main

 

The git fetch command without any options will pull files from all of the branches of the remote repository to the corresponding branches on your local PC. If files are fetched something like this should be returned. In this example, the files in the "main" branch of the cloned repository were fetched into the "main" branch of the cloned repository on the local PC.

If you re-issue the git fetch command no stdout will be displayed. This simply means there were no files to fetch from the remote repository since all of the files in your clone of the repo are an exact match of the files in the remote repo.

~]# git fetch
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 10 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (10/10), 2.37 KiB | 808.00 KiB/s, done.
From github.com:foo/bar
   c09a4b6..70da4f4  main       -> origin/main

 

The git remote command can be used to display the remotes. In this example, there are two remotes, origin and demo.

]$ git remote -v
origin  git@github.com:foo/hello.git (fetch)
origin  git@github.com:foo/hello.git (push)
demo    git@github.com:foo/world.git (fetch)
demo    git@github.com:foo/world.git (push)

 

The name of the remote can be used to only fetch the files from the specific remote, origin in this example.

git fetch origin

 

The -q or --quiet option can be used to suppress output.

git fetch --quiet

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter b78b84 in the box below so that we can be sure you are a human.