DEV Community

ronak navadia
ronak navadia

Posted on

The Complete Beginner’s Guide to Git: Setup, SSH, Tokens, Remotes, and More.

If you’re learning development, sooner or later you'll hear everyone saying “use Git!”
But no one really stops to explain it like a normal human being.

Let's fix that.

This post is your no-jargon, no-overcomplicated guide to Git.
We’ll start with what Git is, why you need it, and how to set it up properly — even covering the small details like SSH vs HTTPS vs Tokens.

What is Git?(Magic Time Machine for Your Code)

Imagine you're writing a story and want to save versions as you go—so you can look back or undo changes. Git does that for coding projects.

🔧 Definition:
Git is a version control system. It tracks changes, helps you work with others, and lets you go back to earlier versions of your files.

🧠 Why Use Git?

  • Track changes over time
  • Collaborate without conflicts
  • Experiment safely in branches
  • Backup your work using platforms like GitHub

GitHub

📘 Example:
On Monday: <h1>Hello World</h1>
On Tuesday: <h1>Welcome to My Website</h1>
With Git, you can easily switch between these versions and see what changed.

🖥️ How It Works:

  • Start with git init
  • Save changes using git commit
  • Upload to GitHub using git push

What is GitHub, GitLab, Bitbucket? (Code Banks)

Git is only local by default — like saving your essay on your personal laptop.

But what if:

  • You want a backup in case your laptop crashes?
  • You want to share it with friends across the world?

You need an online bank for your code like GitHub, GitLab, Bitbucket

Think of GitHub / GitLab / Bitbucket as Google Drive for Code.

  1. GitHub – The most widely used, great for open source and teams. Features include issues, pull requests, and GitHub Actions for automation.
  2. GitLab – Offers Git hosting plus built-in DevOps tools like CI/CD. Great for teams wanting an all-in-one solution.
  3. Bitbucket – Made by Atlassian (creators of Jira), it’s ideal for teams already using Jira or Trello. Includes Bitbucket Pipelines for CI/CD.

How to Set Up Git on Your Local Machine (Step-by-Step)

🖥️ Step 1: Install Git

  • Windows: Download from git-scm.com and run the installer (default settings are fine). Open Git Bash after install.
  • macOS: Run git in Terminal—macOS will prompt you to install Xcode CLI tools.
  • Linux:
sudo apt update  
sudo apt install git  
Enter fullscreen mode Exit fullscreen mode

🧪 Check Git Version

git --version  
Enter fullscreen mode Exit fullscreen mode

⚙️ Step 2: Set Your Identity
Tell Git who you are:

git config --global user.name "Your Name"  
git config --global user.email "you@example.com"  
Enter fullscreen mode Exit fullscreen mode

Check settings with:

git config --list  
Enter fullscreen mode Exit fullscreen mode

📁 Step 3: Initialize Git
In your project folder:

cd your/project/path  
git init  
Enter fullscreen mode Exit fullscreen mode

📝 Step 4: Make Your First Commit
Create a file (e.g., index.html), then run:

git add index.html  
git commit -m "Initial commit"  
Enter fullscreen mode Exit fullscreen mode

✅ Your project is now tracked by Git!

🔍 Optional: Local Git Config vs Global Git Config

  • --global: Applies settings to all projects on your computer.
  • (No --global): Applies settings only to the current project (inside that .git folder).
git config user.email "another@example.com"
Enter fullscreen mode Exit fullscreen mode

Only applies to the current project.


Understanding What Remote and Origin in Git is.

🌐 What’s a Remote?
When you use Git on your computer, everything is local. But to share or back up your project online (like on GitHub), you need to connect it to a remote.

🛰️ Remote = Online version of your project
When you run:

git push  
Enter fullscreen mode Exit fullscreen mode

You’re saying: “Send my changes to the remote.”

When you run:

git pull  
Enter fullscreen mode Exit fullscreen mode

You’re saying: “Get the latest changes from the remote.”

🔖 What is origin?
When you first connect your local project to a remote, Git gives it a default name:
origin = nickname for your main remote (you can rename it, but "origin" is the standard).

🧪 Example: Connect to GitHub
If your GitHub repo is:
https://github.com/yourname/myproject.git

Connect it with:

git remote add origin https://github.com/yourname/myproject.git  
Enter fullscreen mode Exit fullscreen mode

Now push your code:

git push -u origin main  
Enter fullscreen mode Exit fullscreen mode

🔍 View Remotes

git remote -v  
Enter fullscreen mode Exit fullscreen mode

🔄 Multiple Remotes? No problem.
You can add more:

git remote add github https://github.com/you/repo.git  
git remote add gitlab https://gitlab.com/you/repo.git
Enter fullscreen mode Exit fullscreen mode

Push to each one:

git push github main  
git push gitlab main  
Enter fullscreen mode Exit fullscreen mode

Understanding the Difference Between HTTPS and SSH URLs in Git

When connecting your local Git project to GitHub (or similar), you'll see two types of URLs:

Both work, but they connect differently.

🔐 HTTPS

  • Uses your username + token to authenticate
  • Easy to set up—no special tools or keys
  • But you might have to enter your token often (unless cached)
  • GitHub doesn’t allow plain passwords anymore

🛡️ SSH

  • Uses a public/private key pair (no password after setup)
  • Takes a bit more time to set up
  • More secure and seamless for daily use

What is SSH? And How to Set It Up for Git (Step-by-Step)

🔐 What is SSH (Simple Version)
SSH (Secure Shell) is a safe way for your computer to connect to services like GitHub without needing to type your password every time.

Instead of logging in manually, you use:

  • Public Key → Added to GitHub/GitLab/etc.
  • Private Key → Stays on your computer and proves your identity automatically

