🚀 Basic Git Tutorial (Windows & Linux)
What is Git?
Simply put, Git is a “Distributed Version Control System.”
- Version Control: It helps you track all the historical changes to your project. Just like having countless save points in a video game, you can return to any “saved” state at any time.
- Distributed: Everyone’s computer has a complete “copy” (repository) of the project. You can work locally (commit, create branches) without needing a constant internet connection.
1. Installing Git
💻 On Windows
- Visit the official Git website:
https://git-scm.com/ - Download the Windows installer.
- Run the installer. During the installation, keeping the default settings is usually the best choice.
- The most important step: After installation, you will get a program called Git Bash. Please always use Git Bash. It provides a command-line environment similar to Linux, and all commands in this tutorial will run there.
🐧 On Linux (Ubuntu/Debian example)
Installation on Linux is very simple. Open your terminal and run:
sudo apt update
sudo apt install git
(For other distributions, like Fedora, you can use sudo dnf install git)
2. First-Time Setup: Global Configuration
Before you make your first commit, you need to tell Git who you are. This information will be attached to every commit you make.
On both Windows (open Git Bash) and Linux (open terminal), the commands are identical.
# Set your username
git config --global user.name "Your Name"
# Set your email (Please use the email you registered on GitHub/GitLab with)
git config --global user.email "your.email@example.com"
3. How to Configure a Git Key (SSH Key)
We use SSH keys to achieve a “password-less login.” Once configured, you will no longer need to repeatedly enter your username and password when you clone and push your own projects.
The process is almost identical on Windows (Git Bash) and Linux (terminal).
Step 1: Generate an SSH Key
# Use your email as a label
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
- After running it, it will ask “Enter file in which to save the key…”
- Just press Enter to accept the default path (usually
~/.ssh/id_rsa).
- Just press Enter to accept the default path (usually
- Next, it will ask “Enter passphrase…”
- It is recommended to press Enter twice (i.e., set no passphrase) for the most convenient pull/push experience.
Step 2: View and Copy Your Public Key
Your “key” is split into two parts: a private key (id_rsa) and a public key (id_rsa.pub). You need to copy the public key (.pub) to the GitHub or GitLab website.
# Display the contents of the public key file
cat ~/.ssh/id_rsa.pub
You will see a long string of text starting with ssh-rsa and ending with your email. Copy this entire string completely.
Step 3: Add it to GitHub/GitLab
- Log in to your GitHub (or GitLab) account.
- Go to Settings.
- Find the SSH and GPG keys menu.
- Click New SSH key.
- Title: Fill in anything, for example, “My Windows PC” or “Linux VM”.
- Key: Paste the public key content you copied in the previous step.
- Click Add SSH key.
4. Core Git Operations
A. git clone (Clone/Download a Repository)
This is your starting point for joining a project. You download a complete copy of a project from a remote repository (like GitHub) to your local machine.
- Go to GitHub and find the project you want to clone.
- Click the green “Code” button.
- Important: Select the “SSH” tab (not “HTTPS”).
- Copy the address that starts with
git@github.com:....
Then, in your terminal (Git Bash or Linux terminal), run:
# Example address, please replace it with your own
git clone git@github.com:username/project-name.git
# After it finishes, enter the project directory
cd project-name
B. git branch (Branch Management)
Branching is the core magic of Git. You can safely develop new features or fix bugs without affecting the “main line” (usually called main or master).
# 1. See all your current branches (and which one you are on)
git branch
# 2. Create a new branch named "feature-login"
git branch feature-login
# 3. Switch to that new branch (all your changes from now on will be on this branch)
git checkout feature-login
⭐ Common Shortcut (Create and switch immediately):
# This one command = Step 2 + Step 3 above git checkout -b new-feature-name
C. git push (Pushing Code)
When you have finished your changes locally, you need to “push” these changes back to the remote repository (GitHub) so others can see them.
The complete push workflow (A three-step process):
Let’s assume you have modified files on your feature-login branch.
# 1. Check the status of your files (see what you changed)
git status
# 2. "Stage" your modified files (telling Git you want to commit these)
# ( . means "all modified files")
git add .
# 3. "Commit" your changes and write a "commit message" (explaining what you changed)
git commit -m "Developed the login feature"
The final step: Push!
# Push your local "feature-login" branch to the remote repository "origin"
git push origin feature-login
⭐ Tip: If this is the first time you are pushing this new branch, Git might prompt you to use a more complete command:
git push --set-upstream origin feature-login(Just copy and paste what it suggests. This “links” your local branch to the remote branch).
Summary: Windows vs. Linux Differences
As you can see, the core Git commands are exactly the same on Windows (using Git Bash) and Linux.
The only real differences are:
- Installation Method: Windows uses an
.exeinstaller, while Linux uses a package manager (apt/dnf). - Terminal Environment: Linux uses the terminal natively. On Windows, it is highly recommended to use Git Bash for a consistent experience.
- Line Endings (CRLF vs. LF): This is a hidden pitfall. Windows defaults to
CRLFfor line breaks, while Linux/macOS useLF. Git can automatically configure this during installation (core.autocrlf), so you usually don’t need to handle it manually.
This basic tutorial should be enough to help you get started.
