Git Basics
Git Basics
Setting Up Your Repository
The first steps involve telling Git that you want to start tracking a project. A repository (often shortened to "repo") is the .git folder that holds all your project's history and metadata.
| Command | Action | Explanation |
|---|---|---|
git init | Initialize | Runs once inside your project folder to create the hidden .git subdirectory. This transforms the folder into a local Git repository, ready to be tracked. |
git status | Check State | A command you'll use constantly. It shows the current state of your Working Directory and Staging Area, listing files that are untracked, modified, or staged for the next commit. |
git config | Configure | Sets your user identity for the commits you make. This is required before your first commit. |
git config --global user.name "Your Name" | ||
git config --global user.email "you@example.com" |
The Core Workflow: Track and Commit
Git's local version control loop involves three steps: Modify, Stage, and Commit. This process creates the historical snapshots that form the backbone of your project's timeline.
Step 1: Modifying Files (Working Directory)
You simply edit, create, or delete files in your project folder. git status will show these files as modified or untracked.
Step 2: Staging Changes (Staging Area / Index)
The Staging Area is a crucial intermediate step. It allows you to precisely curate which changes will be included in the very next snapshot.
| Command | Action | Explanation |
|---|---|---|
git add <file> | Stage Specific File | Adds the changes from a specific file into the Staging Area. |
git add . | Stage All Changes | A common shortcut that stages all modified and untracked files in the current directory and its subdirectories. |
Step 3: Committing (Git Directory)
A commit is the actual act of taking the snapshot. Everything currently in the Staging Area is bundled together, saved to the .git directory, and given a unique ID (a SHA-1 hash).
| Command | Action | Explanation |
|---|---|---|
git commit -m "message" | Snapshot | Permanently records the staged changes. The -m flag is essential for providing a descriptive message that explains why the changes were made, helping you and others understand the history later. |
git commit -am "message" | Quick Commit | A shortcut that combines staging and committing, but only for files that Git is already tracking. It bypasses the explicit staging of modified files. |
Connecting to the Remote World (GitHub/GitLab/BitBucket/...)
To back up your work and collaborate, you need to connect your local repository to a remote repository—typically hosted on a service like GitHub or GitLab.
| Command | Action | Explanation |
|---|---|---|
git remote add origin <URL> | Link Remote | Tells your local repo the URL of the external (remote) server where the code will be hosted. origin is the standard nickname for this primary remote. |
git push | Send Changes | Transfers your committed changes from your local repository to the remote repository. This command is how you back up your work and make it available to collaborators. |
git push -u origin main | Send changes | First Push 🚀. The first time you push, you use the -u (or --set-upstream) flag to tell Git to permanently link your local main branch to the remote origin/main branch. All subsequent pushes can just be git push. |
git pull | Get Changes | Downloads and integrates changes from the remote repository into your local repository. This is how you update your local copy with work done by others (or by yourself on another machine). |
Reviewing and Undoing
Being a time machine, Git gives you tools to inspect your history and easily undo mistakes.
| Command | Action | Explanation |
|---|---|---|
git log | View History | Shows a chronological list of all commits, including the author, date, and commit message. This lets you see the project timeline. |
git show <commit-id> | Inspect Snapshot | Displays the full details of a specific commit, including exactly what was added and removed in that snapshot. You can use the first few characters of the commit's SHA ID. |
git diff | Compare Changes | Shows differences between the Working Directory and the Staging Area (what you've changed but haven't staged). |
git diff --staged | Compare Changes | Shows differences between the Staging Area and the last commit (what you are about to commit). |
git restore --staged <file> | Unstage | Moves a file out of the Staging Area back to the Working Directory. Useful if you staged a file by mistake. |
git checkout -- <file> | Discard Changes | DANGEROUS ⚠️. Discards all local, unstaged changes to a file, reverting it to the state it was in at the last commit. This effectively deletes your work since the last commit. |