Ref: Git Essential Tranining - LinkedIn Learning
Here's a detailed guide to essential Git commands, organized by category, with explanations and examples to help you understand how and when to use them:
🛠️ Setup & Configuration
Command |
Description |
Example |
git config --global user.name "Your Name" |
Sets your Git username (global) |
Identify yourself to Git |
git config --global user.email "you@example.com" |
Sets your Git email (global) |
Used in commit metadata |
git config --list |
Shows current Git config |
Useful for verifying settings |
📁 Repository Initialization & Cloning
Command |
Description |
Example |
git init |
Initializes a new Git repo in current directory |
git init |
git clone <repo-url> |
Clones an existing repo from remote |
git clone https://github.com/user/repo.git |
📄 Staging & Committing Changes
Command |
Description |
Example |
git status |
Shows current changes (staged, unstaged, untracked) |
git status |
git add <file> |
Stages a file for commit |
git add index.html |
git add . |
Stages all changes in current directory |
git add . |
git commit -m "message" |
Commits staged changes with a message |
git commit -m "Fix login bug" |
🔄 Branching & Merging
Command |
Description |
Example |
git branch |
Lists all branches |
git branch |
git branch <name> |
Creates a new branch |
git branch feature-x |
git checkout <name> |
Switches to a branch |
git checkout main |
git merge <name> |
Merges another branch into current |
git merge feature-x |
🌐 Remote Repositories
Command |
Description |
Example |
git remote -v |
Lists remote URLs |
git remote -v |
git push origin <branch> |
Pushes local branch to remote |
git push origin main |
git pull |
Fetches and merges remote changes |
git pull |
git fetch |
Fetches remote changes (no merge) |
git fetch origin |
🧹 Undoing Changes
Command |
Description |
Example |
git reset <file> |
Unstages a file |
git reset index.html |
git checkout -- <file> |
Discards local changes |
git checkout -- style.css |
git revert <commit> |
Reverts a specific commit |
git revert abc1234 |
🕵️ History & Inspection
Command |
Description |
Example |
git log |
Shows commit history |
git log --oneline |
git diff |
Shows changes between commits or working tree |
git diff HEAD |
git show <commit> |
Shows details of a specific commit |
git show abc1234 |