Who is this for?
Developers who need to:
- Work with GitHub, GitLab, or other remote Git servers
- Manage remote repositories
- Collaborate with team members
What is covered?
This cheat sheet provides a comprehensive overview of Git remotes, including:
- Essential remote operations and commands
- Common terminology and concepts
- Repository structures and relationships
- Best practices for remote collaboration
- Common workflows and scenarios
- Troubleshooting tips
Whether you're working on personal projects or collaborating in a team, this guide will help you understand and effectively manage remote repositories.
What are Git Remotes?
Git remotes are versions of your repository hosted on the Internet or network. They enable collaboration and code sharing between team members.
Remote Commands
# List all remotes and their URLs
git remote -v
# Show remote branch details
git ls-remote <remote_name> <branch_name>
# Extract remote URL from Git config
git config --get remote.origin.url
# Alternative: grep "url =" .git/config
# Update remote URL
git remote set-url origin <new_url>
# Add new remote
git remote add <remote_name> <remote_url>
# Remove remote
git remote remove <remote_name>
# Rename remote
git remote rename <old_name> <new_name>
# Prune deleted remote branches
git remote prune <remote_name>
# Fetch from remote
git fetch <remote_name>
# Fetch specific branch from remote
git fetch <remote_name> <branch_name>
# Fetch all remote branches and tags
git fetch --all --tags
# Pull from remote (fetch + merge)
git pull <remote_name> <branch_name>
# Push to remote
git push <remote_name> <branch_name>
# Push to specific remote and branch
git push <remote_name> <local_branch>:<remote_branch>
# Force push (use with caution!)
git push --force-with-lease # Safer alternative to --force
# Acts as a safety mechanism by checking if the remote
# branch has been modified since your last fetch.
# This helps prevent accidentally overwriting team
# members' work, which can happen with a regular
# --force push.
# Check remote tracking branches
git branch -vv
# Remove stale remote-tracking branches
git remote prune origin --dry-run # Preview what will be pruned
git remote prune origin # Actually prune
git fetch --prune # Fetch and prune in one command
# Mirror a repository (including all refs)
git clone --mirror <repository_url>
# Verify remote connections
git remote verify <remote_name>
# Sync fork with upstream
git fetch upstream
git checkout main
git merge upstream/main
Terminology
# Repository Concepts
repository - A directory containing your project and its version history
local - Your copy of the repository on your machine
remote - A repository hosted on the internet or network (GitHub, GitLab, etc.)
fork - A copy of someone else's repository under your account
clone - A local copy of a remote repository
# Common Remote Names
origin - The default name Git gives to the primary remote repository you cloned from
upstream - Commonly used name for the original repository you forked from
# Branches and References
main/master - The default primary branch name (main is the new standard, master was historical)
HEAD - A pointer to the current branch/commit you're working on
tracking branch - A local branch that tracks a remote branch (e.g., main tracks origin/main)
# Data Transfer Operations
fetch - Download changes from remote without integrating them
pull - Download changes from remote and integrate them (fetch + merge)
push - Upload your local changes to a remote repository
# Merging and Integration
merge - Combine changes from one branch into another
fast-forward - A merge where no new commit is created (target branch just moves forward)
merge commit - A new commit created when combining changes from two branches
conflict - When changes in two branches modify the same code and cannot auto-merge
rebase - Alternative to merge: replays your changes on top of target branch
cherry-pick - Apply a specific commit from one branch to another
Repository Structure Example
Your Local Repo (local)
│
├── origin (your fork on GitHub)
│ └── main branch
│
└── upstream (original repository)
└── main branch
Example:
- upstream: https://github.com/original-author/project
- origin: https://github.com/your-username/project
- local: /home/your-username/project
Remote Setups Example
Simple Setup (Direct):
local ↔ origin (primary remote repository)
Fork Setup (Contributing to others):
local ↔ origin (your fork) ↔ upstream (original repository)
Multiple Remotes:
local ↔ origin (primary)
↔ staging (testing server)
↔ production (live server)
Best Practices
- Always verify the remote URL before pushing sensitive code
- Use SSH over HTTPS when possible for better security
- Set up SSH keys with passphrase for secure authentication
- Use
--force-with-lease
instead of--force
to prevent overwriting others' work - Regularly sync with upstream repositories when working with forks
- Set up branch protection rules on remote repositories for critical branches
- Use meaningful names for remotes (e.g., 'upstream' for original repo, 'origin' for fork)
- Configure remote-tracking branches explicitly for better control
- Implement backup strategies using multiple remotes
- Use
.gitignore
properly to avoid pushing sensitive or unnecessary files - Review remote changes before merging using
git fetch
+git diff
- Document your repository structure and Git workflows in a README.md file
Key Points
Working with remotes is a fundamental part of Git and collaborative development. Remember these key points:
- Always synchronise with your remote before starting new work
- Regularly commit and push your changes to avoid large, complex merges
- Use meaningful commit messages to help your colleagues understand your changes
- Keep your local repository organised and regularly prune old branches
- Document your team's Git workflow to maintain consistency
- Consider using Git aliases for frequently used remote commands
- Back up your work regularly by pushing to remote repositories
- When in doubt, create a new branch rather than working directly on main
For more detailed information, consult:
- Git official documentation: https://git-scm.com/doc
- GitHub Guides: https://guides.github.com/
- Your team's internal Git guidelines
Top comments (3)
This is great @j3zz3r. Thanks for this really useful cheatsheet.
I've updated the cmd
git push --force-with-lease
to provide better context about what the command does and why it's safer than a regular force push.
If you have any suggested topics or have any questions, please feel free to comment or DM me, cheers.
Amazing 👌👌
Thanks for following my github
github.com/Mohammadparsa1384