GIT – How To Fetch Code From Remote ?


How To Fetch Code From Remote?

Table Of Contents:

  1. What Is Git Fetch?
  2. Examples Of Git Fetch?
  3. Difference Between Fetching and Pulling?

(1) What Is GIT Fetch?

  • You can use git fetch to know the changes done in the remote repo/branch since your last pull.
  • This is useful to allow for checking before doing an actual pull, which could change files in your current branch and working copy (and potentially lose your changes, etc).

(2) Examples Of GIT Fetch?

Example-1: Fetching Remote Repository

git fetch <repository Url>  
git fetch https://github.com/Subrat/Project.git
  • It will download all the changes and keep them in a separate file.
  • It won’t update your working directory.
  • Here two new branches have been downloaded to local from remote,  “origin/main” and “origin/login”
  • You can now check the difference between your local and remote branches, to check for new changes.

Example-2: Checking The Difference Between Branches.

 git diff <LOCAL BRANCH> <REMOTE BRANCH> 
 git diff main origin/main
  • After you see the difference, if there is any conflict, that means the same file and same line other developer has added the code, you can resolve it manually.
  • You can use the “Beyond Compare” tool to resolve the conflict.

Example-2: Fetching A Specific Branch

 git fetch <remote URL> <branch name> 
git fetch https://github.com/Subrat/Project.git branch1
  • It will only download the ‘branch1’ branch.
  • Here you can see that, your local branch is 2 commits behind.
  • You can check those commits and merge them to the local branch.

Leave a Reply

Your email address will not be published. Required fields are marked *