๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Fetching a Git Repository - git fetch


Introduction

  • git fetch command is used to retrieve the latest updates from a remote repository without merging them into your local branch.
  • It allows you to see new commits, branches, and changes before integrating them.
  • Key Points about git fetch :
    • Retrieves Updates Without Merging:
      • Unlike git pull, which fetches and merges changes, git fetch only downloads the latest commits and references from the remote repository.
      • This helps in reviewing changes before applying them.
    • Does Not Modify the Working Directory:
      • Your working files remain unchanged, and the fetched updates are stored in .git history until you explicitly merge or rebase them.
    • Updates Remote Tracking Branches:
      • Remote tracking branches like origin/main or origin/feature-branch are updated with the latest commits from the remote repository.
    • Used for Checking Remote Changes:
      • It allows you to inspect new branches, commits, and tags before deciding whether to merge them into your local branch.

How to use git fetch ?

  1. Make a changes in your original repo i.e. in "Local Repo" OR in "GitHub Repo":
    • Update Local Repo as below:
      • Open the repo on your local machine.
      • Edit a file or add a new one.
      • Run below commands:
        • git add .
        • git commit -m "Added a test file in local repo"
        • git push
    • --------OR--------
    • Update GitHub Repo as below:
      • Open the repo on your GitHub repo.
      • Open any file and update it.
      • Save that file by committing the changes with message.
  2. Fetch Changes in Cloned Repository (Or any other location) :-
    • Now, in another local directory where we have cloned the repository, we can fetch the latest changes from the online repository.
    • Syntax :
      • git fetch <remote-name>
    • Command :
      • git fetch origin
    • The command will show the following output:
      Git Fetch Command
      This command retrieves the latest changes from the specified repository without merging them. The output shows the enumeration, counting, compression and unpacking of objects.
  3. View Fetched Changes Using log and diff :-
    • Now, we examine what changes have been fetched using the git log and git diff commands.
    • Command to View Differences:
      • git diff HEAD FETCH_HEAD
    • Command to View Log:
      • git log HEAD..FETCH_HEAD --oneline
    • Below are the commands with output:
      Git Fetch Command

Different ways to use git fetch ?

  1. Basic Fetch (Fetch All Branches & Updates from Remote):
    • Syntax :
      • git fetch <remote-name>
    • Example:
      • git fetch origin
    • Fetches updates from the origin remote without merging them into your local branch.
  2. Fetch from a Specific Remote URL (Instead of Remote Name):
    • Syntax :
      • git fetch <remote-url>
    • Example:
      • git fetch https://github.com/username/repo-name.git
    • Fetches updates directly from the specified repository URL.
  3. Fetch a Specific Branch:
    • Syntax :
      • git fetch <remote-name> <branch-name>
    • Example:
      • git fetch origin develop
    • Fetches only the develop branch from the origin remote.
  4. Fetch with All Tags:
    • Syntax :
      • git fetch --tags
    • Example :
      • git fetch origin --tags
    • Fetches all tags from the remote repository.
  5. Fetch and Check for Updates Without Downloading Objects:
    • Syntax :
      • git fetch --dry-run
    • Exmaple :
      • git fetch origin --dry-run
    • Checks for updates but does not actually download any objects.
  6. Fetch and Prune (Remove Deleted Remote Branches Locally):
    • Syntax :
      • git fetch --prune
    • Example :
      • git fetch origin --prune
    • Removes references to remote branches that were deleted.
  7. Fetch Only Recent Commits (Shallow Fetch):
    • Syntax :
      • git fetch --depth <number>
    • Example :
      • git fetch origin --depth 1
    • Fetches only the latest commit, reducing bandwidth usage.
  8. Fetch All Remotes at Once:
    • Syntax :
      • git fetch --all
    • Example :
      • git fetch --all
    • Fetches updates from all configured remotes in the repository.
  9. Fetch Specific Ref (Tag, Commit, or Branch):
    • Syntax :
      • git fetch <remote-name> <ref>
    • Example :
      • git fetch origin refs/tags/v1.0.0
    • Fetches only the specified tag v1.0.0 from origin.