Working with Remote Repositories¶
Master the essential commands for collaborating with others through remote Git repositories.
Understanding Remote Repositories¶
Remote Repository Concepts¶
A remote repository is a version of your project that is hosted on the internet or network somewhere. Git's distributed nature allows multiple developers to work independently and then share their changes.
graph LR
A["Local Repo A (Developer A)"] <--> B["Remote Repo (Server)"]
B <--> C["Local Repo B (Developer B)"]
Common Remote Operations¶
- Fetch: Download objects and refs from remote without integrating them
- Pull: Fetch + integrate remote changes into your current branch
- Push: Upload your local branch commits to a remote repository
- Clone: Create a local copy of a remote repository
Local vs Remote¶
Understanding the relationship between your local and remote branches:
gitGraph
commit id: "Initial commit"
commit id: "Second commit"
branch origin/main
checkout origin/main
commit id: "Third commit"
checkout main
commit id: "Fourth commit" type: HIGHLIGHT tag: "HEAD"
commit id: "Fifth commit"
The diagram above shows:
- Your local branch
mainhas two commits ahead of the remote - The remote tracking branch
origin/mainrepresents your last known state of the remote HEADpoints to your current position on the local main branch
After pushing:
gitGraph
commit id: "Initial commit"
commit id: "Second commit"
branch origin/main
checkout main
commit id: "Third commit"
checkout origin/main
commit id: "Third commit"
checkout main
commit id: "Fourth commit"
commit id: "Fifth commit" type: HIGHLIGHT tag: "HEAD"
checkout origin/main
commit id: "Fourth commit"
commit id: "Fifth commit"
After pushing, the remote branch is updated with your local commits, and your local tracking reference to origin/main is updated to match.
Setting Up Remote Connections¶
Adding Remotes¶
Connect your local repository to a remote repository:
Add a remote named "origin"¶
Add another remote with a different name¶
Viewing Remotes¶
Inspect your remote connections:
List all remotes¶
Show detailed information about remotes (URLs)¶
Show information about a specific remote¶
Renaming Remotes¶
Change the name of a remote:
Rename remote from "origin" to "destination"¶
Removing Remotes¶
Delete a remote connection:
Remove the remote named "upstream"¶
Working with Multiple Remotes¶
Multiple remotes are common in open source projects:
Step 1: Add original repository as "upstream"¶
Step 2: Fetch from upstream¶
Step 3: Merge changes from upstream's main branch¶
💡 Tip: In a fork workflow, "origin" typically points to your fork and "upstream" points to the original repository.
Fetching Updates¶
Basic Fetch¶
Download objects and refs from a remote without merging:
Fetch updates from the "origin" remote¶
Fetch updates from a specific remote¶
Fetching Specific Branches¶
Fetch only certain branches from remote:
Fetch a specific branch¶
Fetch multiple specific branches¶
Pruning When Fetching¶
Clean up deleted remote branches:
Fetch and remove any remote-tracking references that no longer exist¶
Short form¶
Configure Git to always prune when fetching¶
Fetch All Remotes¶
Fetch updates from all configured remotes:
Fetch from all remotes¶
💡 Tip: Fetching is a safe operation that doesn't change your working directory or current branch.
Pulling Changes¶
Basic Pull¶
Fetch from a remote and integrate changes into the current branch:
Pull from the remote tracking branch¶
Pull from a specific remote and branch¶
Pull with Rebase¶
Pull using rebase instead of merge:
Pull with rebase¶
Pull from specific remote and branch with rebase¶
Configure pull to use rebase by default¶
Pull with Specific Options¶
Customize pull behavior:
Pull with verbose output¶
Pull without auto-commit (leaves changes staged)¶
Pull only if it can be fast-forwarded¶
Pull vs Fetch + Merge¶
git pull is essentially a combination of git fetch and git merge (or git rebase with --rebase):
Step 1: Fetch remote changes¶
Step 2: Merge remote changes (equivalent to pull)¶
These two steps combined are equivalent to:
⚠️ Warning:
git pullcan lead to unexpected merge conflicts. For more control, some developers prefer using fetch and merge as separate steps.
Pushing Changes¶
Basic Push¶
Upload local branch commits to remote repository:
Push current branch to its tracking branch¶
Push current branch to a specific remote branch¶
Pushing to Specific Remotes¶
Select which remote to push to:
Push to a remote other than origin¶
Push to all remotes¶
Setting Upstream Branch¶
Configure branch tracking during push:
Push and set upstream branch¶
Or:
Check which branches are tracking what¶
Force Push¶
Override remote branch history (use with caution):
Force push (DANGER: overwrites remote history)¶
Safer force push¶
⚠️ Warning: Force pushing rewrites history on the remote. Never force push to shared branches unless you're absolutely certain it won't affect other team members.
Push Tags¶
By default, git push doesn't transfer tags:
Push a specific tag¶
Push all tags¶
Push all annotated tags¶
Managing Remote Branches¶
Listing Remote Branches¶
View branches in remote repositories:
List remote-tracking branches¶
List both local and remote-tracking branches¶
Show latest commit on each branch¶
Creating Remote Branches¶
Create a branch on the remote repository:
Push local branch to create remote branch with same name¶
Create remote branch with different name¶
Tracking Remote Branches¶
Set up local branches to track remote branches:
Create a local branch that tracks a remote branch¶
Set an existing local branch to track a remote branch¶
Create and switch to a branch that tracks a remote branch¶
Deleting Remote Branches¶
Remove branches from remote repositories:
Delete a remote branch¶
Alternative syntax¶
Authentication and Credentials¶
HTTPS Authentication¶
Authenticate with username and password:
Clone with HTTPS URL¶
When you push, you'll be prompted for credentials.
SSH Authentication¶
Authenticate using SSH keys (more secure):
Step 1: Generate SSH key pair¶
ssh-keygen -t ed25519 -C "[email protected]"
Step 2: Show public key to add to GitHub/GitLab¶
Step 3: Clone with SSH URL¶
git clone [email protected]:username/repo.git
Credential Helpers¶
Store credentials to avoid typing them repeatedly:
Cache credentials in memory for 15 minutes¶
Store credentials permanently (in plaintext)¶
Use the OS credential manager¶
For Windows:
For macOS:
Personal Access Tokens¶
Modern authentication for HTTPS (replacing passwords):
# Use token as password when prompted
git clone https://github.com/username/repo.git
# Username: your-username
# Password: your-personal-access-token
💡 Tip: GitHub, GitLab, and other platforms now require personal access tokens instead of passwords for Git operations.
Common Git Workflows¶
This section covers standard workflows for different collaboration models in Git.
Fork and Pull Request Workflow¶
A workflow commonly used in open source projects where you don't have direct write access to the repository.
gitGraph
commit id: "Initial project"
commit id: "More work"
branch fork/main
checkout fork/main
commit id: "Clone fork"
branch feature
checkout feature
commit id: "Make changes"
commit id: "Finalize feature"
checkout fork/main
merge feature
checkout main
merge fork/main id: "PR merged"
Step 1: Fork the Repository¶
Create your own copy of the repository on the remote server (done through GitHub/GitLab web interface).
Step 2: Clone Your Fork¶
Create a local copy of your fork:
Step 3: Add Original Repository as Upstream¶
Connect to the original repository to fetch updates later:
Step 4: Create Feature Branch¶
Create a branch for your work:
Step 5: Make and Commit Changes¶
Implement your feature or fix:
Step 6: Push to Your Fork¶
Upload your changes to your remote fork:
Step 7: Create Pull Request¶
Submit your changes for review (done through GitHub/GitLab web interface).
Direct Collaboration Workflow¶
A workflow used when you have direct write access to the shared repository.
gitGraph
commit id: "Project start"
commit id: "Previous work"
branch feature-branch
checkout feature-branch
commit id: "Add feature"
commit id: "Refine feature"
checkout main
merge feature-branch id: "Merge feature"
commit id: "Hotfix"
Step 1: Clone the Shared Repository¶
Get a local copy of the shared repository:
Step 2: Create a Feature Branch¶
Create a branch for your specific work:
Step 3: Make and Commit Changes¶
Implement your feature or fix:
Step 4: Regularly Fetch Team Changes¶
Stay up to date with others' work:
Or alternatively:
Step 5: Push Your Branch¶
Share your work with the team:
Step 6: Merge or Create Pull Request¶
Either merge directly or create a pull request for review, depending on team practices:
Fork Synchronization Workflow¶
Keeping your fork up to date with the original repository.
gitGraph
commit id: "Original state"
branch fork/main
checkout fork/main
commit id: "Your fork"
checkout main
commit id: "Upstream updates"
commit id: "More upstream work"
checkout fork/main
merge main id: "Sync from upstream"
commit id: "Your new work"
Step 1: Add Upstream Remote¶
Connect to the original repository (if not done already):
Step 2: Fetch Upstream Changes¶
Download the latest updates from the original repository:
Step 3: Switch to Your Main Branch¶
Move to your local main branch:
Step 4: Merge Upstream Changes¶
Incorporate the updates into your local branch:
Step 5: Push Updates to Your Fork¶
Update your remote fork with the synced changes:
Gitflow Workflow¶
A more structured workflow for larger projects with scheduled releases.
gitGraph
commit id: "Project start"
branch develop
checkout develop
commit id: "Development work"
branch feature/login
checkout feature/login
commit id: "Add login"
commit id: "Refine login"
checkout develop
merge feature/login
branch release/1.0
checkout release/1.0
commit id: "Release prep"
checkout main
merge release/1.0 id: "Release 1.0"
checkout develop
merge release/1.0
branch hotfix/1.0.1
checkout hotfix/1.0.1
commit id: "Critical fix"
checkout main
merge hotfix/1.0.1 id: "Release 1.0.1"
checkout develop
merge hotfix/1.0.1
Step 1: Initialize Gitflow¶
Set up the Gitflow branching structure:
Or manually create develop branch:
Step 2: Start a Feature¶
Create a feature branch from develop:
Or manually:
Step 3: Complete a Feature¶
Finish the feature, merging back to develop:
Or manually:
Step 4: Create a Release¶
Branch off for release preparation:
Or manually:
Step 5: Finish a Release¶
Merge to main and back to develop:
Or manually:
Step 6: Create a Hotfix¶
Fix critical issues in production:
Or manually:
Step 7: Finish a Hotfix¶
Apply the fix to both main and develop:
Or manually:
Troubleshooting Remote Operations¶
Common Push Errors¶
Rejected non-fast forward push:
Solution:
Step 1: Pull first to integrate remote changes¶
Step 2: Or force push if you're sure (use with caution)¶
Failed to push some refs:
Solution:
Option 1: Fetch and merge the remote changes¶
Option 2: Pull (fetch + merge in one step)¶
Common Pull Errors¶
Local changes would be overwritten:
Solution:
Option 1: Stash your changes¶
Option 2: Commit your changes first¶
Merge conflicts:
Solution:
Step 1: Resolve conflicts in the files¶
Edit the files to resolve conflicts.
Step 2: Mark conflicts as resolved¶
Alternative: Abort the pull¶
Authentication Issues¶
Permission denied errors:
Solutions:
Step 1: Check remote URL¶
Step 2: Update URL with correct credentials¶
git remote set-url origin https://[email protected]/username/repo.git
Alternative: Switch to SSH if you have it set up¶
git remote set-url origin [email protected]:username/repo.git
Command Summary¶
| Operation | Command | Description |
|---|---|---|
| Add Remote | git remote add <name> <url> | Connect to remote repo |
| View Remotes | git remote -v | List all remotes with URLs |
| Fetch | git fetch <remote> | Download without merging |
| Pull | git pull <remote> <branch> | Download and merge |
| Pull with Rebase | git pull --rebase | Download and rebase |
| Push | git push <remote> <branch> | Upload local commits |
| Set Upstream | git push -u <remote> <branch> | Push and track branch |
| Delete Remote Branch | git push <remote> --delete <branch> | Remove remote branch |
| List Remote Branches | git branch -r | Show remote branches |