A remote is a connection between a local Git repository and a repository stored on another server.
Remote repositories allow developers to upload, download, and collaborate on projects with others.
Platforms like GitHub, GitLab, and Bitbucket use remote repositories.
Local Repository 💻
A repository stored on your own computer.
It contains:
- Project files
- Commits
- Branches
- History
Remote Repository ☁️
A repository stored on a remote server.
It is used for:
- Backup
- Collaboration
- Sharing code
- Team development
When cloning a repository, Git usually creates a remote called:
origin
Origin is the default name for the main remote repository.
Example:
origin -> https://github.com/user/project.git
Show remote names:
git remote
Example:
origin
Show detailed information:
git remote -v
Example:
origin https://github.com/user/project.git (fetch)
origin https://github.com/user/project.git (push)
Add a remote connection:
git remote add name url
Example:
git remote add origin https://github.com/user/project.git
The name "origin" is commonly used for the main remote.
Remove a remote connection:
git remote remove remote-name
Example:
git remote remove origin
Update an existing remote URL:
git remote set-url origin new-url
Example:
git remote set-url origin https://github.com/user/new-project.git
Fetch downloads information from a remote repository without changing your files.
Command:
git fetch
Fetch from a specific remote:
git fetch origin
Fetch updates:
- New branches
- New commits
- Remote history
Pull downloads changes and automatically integrates them.
Command:
git pull
Equivalent to:
git fetch
git merge
Example:
git pull origin main
Push uploads local commits to a remote repository.
Command:
git push
Push to a specific remote branch:
git push origin main
Example:
git push origin feature-login
An upstream branch connects a local branch with a remote branch.
Command:
git push -u origin branch-name
Example:
git push -u origin main
After this, Git remembers the remote branch.
Future pushes can use:
git push
A tracking branch automatically knows which remote branch it follows.
View tracking information:
git branch -vv
Example:
main abc123 [origin/main] Latest changes
Clone downloads a complete repository.
Command:
git clone repository-url
Example:
git clone https://github.com/user/project.git
After cloning:
- Remote named origin is created.
- Remote branches become available.
- Local main branch tracks origin/main.
List remote branches:
git branch -r
List all branches:
git branch -a
Remote branches are references to branches stored on the server.
Delete a remote branch:
git push origin --delete branch-name
Example:
git push origin --delete feature-old
Rename a branch locally:
git branch -m old-name new-name
Push the new branch:
git push origin new-name
Delete the old remote branch:
git push origin --delete old-name
Clone project:
git clone repository-url
Create changes:
git add .
git commit -m "Add new feature"
Upload changes:
git push
Download updates:
git pull
- Keep remote URLs correct.
- Pull changes before starting new work.
- Push commits regularly.
- Use meaningful branch names.
- Protect main branch.
- Avoid force pushing shared branches.
- Review changes before pushing.
git remote
List remote repositories.
git remote -v
Show remote URLs.
git remote add
Add a remote.
git fetch
Download remote information.
git pull
Download and merge changes.
git push
Upload commits.
git branch -r
Show remote branches.
Continue to:
Stash Guide