Back to roadmaps git Course

Syncing Code: git push, git fetch, and git pull

Let us examine the commands used to synchronize files between your local workspace and a remote server.


1. Uploading Changes: git push

To upload your local commits to your remote repository:

# Push and link the active branch to remote tracker
git push -u origin main

The -u (upstream) flag links your local branch to the remote branch, allowing you to run future commands using simply git push or git pull.


2. Downloading Updates: git fetch vs git pull

graph TD
    A[Remote Repository] -->|git fetch| B[Local .git database history]
    A -->|git pull| C[Local Working Directory files]
    B -->|git merge| C

Git Fetch (Safe Preview)

git fetch downloads commits and history metadata from the remote repository, but does not merge them into your active working files:

git fetch origin

This command allows you to review incoming changes safely using git log before integrating them.

Git Pull (Download and Merge)

git pull downloads remote changes and immediately merges them into your active working directory files:

git pull origin main

Note: git pull is shorthand for running git fetch followed by git merge.

Published on Last updated: