Appearance
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.gitOutput:
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-folderViewing and managing remotes
List the remotes you have:
bash
git remote -vOutput:
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.gitRename a remote:
bash
git remote rename origin upstreamRemove a remote:
bash
git remote remove stale-backupFetching: 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 originOutput:
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/mainThe 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 mainThis 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 pullGit 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 mainThis 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 trueThe 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 mainOutput:
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 -> mainThis 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-featureThe -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 pushWhat's happening here
| Command | What Git does locally | What Git does over the network |
|---|---|---|
git clone <url> | Creates .git, sets up origin remote, checks out default branch | Downloads all reachable objects from remote |
git fetch | Updates remote-tracking refs (origin/main) | Downloads new objects the remote has but you don't |
git pull | Runs fetch, then merge (or rebase) of remote into current branch | Same network traffic as fetch |
git push | Packs local commits into a packfile | Uploads packfile, then updates remote ref |
git remote -v | Reads .git/config | None |
Common mistakes
| Mistake | What happens | How to fix |
|---|---|---|
git push without upstream set | Git does not know where to push | Use git push -u origin <branch> the first time |
git push when remote is ahead | Push rejected (non-fast-forward) | git pull --rebase first, then push |
Forgetting git fetch before checking branch status | git status shows outdated info | Run git fetch first to sync remote-tracking refs |
git pull with uncommitted work | Merge conflict on your uncommitted changes | git stash, pull, git stash pop |
| Pushing to the wrong remote | Commits go to unintended repository | git push origin main specifies the target; check git remote -v |
| Cloning with HTTPS when you need SSH | Git prompts for password every time | Change 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 upstreamNext 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.