Git Push and Git Pull

Once a remote repository is connected, code can be synchronized between the local machine and GitHub using two commands:

  • git push — Uploads local commits to the remote repository
  • git pull — Downloads new commits from the remote repository to the local machine
Local Computer                     GitHub (Remote)
──────────────                     ───────────────
   git push    ──────────────────►  Remote Repo
   git pull    ◄──────────────────  Remote Repo

git push — Uploading Changes

What Does git push Do?

After commits are made locally, those commits only exist on the local machine. Running git push uploads those commits to GitHub so they are backed up and visible to collaborators.

Basic Syntax

git push origin branch-name
  • origin — The name of the remote (default name for GitHub)
  • branch-name — The branch to push (e.g., main)

First Push — Setting the Upstream Branch

The first time pushing to a new remote/branch, use the -u flag. This sets a "tracking relationship" so that in the future, just typing git push is enough:

git push -u origin main

After this initial push, subsequent pushes only need:

git push

Push Example — Step by Step

# Make changes and commit locally
echo "New content" >> index.html
git add index.html
git commit -m "Update homepage content"

# Push to GitHub
git push origin main

# Output:
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 312 bytes | 312.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/ravikumar/my-website.git
   b7c3d45..a3f92b1  main -> main

The output confirms the commits were successfully uploaded.

Push a Specific Branch

git push origin feature-login

Pushes the local feature-login branch to GitHub.

Force Push (Use with Extreme Caution)

git push --force origin main

A force push overwrites the remote history with local history. This is dangerous on shared branches as it can permanently erase other people's commits. Only use it on personal branches when absolutely necessary.

Push All Branches

git push --all origin

Push Tags

git push origin --tags

Pushes all local tags to GitHub.

git pull — Downloading Changes

What Does git pull Do?

When teammates push new commits to the remote repository, those changes need to be downloaded to the local machine. git pull fetches the new commits from GitHub AND automatically merges them into the current local branch.

git pull is actually a combination of two commands:

  1. git fetch — Downloads new commits from remote (does not merge)
  2. git merge — Merges the downloaded commits into the current branch

Basic Syntax

git pull origin branch-name

Or simply, if the tracking relationship is already set:

git pull

Pull Example

# Download and merge latest changes from remote main branch
git pull origin main

# Output:
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
Unpacking objects: 100% (3/3), done.
From https://github.com/ravikumar/my-website
   b7c3d45..d3e4f56  main -> origin/main
Updating b7c3d45..d3e4f56
Fast-forward
 about.html | 10 ++++++++++
 1 file changed, 10 insertions(+)

git fetch vs git pull

CommandWhat it DoesSafe?
git fetchDownloads commits but does NOT merge them. Shows what's available on the remote.Very safe — no changes to working directory
git pullDownloads commits AND automatically merges them into the current branch.Safe most of the time, but can cause merge conflicts

Using git fetch first and then reviewing changes before merging is a safer approach:

# Download changes without applying them
git fetch origin

# See what was downloaded
git log HEAD..origin/main --oneline

# Now merge (if happy with the changes)
git merge origin/main

git pull with Rebase

Instead of creating a merge commit when pulling, a cleaner history can be created using rebase:

git pull --rebase origin main

This places local commits on top of the downloaded commits, creating a straight linear history without extra merge commits.

Common Push/Pull Errors and Solutions

Error: "rejected — non-fast-forward"

! [rejected]    main -> main (non-fast-forward)

Cause: The remote has commits that are not in the local repo. A pull is needed first.

Solution:

git pull origin main
git push origin main

Error: "authentication failed"

Cause: Incorrect username/password or expired credentials.

Solution: Use a Personal Access Token (PAT) instead of a password. Generate one at GitHub → Settings → Developer Settings → Personal Access Tokens.

The Typical Daily Workflow

# Start of the day: pull latest changes
git pull origin main

# Work on code
# ... make changes ...

# Stage and commit
git add .
git commit -m "Add new feature"

# End of the day: push work to GitHub
git push origin main

Summary

git push uploads local commits to GitHub. git pull downloads and merges remote commits to the local machine. Use git push -u origin main for the first push to set the tracking relationship. Always pull before pushing in a team environment to avoid conflicts.

Leave a Comment

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