Git Tutorial
- 1. Introduction to Git & VCS
- 2. Installation & Configuration
- 3. Git Architecture & Concepts
- 4. Basic Workflow (add/commit)
- 5. Git Log & History
- 6. Branching Basics
- 7. Merging & Conflict Resolution
- 8. Git Rebasing
- 9. Undoing Changes
- 10. Working with Remotes
- 11. Git Stashing
- 12. Git Tagging
- 13. Git Ignore & Attributes
- 14. Advanced Git Tools
- 15. Best Practices & Workflows
10. Working with Remotes
Git is a distributed version control system, meaning developers can work entirely offline on their local machines. However, to collaborate with others, share your code, and backup your projects, you need to work withRemote Repositories hosted on servers like GitHub, GitLab, or Bitbucket.
What is a Remote Repository?
A remote repository is a copy of your project that is hosted on the internet or a network server. You can have multiple remotes associated with your local project, but the default primary remote is almost always named origin.
Core Collaboration Commands
1. Cloning a Repository: git clone
To download an existing Git repository from a server onto your computer, copy its URL and use the clone command:
git clone https://github.com/user/project-name.gitThis does several things automatically:
- Creates a new folder named
project-nameon your filesystem. - Downloads the entire project files, commits, branches, and historical database into a hidden
.gitfolder. - Configures a remote reference named
originpointing back to the cloned URL. - Checks out the default branch (usually
main) for you to begin working.
2. Managing Remotes: git remote
If you initialized a local directory (git init) and want to link it to a newly created empty repository on GitHub:
# Link your local repo to a remote URL with the alias "origin"
git remote add origin https://github.com/user/project-name.git
# View your configured remote aliases
git remote -v3. Sharing Your Commits: git push
To upload your local commits from your current active branch to your remote server:
# Push changes to the default remote "origin" and branch "main"
git push origin main
# Shortcut: Set upstream tracking reference once, then you can just type "git push"
git push -u origin main4. Downloading Changes: git fetch vs. git pull
When collaborating with others, you will need to retrieve changes they have pushed to the server. Git provides two commands for this:
Option A: git fetch (Safe)
git fetch downloads all new commits, branches, and tags from the remote server, but **does not touch** your active working files. It is a completely safe read-only operation. You can inspect the fetched history using:
git fetch origin
git log origin/mainOption B: git pull (Convenient but can cause conflicts)
git pull is a shortcut command that performs a git fetch followed immediately by a git mergeto pull those changes into your current active local branch:
git pull origin mainBest Practice: Pull with Rebase
By default, running git pull creates a messy, unnecessary merge commit in your history every time your remote and local history diverge. To prevent this, you can configure Git to **rebase** your local commits on top of the fetched remote commits:
# Pull changes and rebase local commits (retains a clean, linear history)
git pull --rebase origin main
# Configure Git to ALWAYS use rebase by default for all pulls
git config --global pull.rebase true