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

Fetch and Merge Changes from Remote Repository -


Introduction

  • git pull command is used to fetch the latest changes from a remote repository and automatically merge them into the current local branch..
  • It ensures your local repository stays up-to-date with the remote repository without manually running separate commands.
  • It is equivalent to running git fetch followed by git merge :
  • After fetching the updates, git pull attempts to merge them into the current branch.
  • If you want to inspect changes before merging, using git fetch followed by git merge is recommended instead of directly using git pull.

How to use git pull ?

  1. Open the Git terminal (or command prompt) and navigate to the project folder where your Git repository is initialized.
  2. Make sure that:-
    • You have connected your local project to the remote repository using:
      • git remote add origin https://github.com/SmartProgrammingCoders/GitDemo.git
    • You are on the correct branch (e.g., main) and it matches the remote branch name. If not, rename it using:
      • git branch -M main
    • You have already pushed the project at least once, so remote contains some data.
    • git push origin main
  3. Now run the git pull command:
    • Syntax : git pull origin <branch-name>
    • Example : git pull origin main
    • The command will show the following: Git Pull Command
      • This command will:
        • Fetches the latest changes from the remote repository (origin).
        • Merges those changes into your current local branch (main).

Different ways to use git pull ?

  1. Basic Pull (from default remote and branch):
    • Syntax : git pull
    • This pulls the latest changes from the default remote (usually origin) and the tracking branch (e.g. main or master).
    • Use case: You already cloned the repo and are on a branch connected to the remote.
  2. Pull from Specific Remote:
    • Syntax : git pull <remote-name>
    • Example:
      • git pull origin
    • Pulls changes from the origin remote using the currently checked-out branch.
  3. Pull from Specific Remote and Branch:
      Syntax :
    • git pull <remote-name> <branch-name>
    • Example:
      • git pull origin main
    • This pulls changes from the main branch of the origin remote.
    • Useful if you are not on a tracking branch or want changes from another branch.
  4. Pull with Rebase Instead of Merge:
      Example:
    • git pull --rebase
    • Instead of merging, Git re-applies your local commits on top of the fetched changes.
    • Cleaner history, no merge commits.
  5. Pull into a New Branch:
    • Syntax : git checkout -b new-branch-name origin/branch-name
    • Example:
      • git checkout -b feature-x origin/feature-x
      • git pull
    • Useful when pulling changes from a remote branch that doesn't exist locally yet.
  6. Pull with Depth Limit (shallow pull):
      Example :
    • git pull --depth=1
    • Fetches only the most recent commits from the remote.
    • Useful for performance or bandwidth-limited environments.