Git

Remote Repositories

Git · 9 entries

Add a Remote

syntax
git remote add <name> <url>
example
git remote add origin https://github.com/user/project.git
git remote add upstream https://github.com/original/project.git

Note By convention, 'origin' is your fork or main remote, and 'upstream' is the original repo you forked from. You can have multiple remotes.

View Remotes

syntax
git remote -v
example
git remote -v
output
origin    https://github.com/user/project.git (fetch)
origin    https://github.com/user/project.git (push)
upstream  https://github.com/original/project.git (fetch)
upstream  https://github.com/original/project.git (push)

Note The -v flag shows the full URLs. Without it, you only see remote names. Fetch and push URLs can differ if configured separately.

Fetch from Remote

syntax
git fetch [remote] [branch]
example
git fetch origin
git fetch origin main
git fetch --all
output
remote: Counting objects: 15, done.
From https://github.com/user/project
   a1b2c3d..f4e5d6c  main -> origin/main

Note Fetch downloads new data but does NOT modify your working directory or merge anything. It's always safe to run. Use --all to fetch from every configured remote.

Pull (Fetch + Merge)

syntax
git pull [remote] [branch]
example
git pull origin main

# Pull with rebase instead of merge:
git pull --rebase origin main
output
Updating a1b2c3d..f4e5d6c
Fast-forward
 src/app.js | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Note git pull = git fetch + git merge. Use --rebase to replay your local commits on top of the fetched changes (cleaner linear history). Some teams mandate pull --rebase to avoid unnecessary merge commits.

Push to Remote

syntax
git push [remote] [branch]
example
git push origin main
git push origin feature/payments
output
Counting objects: 5, done.
To https://github.com/user/project.git
   a1b2c3d..f4e5d6c  main -> main

Note Push will be rejected if the remote has commits you don't have locally. Pull first, resolve any conflicts, then push again. Avoid --force unless you truly understand the consequences (see Common Mistakes).

Set Upstream Tracking Branch

syntax
git push -u origin <branch>
git branch --set-upstream-to=origin/<branch>
example
# First push of a new branch — set tracking:
git push -u origin feature/payments

# After this, just use:
git push
git pull
output
Branch 'feature/payments' set up to track remote branch 'feature/payments' from 'origin'.

Note The -u flag (or --set-upstream-to) links your local branch to a remote branch. After that, plain git push and git pull know where to go without specifying the remote and branch every time.

Prune Stale Remote Branches

syntax
git remote prune <remote>
git fetch --prune
example
git remote prune origin

# Or prune during fetch:
git fetch --prune
output
Pruning origin
 * [pruned] origin/feature/old-feature

Note When someone deletes a remote branch, your local reference to it lingers. Pruning removes these stale references. Does not delete your local branches — only the remote-tracking references.

Rename or Remove a Remote

syntax
git remote rename <old> <new>
git remote remove <name>
example
git remote rename origin backup
git remote remove upstream

Note Renaming a remote also updates all remote-tracking branches. Removing a remote deletes all its tracking branches and configuration entries.

Push All Branches

syntax
git push --all <remote>
git push --tags <remote>
example
git push --all origin
git push --tags origin

Note Pushes every local branch to the remote. Be cautious with this in shared repos — you might push experimental branches others don't want. --tags pushes all tags separately.