Version control is the backbone of modern software development, and Git is its most widely used tool. Whether you’re working solo or collaborating on open-source projects, you need to know how to connect your local Linux machine to remote repositories.
In this guide, you’ll learn:
- How to authenticate via HTTPS and SSH
- How to clone, pull, and push to remotes
What You Need Before Starting #
Make sure you have:
-
Git installed:
git --versionIf missing:
sudo apt install git # Debian/Ubuntu sudo dnf install git # Fedora sudo pacman -S git # Arch -
A GitHub/GitLab account (or another Git hosting service).
-
Basic knowledge of Linux terminal commands.
Setting Your Git Identity #
Configure your username and email once for all:
git config --global user.name "Jane Doe"
git config --global user.email "[email protected]"Verify settings:
git config --listAuthentication Methods: HTTPS vs SSH #
SSH (Recommended) #
- URL format:
[email protected]:user/repo.git - Pros: No password prompts after setup.
- Steps:
- Generate a key:
ssh-keygen -t ed25519 -C "[email protected]" - Copy your key to clipboard:
cat ~/.ssh/id_ed25519.pub - Add it to GitHub/GitLab → Settings → SSH Keys.
- Test connection:
ssh -T [email protected]
- Generate a key:
HTTPS #
- URL format:
https://github.com/user/repo.git - Pros: Simple to set up.
- Steps:
- got to Github Settings → Developer Settings → Tokens
- Generate a new token and copy it
git clone https://github.com/user/repo.git Username: your-github-username Password: <paste your personal access token> - Cons: May prompt for credentials unless you enable a credential helper:
git config --global credential.helper store
Cloning a Remote Repository #
To download a repository:
git clone https://github.com/user/repo.git
# or using SSH:
git clone [email protected]:user/repo.gitThis creates a project directory containing all files and commit history. You can add --recursive after clone if the repository you are cloning contains submodules.
Linking a Local Project to a Remote #
If you already have a project and want to push it online:
git init
git remote add origin https://github.com/user/repo.git
git add .
git commit -m "Initial commit"
git push -u origin mainFetching, Pulling, and Pushing #
-
Fetch: Download remote changes without merging.
git fetch origin -
Pull: Download and merge changes into your branch.
git pull origin main -
Push: Upload your local changes.
git push origin main