🧱 1. Repository Setup
Purpose: Start tracking a project with Git.
Command | Description | Example |
---|---|---|
git init | Initialize a new Git repository in your current folder. | git init |
git clone <repo_url> | Copy an existing remote repository to your local machine. | git clone https://github.com/user/project.git |
Tip:
After cloning, you already have a .git
folder — no need to run git init
again.
🧩 2. Checking Status and Changes
Purpose: See what’s happening in your working directory.
Command | Description | Example |
---|---|---|
git status | Shows untracked, modified, and staged files. | git status |
git diff | View unstaged changes (differences between working directory and index). | git diff |
git diff --staged | View differences between staged files and last commit. | git diff --staged |
📦 3. Staging and Committing
Purpose: Record file changes into history.
Command | Description | Example |
---|---|---|
git add <file> | Stage a file for commit. | git add index.html |
git add . | Stage all changes in the current directory. | git add . |
git commit -m "message" | Save staged changes with a descriptive message. | git commit -m "Add homepage layout" |
git commit -am "message" | Add and commit all tracked files in one step. | git commit -am "Fix typo" |
🧭 4. Viewing History
Purpose: Review what happened over time.
Command | Description | Example |
---|---|---|
git log | Show full commit history. | git log |
git log --oneline | Show compact commit history. | git log --oneline |
git show <commit_id> | View details of a specific commit. | git show a1b2c3d |
Tip:
Press q
to exit the log viewer.
🔁 5. Branching and Merging (Basics)
Purpose: Work on features independently.
Command | Description | Example |
---|---|---|
git branch | List all branches. | git branch |
git branch <name> | Create a new branch. | git branch feature/login |
git switch <name> | Switch to another branch. | git switch feature/login |
git merge <branch> | Merge another branch into the current one. | git merge feature/login |
🌍 6. Working with Remotes
Purpose: Connect your local repo to a remote (e.g., GitHub).
Command | Description | Example |
---|---|---|
git remote add origin <url> | Link your local repo to a remote. | git remote add origin https://github.com/user/project.git |
git push -u origin main | Push commits to the remote repository. | git push -u origin main |
git pull | Fetch and merge changes from the remote. | git pull |
git fetch | Download changes without merging. | git fetch |
🔄 7. Undoing Changes
Purpose: Fix mistakes safely.
Command | Description | Example |
---|---|---|
git restore <file> | Undo unstaged changes in a file. | git restore index.html |
git restore --staged <file> | Unstage a file (keep changes). | git restore --staged index.html |
git reset --hard <commit> | Reset everything to a previous commit (⚠ destructive). | git reset --hard HEAD~1 |
git revert <commit> | Create a new commit that undoes the changes from another commit. | git revert a1b2c3d |
🧰 8. Ignoring Files
Purpose: Tell Git which files/folders to skip.
File: .gitignore
# Example .gitignore
node_modules/
.env
*.log
🕵️ 9. Viewing Information
Command | Description | Example |
---|---|---|
git status | See which files changed. | git status |
git diff | Compare working changes. | git diff |
git remote -v | View remote connections. | git remote -v |
git branch -vv | View branches and their tracking info. | git branch -vv |
🧠 10. Common Git Command Workflow
Here’s the everyday Git cycle:
# 1. See what’s changed
git status
# 2. Stage your changes
git add .
# 3. Commit them
git commit -m "Add feature X"
# 4. Push to remote
git push
# 5. Update from others
git pull