DEV Community

Harsh Mishra
Harsh Mishra

Posted on

Git: fetch, merge, pull and push

Understanding the git fetch, git merge, git pull, and git push commands is crucial when working with Git for version control. Below is a detailed guide to help clarify these commands and their syntax, including examples and explanations.

1. git fetch

  • Purpose: Updates your local repository with changes from the remote repository, but does not merge them into your current branch.
  • When to Use: When you want to check what changes exist on the remote without merging them yet.

Syntax:

   git fetch <remote> <branch>
Enter fullscreen mode Exit fullscreen mode
  • <remote>: The name of the remote repository (typically origin). Optional, default value is origin.
  • <branch>: The branch you want to fetch. If omitted, all branches are fetched. Optional, default is fetching all branches.

Examples:

   git fetch origin
Enter fullscreen mode Exit fullscreen mode

Fetches all branches from the remote origin but doesn’t modify any local branches.

   git fetch origin main
Enter fullscreen mode Exit fullscreen mode

Fetches updates from the main branch of the remote repository but does not apply them to your local main branch.

2. git merge

  • Purpose: Combines changes from another branch into the current branch. It is used to integrate fetched or locally developed changes.
  • When to Use: When you want to combine changes from different branches, typically after using git fetch.

Syntax:

   git merge <branch>
Enter fullscreen mode Exit fullscreen mode
  • <branch>: The name of the branch whose changes you want to merge into your current branch. Required, no default value.

Example:

   git merge feature-branch
Enter fullscreen mode Exit fullscreen mode

Merges the changes from feature-branch into the current branch (e.g., main).

3. git push

  • Purpose: Uploads local branch commits to the remote repository.
  • When to Use: After committing changes locally, you use git push to send your changes to the remote repository.

Syntax:

   git push <remote> <branch>
Enter fullscreen mode Exit fullscreen mode
  • <remote>: The name of the remote repository (typically origin). Optional, default value is origin.
  • <branch>: The branch you want to push your changes to. Optional, default value is the current branch.

Examples:

   git push origin main
Enter fullscreen mode Exit fullscreen mode

Pushes changes from your local main branch to the main branch on the remote origin.

   git push origin feature-branch
Enter fullscreen mode Exit fullscreen mode

Pushes changes from your local feature-branch to the remote origin.

4. git pull

  • Purpose: Combines git fetch and git merge in one step. It fetches changes from the remote repository and immediately merges them into your current branch.
  • When to Use: When you want to update your local branch with the latest changes from the remote repository in one step.

Syntax:

   git pull <remote> <branch>
Enter fullscreen mode Exit fullscreen mode
  • <remote>: The name of the remote repository (typically origin). Optional, default value is origin.
  • <branch>: The branch you want to pull changes from. Optional, default value is the current branch.

Examples:

   git pull origin main
Enter fullscreen mode Exit fullscreen mode

Fetches changes from the main branch on the remote origin and immediately merges them into your current branch.

Common Scenarios and Workflows

Fetching and Merging

When you want to manually control the update process:

  1. Fetch changes from the remote:
   git fetch origin main
Enter fullscreen mode Exit fullscreen mode
  1. Review changes, then merge them into your current branch:
   git merge origin/main
Enter fullscreen mode Exit fullscreen mode

Pulling (Fetching + Merging)

When you want to automatically fetch and merge in one step:

  1. Pull changes from the remote:
   git pull origin main
Enter fullscreen mode Exit fullscreen mode

Pushing Changes

After committing local changes:

  1. Push them to the remote:
   git push origin main
Enter fullscreen mode Exit fullscreen mode

Detailed Example of Workflow

  1. Fetch the Latest Changes
   git fetch origin
Enter fullscreen mode Exit fullscreen mode

This fetches updates from all branches of the origin remote.

  1. Merge Updates into Your Branch
   git merge origin/main
Enter fullscreen mode Exit fullscreen mode

This merges the latest updates from origin/main into your current branch.

  1. Make Changes Locally You make some edits and then commit your changes:
   git add .
   git commit -m "Added new feature"
Enter fullscreen mode Exit fullscreen mode
  1. Push Changes to the Remote After committing, push your changes:
   git push origin main
Enter fullscreen mode Exit fullscreen mode
  1. Pulling Updates from Remote If someone else made changes to main, you can pull their updates:
   git pull origin main
Enter fullscreen mode Exit fullscreen mode

Summary of Differences:

  • git fetch: Downloads updates from the remote repository but does not modify your working directory or branches.
  • git merge: Combines changes from one branch (either remote or local) into your current branch.
  • git pull: A combination of git fetch and git merge, fetching changes from the remote and merging them into your current branch in one command.
  • git push: Uploads your local branch's changes to the remote repository.

Top comments (0)