Git Fundamentals, Object Model (Blobs, Trees, Commits), and Staging
⚡ Quick Dive
Essential Git Command Cheat Sheet
# Initialize & inspect
git init
git status -s
git log --oneline --graph --all
# Staging & commits
git add -A
git commit -m "feat: implement user authentication"
# Remote synchronization
git remote -v
git push -u origin main
git pull --rebase origin main
📖 Extended Guide
1. The Git Object Model & .git Anatomy
Git is a content-addressable key-value store under .git/objects/:
- Blob: Stores pure file data without filenames or permissions.
- Tree: Stores directory hierarchies, file names, and pointers to blobs or sub-trees.
- Commit: Points to a root tree, parent commit hashes, author metadata, and message.
- Tag: Permanent cryptographic pointer to a specific commit.
[ Commit: a1b2c3d ] ──► [ Tree: root ] ──┬──► [ Blob: main.go ]
│ └──► [ Tree: internal ] ──► [ Blob: auth.go ]
▼
[ Parent Commit: 9z8y7x ]
1. Setting Up Git
- Before you start using Git, configure your identity:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
- Check your configuration:
git config --list
2. Working with a Repository
- Clone a Repository
git clone <repository-url>
- Check Repository Status
git status
- Add Files to Staging
git add <file-name>
# Or add all changes
git add .
- Commit Changes
git commit -m "Your commit message"
3. Branching and Merging
- Create a New Branch
git branch <branch-name>
- Switch to a Branch
git checkout <branch-name>
- Create and Switch to a Branch
git checkout -b <branch-name>
- Merge Branches
git merge <branch-name>
4. Syncing with Remote
- Push Changes
git push origin <branch-name>
- Pull Changes
git pull origin <branch-name>
- Set Remote Repository
git remote add origin <repository-url>
5. Undoing Changes
- Discard Unstaged Changes
git checkout -- <file-name>
- Unstage Changes
git reset <file-name>
- Undo the Last Commit (Keep Changes)
git reset --soft HEAD~1
- Undo the Last Commit (Discard Changes)
git reset --hard HEAD~1
6. Viewing Logs and History
- Show Commit History
git log
- Show Changes in Commit History
git log -p
- Show Graphical History
git log --oneline --graph --decorate --all
Git Cheatsheet
| Command | Description |
|---|---|
git config --global |
Configure global username/email |
git init |
Initialize a repository |
git clone <url> |
Clone a repository |
git status |
Show the working directory status |
git add <file> / git add . |
Add file(s) to the staging area |
git commit -m "<message>" |
Commit staged changes |
git branch |
List branches |
git branch <branch-name> |
Create a new branch |
git checkout <branch-name> |
Switch to a branch |
git checkout -b <branch-name> |
Create and switch to a branch |
git merge <branch-name> |
Merge a branch into the current branch |
git pull origin <branch-name> |
Pull updates from a remote branch |
git push origin <branch-name> |
Push changes to a remote branch |
git reset <file> |
Unstage a file |
git reset --soft HEAD~1 |
Undo last commit but keep changes |
git reset --hard HEAD~1 |
Undo last commit and discard changes |
git log |
Show commit history |
git log --oneline --graph --all |
Show graphical commit history |
For more advanced Git commands, consult the Git documentation.