Mastering Git: The Ultimate Guide from Beginner to Advanced

Git
Github
GitCommands
GitGuide
GitForBeginner
GitAdvanced
GitWorkflow
Learn all Git commands from beginner to advanced in this complete 2025 guide. Master version control, GitHub integration, branching, merging, undoing changes, and more with real examples and clear explanations.
๐ง What Is Git (In Simple Terms)?
Git is a version control system that tracks your file changes over time. You can:
- Save versions of your code.
- Collaborate with others.
- Undo mistakes.
- Share work using services like GitHub.
๐ง Git Setup: Initial Configuration (One-Time Only)
1. Install Git (if not already installed):
For Windows:
Download from https://git-scm.com
For macOS:
1brew install git
For Ubuntu/Linux:
1sudo apt update
2sudo apt install git
Check version:
1git --version
๐ค Git Configuration
1git config --global user.name "Your Name"
2git config --global user.email "you@example.com"
3git config --global core.editor "code --wait" # Set VS Code as default editor
View all config:
1git config --list
๐ Repository Basics
Initialize Git
1git init
Clone Repository
1git clone <repo-url>
๐ File Tracking Commands
Check Status
1git status
Add Files to Staging
1git add <filename> # Add a specific file
2git add . # Add all files in current directory
3git add -A # Add all changes (tracked/untracked/deleted)
Commit Changes
1git commit -m "Your message"
Commit with Staged + Unstaged Info
1git commit -am "Commit tracked changes"
๐ Working with Remote Repositories
Add Remote
1git remote add origin <url>
View Remotes
1git remote -v
Push Code
1git push -u origin main # First time push with upstream
2git push # Normal push
Pull Latest Changes
1git pull origin main
๐ฟ Branching & Merging
Create a New Branch
1git branch <branch-name>
Switch Branch
1git checkout <branch-name>
2git switch <branch-name> # Recommended
Create + Switch Branch
1git checkout -b <branch-name>
2git switch -c <branch-name>
Merge Branch
1git checkout main
2git merge <branch-name>
๐ View History
View Commit History
1git log
2git log --oneline
3git log --graph --all --decorate
๐ช Undoing Changes
Unstage a File
1git reset <file>
Undo Last Commit (keep changes)
1git reset --soft HEAD~1
Undo Last Commit (remove staged)
1git reset --mixed HEAD~1
Undo Last Commit (remove everything)
1git reset --hard HEAD~1
๐ซ Ignoring Files
Create .gitignore
1# .gitignore
2node_modules/
3.env
4.DS_Store
5*.log
6
๐ฆ Stashing Work
Temporarily save changes without committing:
1git stash
2git stash list
3git stash apply # Apply latest stash
4git stash pop # Apply and delete
โ๏ธ Deleting & Renaming Files
1git rm <file>
2git mv <old> <new>
๐ Viewing Changes
1git diff # Unstaged changes
2git diff --staged # Staged changes
3git diff HEAD # All changes since last commit
๐ Rebase
1git checkout feature
2git rebase main
- Makes history linear
- Resolve conflicts like merges
๐ Cherry-pick
Apply a specific commit to your current branch:
1git cherry-pick <commit-hash>
๐งน Clean Untracked Files
1git clean -n # Show what will be deleted
2git clean -f # Delete untracked files
๐ต๏ธโโ๏ธ Blame & Annotate
1git blame <file> # Shows who changed each line
๐ GitHub Integration
Generate SSH Key
1ssh-keygen -t ed25519 -C "you@example.com"
Add SSH Key
- Add the public key (~/.ssh/id_ed25519.pub) to GitHub โ Settings โ SSH Keys.
Clone with SSH
1git clone git@github.com:user/repo.git
โ๏ธ Advanced Config & Shortcuts
Aliases
1git config --global alias.st status
2git config --global alias.co checkout
3git config --global alias.br branch
4git config --global alias.cm 'commit -m'
Now use:
1git st
2git co main
3git cm "msg"
๐งฉ Git Submodules
Used to include one repo inside another.
1git submodule add <repo-url> path/
2git submodule update --init --recursive
3
โ Final Tips
- Commit often with clear messages.
- Pull frequently to avoid conflicts.
- Use branches for features or bugs.
- Push only tested and reviewed code.
- Learn rebase vs merge for clean histories.
๐ Git Command Cheat Sheet
git init | Start a repo |
---|---|
git clone | Copy a repo |
git add | Stage changes |
git commit | Save changes |
git push | Upload to remote |
git pull | Download from remote |
git branch | List or create branches |
git merge | Merge branches |
git rebase | Rebase commits |
git reset | Undo changes |
git log | View commit history |
git stash | Save uncommitted work |
git cherry-pick | Apply a specific commit |
git remote | Manage remote repos |
git diff | View file differences |
git tag | Create version tags |