Skip to content

Working with Remotes: Clone, Push, Pull, and Fetch ​

You made a local commit. Now you need to share it: push to GitHub (or GitLab, or your team's Forgejo), pull down your teammate's latest changes, or clone a project from scratch. This guide covers the commands that move commits between repositories.

What is a remote? ​

A remote is a pointer to another copy of the repository. It stores a URL and a name. The default name after cloning is origin. You can add more: upstream, backup, production.

Think of a remote not as a server, but as a bookmark. Git talks to it over HTTPS or SSH, fetches or pushes commits, and updates the bookmark so you know what the other side had last time you talked.

Cloning: copy an existing repository ​

Cloning downloads a repository and sets up the remote bookmark automatically:

bash
git clone https://github.com/user/project.git

Output:

Cloning into 'project'...
remote: Enumerating objects: 42, done.
remote: Counting objects: 100% (42/42), done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 42 (delta 6), reused 42 (delta 6), pack-reused 0
Receiving objects: 100% (42/42), done.
Resolving deltas: 100% (6/6), done.

Git creates a directory named project, initializes .git inside it, downloads all commits from the remote, checks out the default branch, and configures origin to point back to the URL you gave.

Clone into a specific directory:

bash
git clone https://github.com/user/project.git my-custom-folder

Viewing and managing remotes ​

List the remotes you have:

bash
git remote -v

Output:

origin  https://github.com/user/project.git (fetch)
origin  https://github.com/user/project.git (push)

Add a second remote:

bash
git remote add upstream https://github.com/original-owner/project.git

Rename a remote:

bash
git remote rename origin upstream

Remove a remote:

bash
git remote remove stale-backup

Fetching: download commits without merging ​

Fetch downloads commits and updates your remote-tracking branches (like origin/main) but does NOT change your working tree or current branch.

bash
git fetch origin

Output:

remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 1.02 KiB | 1.02 MiB/s, done.
From https://github.com/user/project
   a1b2c3d..e4f5g6h  main       -> origin/main

The line main -> origin/main means: the remote's main branch was at a1b2c3d, now it's at e4f5g6h, and Git updated your local copy of it (origin/main). Your own main branch did not move.

Fetch is safe. It never loses your work, never causes merge conflicts, never changes files you are editing. Run it any time.

Pulling: fetch and merge in one step ​

git pull runs git fetch followed by git merge:

bash
git pull origin main

This fetches commits from origin's main branch, then merges them into your current branch. If you are on main and your remote tracking branch is origin/main, you can just type:

bash
git pull

Git figures out the upstream branch from config. When the merge produces conflicts, Git stops and marks the conflicted files. Resolve them, git add the resolved files, then git commit to finish the merge.

Pull with rebase ​

Many teams prefer rebasing instead of merging when pulling:

bash
git pull --rebase origin main

This replays your local commits on top of the fetched commits, producing a linear history. If you want this as the default:

bash
git config --global pull.rebase true

The beginner guide on merging covers what to do when conflicts happen during either pull strategy.

Pushing: send your commits to a remote ​

bash
git push origin main

Output:

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 412 bytes | 412.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), remote: Resolving deltas: 100% (2/2)
To https://github.com/user/project.git
   a1b2c3d..e4f5g6h  main -> main

This sends your commits to origin's main branch. Git only sends the commits the remote does not already have (the "delta").

First push of a new branch ​

When you create a local branch and want to push it for the first time:

bash
git push -u origin my-feature

The -u flag (short for --set-upstream) sets up tracking: it tells Git that your local my-feature should track origin/my-feature. After that, you can just run git push and git pull without specifying the remote or branch.

What happens when push is rejected ​

If the remote has commits you do not have locally, Git rejects the push:

 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/user/project.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.

Fix it by pulling (or fetching and rebasing) first, then pushing again.

The full workflow in practice ​

bash
# Get a project
git clone https://github.com/user/project.git
cd project

# Create a branch, make changes
git checkout -b fix-login
echo "fix" >> README.md
git add README.md
git commit -m "fix: typo in readme"

# Push the branch and set upstream
git push -u origin fix-login

# Someone else pushed to main while you worked
git checkout main
git pull            # get their changes

# Back to your branch, rebase on latest main
git checkout fix-login
git rebase main     # or: git pull --rebase origin main

# Push updated branch
git push

What's happening here ​

CommandWhat Git does locallyWhat Git does over the network
git clone <url>Creates .git, sets up origin remote, checks out default branchDownloads all reachable objects from remote
git fetchUpdates remote-tracking refs (origin/main)Downloads new objects the remote has but you don't
git pullRuns fetch, then merge (or rebase) of remote into current branchSame network traffic as fetch
git pushPacks local commits into a packfileUploads packfile, then updates remote ref
git remote -vReads .git/configNone

Common mistakes ​

MistakeWhat happensHow to fix
git push without upstream setGit does not know where to pushUse git push -u origin <branch> the first time
git push when remote is aheadPush rejected (non-fast-forward)git pull --rebase first, then push
Forgetting git fetch before checking branch statusgit status shows outdated infoRun git fetch first to sync remote-tracking refs
git pull with uncommitted workMerge conflict on your uncommitted changesgit stash, pull, git stash pop
Pushing to the wrong remoteCommits go to unintended repositorygit push origin main specifies the target; check git remote -v
Cloning with HTTPS when you need SSHGit prompts for password every timeChange remote URL: git remote set-url origin git@github.com:user/project.git

Quick reference ​

bash
git clone <url> [dir]           # Clone a repository
git remote -v                   # List remotes
git remote add <name> <url>     # Add a remote
git fetch [remote]              # Download objects and refs
git pull [remote] [branch]      # Fetch + merge (or rebase)
git pull --rebase               # Fetch + rebase
git push [remote] [branch]      # Upload commits
git push -u origin <branch>     # Push and set upstream

Next steps ​

Now that you can move commits between repositories, read the guide on remote-tracking branches to understand what origin/main really is and how git status decides whether you are ahead or behind. Then try the branching basics guide for the full branch workflow.

Git Master — Git Reference for Every Skill Level