Git fetch is a command used to download objects and references from another repository. This is particularly useful when you want to retrieve changes made by others without merging them into your local branches.
Example 1: Fetching changes from the remote repository:
git fetch origin
This command fetches all branches from the remote named ‘origin’. To verify, check the output which lists the branches fetched.
Example 2: Fetching a specific branch:
git fetch origin main
Here, ‘main’ is the branch name. This fetches changes from the ‘main’ branch of the remote ‘origin’. Check the output to see the updates fetched from the remote branch.
Example 3: Fetching tags:
git fetch --tags origin
Using the --tags
option fetches all tags from the remote ‘origin’. Verify by listing tags after executing the command.
Example 4: Fetching and pruning deleted branches:
git fetch --prune origin
This command fetches remote branches and prunes any deleted remote branches from your local repository. Verify by checking that deleted branches are removed locally.
Example 5: Fetching a specific tag:
git fetch origin tag
Replace <tagname>
with the name of the tag you want to fetch. This fetches the specific tag from the remote ‘origin’. Verify by checking if the tag is fetched and available locally.
Example 6: Fetching changes from multiple remotes:
git fetch origin upstream
Fetching changes from multiple remotes like ‘origin’ and ‘upstream’. Verify by inspecting the fetched branches and tags from both remotes.
Example 7: Fetching changes without pruning:
git fetch --no-prune origin
Using --no-prune
option prevents pruning of deleted branches during fetch. Verify by ensuring deleted branches are retained locally after fetch.
Example 8: Fetching and showing verbose output:
git fetch --verbose origin
This command fetches with verbose output, showing detailed progress and information during the fetch process. Verify by observing detailed fetch progress messages.
Example 9: Fetching with force to overwrite local changes:
git fetch --force origin
Using --force
option fetches and overwrites local changes if any conflict occurs during fetch. Verify by checking if local changes are overwritten with remote changes.
Example 10: Fetching a specific commit:
git fetch origin
Replace <commit_hash>
with the commit hash and <branch_name>
with the branch name to fetch a specific commit to a specific branch. Verify by checking out the branch and seeing the fetched commit.
Discussion about this post