It's like using a digital key to unlock the door without knocking.

🛠️ Set Up SSH for Git (Quick Steps)
✅ 1. Check for existing keys:

Windows (Git Bash):

ls -al ~/.ssh
Enter fullscreen mode Exit fullscreen mode

Windows (PowerShell):

Get-ChildItem -Path $env:USERPROFILE\.ssh
Enter fullscreen mode Exit fullscreen mode

Windows (CMD):

dir %USERPROFILE%\.ssh
Enter fullscreen mode Exit fullscreen mode

macOS / Linux:

ls -al ~/.ssh
Enter fullscreen mode Exit fullscreen mode

🔐 2. Generate a new key:

ssh-keygen -t ed25519 -C "your_email@example.com"  
Enter fullscreen mode Exit fullscreen mode

Press Enter through prompts. It creates two files:

  • Private key: ~/.ssh/id_ed25519 (keep safe)
  • Public key: ~/.ssh/id_ed25519.pub (share)

🚀 3. Add your key to the SSH agent:
The SSH agent is a background program that securely holds your SSH key in memory, so you don’t have to re-enter your passphrase every time you use Git. It makes authentication seamless and more secure during your session.

eval "$(ssh-agent -s)"  
ssh-add ~/.ssh/id_ed25519  
Enter fullscreen mode Exit fullscreen mode

🌐 4. Add your public key to your Git host:
Show the key:

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Copy the output and paste it into:

  • GitHub → Settings → SSH Keys
  • GitLab → Preferences → SSH Keys
  • Bitbucket → Personal Settings → SSH Keys

🔍 5. Test your connection:

ssh -T git@github.com  
Enter fullscreen mode Exit fullscreen mode

You should see:
“Hi username! You've successfully authenticated...”


Different Ways to Connect Git with Online Services (GitHub, GitLab, Bitbucket)

🔹 Connecting Git Using Username and Password (Only on GitLab and Bitbucket)

🚫 GitHub no longer allows username + password for Git over HTTPS.
✅ This still works on GitLab and Bitbucket (for now), but is considered less secure

How it works:
You clone the repo using the HTTPS URL:

git clone https://gitlab.com/username/project.git
Enter fullscreen mode Exit fullscreen mode

When you push:

git push
Enter fullscreen mode Exit fullscreen mode

Git will prompt you to enter:

  • Your username
  • Your password

💡 You can cache these credentials using Git Credential Manager (on Windows/macOS) or helper scripts.

Why avoid it?

  • Easily forgettable.
  • Passwords can be compromised.
  • Many platforms are moving away from this for security reasons.

🔹 b. Connecting Git Using SSH (Recommended Method)
**How it works:
You clone the repo using the SSH URL:

git clone git@gitlab.com:username/project.git
Enter fullscreen mode Exit fullscreen mode

Your SSH key is automatically used to authenticate (no username or password prompts).

✅ Secure and fast once set up.
🔁 Works well for frequent Git users or developers managing many projects.

🔹 c. Connecting Git Using Personal Access Token (PAT)

🔐 GitHub requires a Personal Access Token (PAT) for HTTPS connections.
GitLab and Bitbucket support PATs as an alternative to passwords.

Step-by-Step:

Generate a token from your Git platform:

Use HTTPS to clone the repo:

git clone https://github.com/yourusername/project.git
Enter fullscreen mode Exit fullscreen mode

When prompted:

  • Username: your GitHub/GitLab username
  • Password: your access token

💡 You can use a credential helper to cache the token so you don’t retype it every time:

git config --global credential.helper cache
Enter fullscreen mode Exit fullscreen mode

Handling Multiple Git Accounts on the Same Device

If you work with more than one GitHub, GitLab, or Bitbucket account (e.g., one personal and one work), things can get tricky.

Here’s how to manage multiple accounts without mixing them up.

✅ Method 1: Use SSH with Separate Keys for Each Account
Generate a second SSH key:

ssh-keygen -t ed25519 -C "you@work-email.com" -f ~/.ssh/id_ed25519_work
Enter fullscreen mode Exit fullscreen mode

Add it to the SSH agent:

ssh-add ~/.ssh/id_ed25519_work
Enter fullscreen mode Exit fullscreen mode

Add the public key to your second Git account (e.g., work GitHub/GitLab).

Edit ~/.ssh/config to tell Git which key to use for each domain:

# Personal GitHub
Host github.com-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519

# Work GitHub
Host github.com-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work

Enter fullscreen mode Exit fullscreen mode

Clone using custom hostname:

git clone git@github.com-work:workuser/project.git
Enter fullscreen mode Exit fullscreen mode

In your local repo, set the correct remote:

git remote set-url origin git@github.com-work:workuser/project.git
Enter fullscreen mode Exit fullscreen mode

✅ Method 2: Use HTTPS + Git Credential Manager (Separate Git Identities)
You can also manage multiple accounts by:

Setting per-project Git identities:

git config user.name "Work Name"
git config user.email "work@example.com"
Enter fullscreen mode Exit fullscreen mode

Using Git Credential Manager (built-in on Windows/macOS) to store different credentials per URL.

🧾 Wrapping Up
Getting started with Git and remote repositories might feel a bit technical at first, but once you understand the core concepts—like what Git is, how remotes work, and the difference between HTTPS and SSH—it becomes second nature.

Whether you're a solo developer or working in a team, properly setting up Git and knowing how to securely connect to platforms like GitHub, GitLab, or Bitbucket will save you time and headaches down the road.

Now that you've got the essentials down, you're ready to collaborate, contribute, and code with confidence.

Happy committing! 🚀

Top comments (0)