A practical Git reference organized by when and why you use each command.
1. Git Basics — Understand the Workflow
Git has three important areas:
Working Directory
↓
git add
↓
Staging Area
↓
git commit
↓
Local Repository
↓
git push
↓
Remote Repository (GitHub/GitLab/etc.)The basic workflow is:
git status
git add .
git commit -m "feat: add authentication"
git push2. First-Time Git Configuration
Set your identity:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"Check configuration:
git config --listCheck a specific setting:
git config user.name
git config user.emailSet the default branch name:
git config --global init.defaultBranch mainSet the default editor:
VS Code
git config --global core.editor "code --wait"Notepad
git config --global core.editor "notepad"Vim
git config --global core.editor "vim"Check your configured editor:
git config --global core.editor3. Create a Repository
Initialize Git inside an existing project:
git initCheck the repository:
git statusClone an existing repository:
git clone <repository-url>Clone into a specific directory:
git clone <repository-url> my-project4. Daily Essentials
These are the commands you will use constantly.
Check status
git statusShows:
modified files
staged files
untracked files
current branch
Stage a specific file
git add filenameExample:
git add app/Models/User.phpStage multiple files:
git add file1.php file2.phpStage everything:
git add .Commit changes
git commit -m "feat: add user authentication"Good commit messages describe what changed.
Examples:
feat: add user authentication
fix: correct navbar alignment
refactor: simplify order service
docs: update installation guide
test: add checkout tests
chore: update dependenciesStage tracked files and commit
git commit -am "fix: update validation rules"Important:
git commit -am does NOT include new untracked files.
For new files:
git add .
git commit -m "feat: add checkout page"5. View Changes
View unstaged changes:
git diffView changes for a specific file:
git diff filename
View staged changes:
git diff --stagedCompare two commits:
git diff <commit1> <commit2>6. Viewing Commit History
Basic history:
git logCompact history:
git log --onelineRecommended visual history:
git log --oneline --graph --decorate --allShow the last 5 commits:
git log -5Show a specific commit:
git show <commit-hash>Example:
git show a82f91c7. Branches
Branches allow you to work on features without directly modifying main.
List local branches:
git branchList all branches:
git branch -aCreate a branch:
git branch feature/authenticationCreate and switch:
git switch -c feature/authenticationOlder alternative:
git checkout -b feature/authenticationSwitch branches:
git switch mainor:
git switch feature/authentication8. Naming Branches
Use descriptive names.
Examples:
feature/user-authentication
feature/payment-integration
feature/order-management
fix/navbar-alignment
fix/login-validation
refactor/order-service
hotfix/payment-errorA common workflow:
git switch main
git pull
git switch -c feature/paymentThen work:
git add .
git commit -m "feat: add payment integration"
git push -u origin feature/payment9. Merge
Suppose you are on:
mainand want to merge:
feature/paymentRun:
git switch main
git pull
git merge feature/paymentThen:
git pushDelete the local branch after merging:
git branch -d feature/paymentForce delete:
git branch -D feature/paymentBe careful with -D because it can delete an unmerged branch.
10. Remote Repositories
View remote repositories:
git remote -vAdd a remote:
git remote add origin <repository-url>Change a remote URL:
git remote set-url origin <repository-url>Remove a remote:
git remote remove origin11. Push
First push a new branch:
git push -u origin feature/authenticationAfter that:
git pushPush current branch:
git push -u origin HEADThe -u establishes the upstream relationship, so future pushes can simply use:
git push12. Pull vs Fetch
This is important.
git fetch
Downloads remote changes but does not merge them:
git fetchThink:
"Show me what changed remotely."
git pull
Usually means:
git fetch
git mergeSo:
git pulldownloads and integrates remote changes.
Safer inspection workflow
Before merging remote changes:
git fetch
git log --oneline --graph --allThen decide what you want to merge/rebase.
13. Stashing
Stash is useful when you have unfinished work but need to switch branches.
Save changes:
git stashBetter, descriptive version:
git stash push -m "WIP: checkout page"List stashes:
git stash listApply the latest stash:
git stash applyApply and remove it from stash:
git stash popApply a specific stash:
git stash apply stash@{2}Delete a stash:
git stash drop stash@{0}Delete all stashes:
git stash clear14. Stashing Untracked Files
Normally:
git stashdoes not stash untracked files.
To include them:
git stash push -u -m "WIP: new checkout files"Include ignored files too:
git stash push -a -m "WIP"Use -a carefully.
15. Undoing Changes — Very Important
There are several different kinds of "undo."
Undo unstaged changes
git restore filenameExample:
git restore resources/js/app.tsxThis discards your uncommitted changes to that file.
Unstage a file
git restore --staged filenameYour changes remain in the working directory.
Unstage everything
git restore --staged .16. Undo a Commit Safely
If the commit has already been pushed/shared:
git revert <commit-hash>Git creates a new commit that reverses the previous commit.
This is generally the safest option for shared branches.
17. Reset
Reset changes the current branch history.
Soft reset
git reset --soft HEAD~1Removes the latest commit but keeps changes staged.
Mixed reset
git reset HEAD~1Removes the latest commit and unstages the changes, but keeps the files modified.
Hard reset
git reset --hard HEAD~1Removes the commit and changes.
Dangerous.
Do not use this casually.
18. Amend the Last Commit
Forgot to add a file?
git add forgotten-file.php
git commit --amend --no-editChange the previous commit message:
git commit --amend -m "feat: add authentication"Avoid amending commits that other people have already based work on.
19. Reflog — Git's Safety Net
One of the most important advanced commands:
git reflogIt shows where HEAD has been.
For example, if you accidentally run:
git reset --hard HEAD~3and lose commits, check:
git reflogYou may find the previous commit:
HEAD@{1}
HEAD@{2}Then recover it:
git reset --hard <commit-hash>Remember:
reflogcan often rescue you from your own Git mistakes.
20. Cherry-Pick
Suppose another branch contains one useful commit:
abc1234You can apply only that commit to your current branch:
git cherry-pick abc1234Useful when:
you need one bug fix
you don't want to merge an entire branch
a hotfix exists on another branch
21. Rebase
Rebase moves your commits on top of another branch.
Example:
git switch feature/payment
git fetch origin
git rebase origin/mainConceptually:
Before:
A---B---C main
\
D---E featureAfter rebase:
A---B---C---D'---E' featureRebase creates new commit IDs.
Use it carefully on branches shared with other developers.
22. Interactive Rebase
Clean up your local commits:
git rebase -i HEAD~3You might see:
pick abc123 Add login
pick def456 Fix login
pick ghi789 Update loginYou can change them to:
pick abc123 Add login
squash def456 Fix login
squash ghi789 Update loginThis combines commits.
Useful before creating a Pull Request.
23. Merge Conflicts
Sometimes Git cannot automatically merge changes.
You may see:
<<<<<<< HEAD
your code
=======
other branch code
>>>>>>> feature/paymentYou must manually decide what the final code should be.
After fixing:
git add .For a merge:
git commitFor a rebase:
git rebase --continueAbort a merge:
git merge --abortAbort a rebase:
git rebase --abort24. .gitignore
.gitignore tells Git which files should not be tracked.
Typical Laravel example:
/vendor/
/node_modules/
.env
.env.*
!.env.example
/public/build/
/storage/*.key
.phpunit.result.cacheTypical Node/React example:
node_modules/
.env
.env.local
dist/
.next/
coverage/Check whether Git is ignoring a file:
git check-ignore -v filename25. Important Files You Should NEVER Accidentally Commit
Be careful with:
.env
.env.local
API keys
private keys
passwords
node_modules/
vendor/
large generated filesFor example, Laravel normally keeps:
.envout of Git and provides:
.env.examplefor other developers.
26. Tags
Tags are useful for releases.
Create a tag:
git tag v1.0.0List tags:
git tagCreate an annotated tag:
git tag -a v1.0.0 -m "Release version 1.0.0"Push a tag:
git push origin v1.0.0Push all tags:
git push --tagsDelete local tag:
git tag -d v1.0.027. Find Information About a Commit
Show commit details:
git show <commit>Find commits containing a word:
git log --grep="authentication"Search through all commits:
git log --all --oneline --grep="payment"Find who changed a line:
git blame filenameExample:
git blame app/Models/User.php28. Search the Codebase
Search tracked files for text:
git grep "calculateDelivery"Search a specific branch:
git grep "calculateDelivery" origin/mainThis can be very useful in large projects.
29. Compare Branches
See commits that exist on one branch but not another:
git log main..feature/payment --onelineSee changes between branches:
git diff main..feature/paymentSee commits in either branch but not both:
git log --oneline --left-right main...feature/payment30. Clean Untracked Files
See what would be deleted:
git clean -nDelete untracked files:
git clean -fDelete untracked files and directories:
git clean -fdAlways use:
git clean -nfirst.
31. Git Aliases
Aliases create shortcuts.
Status:
git config --global alias.st statusCheckout/switch:
git config --global alias.co switchVisual log:
git config --global alias.lg "log --oneline --graph --decorate --all"Useful aliases:
git config --global alias.br branch
git config --global alias.cm "commit -m"
git config --global alias.unstage "restore --staged"
git config --global alias.last "log -1 HEAD"Then:
git st
git br
git lg
git last32. Recommended Git Aliases
A practical set:
git config --global alias.st status
git config --global alias.br branch
git config --global alias.co switch
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.last "log -1 HEAD"
git config --global alias.unstage "restore --staged"
You can also create:
git config --global alias.visual "log --oneline --graph --decorate --all"
Then:
git visual
33. A Professional Feature Workflow
For a typical Laravel/React project:
Step 1 — Start from updated main
git switch main
git pull
Step 2 — Create feature branch
git switch -c feature/user-profile
Step 3 — Work
Edit code
Test code
Fix bugs
Step 4 — Check changes
git status
git diff
Step 5 — Stage
git add .
Step 6 — Commit
git commit -m "feat: add user profile"
Step 7 — Push
git push -u origin feature/user-profile
Step 8 — Create Pull Request
Create the PR on GitHub/GitLab.
Step 9 — After merging
git switch main
git pull
Step 10 — Delete local feature branch
git branch -d feature/user-profile
34. Typical Bug-Fix Workflow
git switch main
git pull
git switch -c fix/login-validation
Make the fix:
git status
git diff
Commit:
git add .
git commit -m "fix: correct login validation"
Push:
git push -u origin fix/login-validation
35. Emergency: "I Made a Mess"
First:
git status
Then inspect:
git diff
Check history:
git log --oneline --graph --decorate --all
If you accidentally lost commits:
git reflog
If you need to discard changes to one file:
git restore filename
If you need to undo a pushed commit:
git revert <commit>
Don't immediately reach for:
git reset --hard
36. Commands You Should Master First
Don't try to memorize everything.
Start with these:
git status
git add .
git commit -m "message"
git push
git pull
git branch
git switch -c <branch>
git switch <branch>
git merge <branch>
git diff
git log --oneline --graph --all
git stash
git stash pop
git restore <file>
git restore --staged <file>
git revert <commit>
git reflog
37. Commands to Learn After the Basics
Once the above feels natural, learn:
git fetch
git cherry-pick
git rebase
git rebase -i
git bisect
git reflog
git worktree
git tag
git clean
git grep
38. git bisect — Find Which Commit Introduced a Bug
This is a powerful debugging tool.
Start:
git bisect start
Mark current version as bad:
git bisect bad
Mark a known working commit as good:
git bisect good <commit>
Git checks out a commit in the middle.
Test it.
If the bug exists:
git bisect bad
If the bug does not exist:
git bisect good
Git keeps narrowing the range until it identifies the commit that introduced the problem.
Finish:
git bisect reset
39. git worktree — Work on Multiple Branches
Useful when you need two branches checked out simultaneously.
Example:
git worktree add ../project-hotfix hotfix/payment
Now you can have:
project/
project-hotfix/
with different branches checked out.
This is especially useful for developers working on large projects.
40. Git Mental Model
Try to understand Git instead of memorizing commands.
Think:
git add
Working ─────────────────→ Staging
│
│ git commit
↓
Local History
│
│ git push
↓
Remote Repository
And:
Remote
│
│ git fetch
↓
Remote-tracking branch
│
│ merge/rebase
↓
Your local branch
This mental model makes Git much easier.
41. The Most Important Differences
git restore
Undo file changes.
git restore file
git restore --staged
Remove a file from staging.
git restore --staged file
git revert
Safely undo a commit by creating another commit.
git revert <commit>
git reset
Move the branch pointer backward.
git reset --soft HEAD~1
git rebase
Rewrite/replay commits onto another base.
git rebase main
git merge
Combine histories.
git merge feature
42. Golden Rules
Rule 1
Before doing anything complicated:
git status
Rule 2
Before destructive commands:
git log --oneline --graph --all
Rule 3
If you think you lost something:
git reflog
Rule 4
For shared/public commits, prefer:
git revert
over rewriting history.
Rule 5
Be extremely careful with:
git reset --hard
git clean -fd
git push --force
Rule 6
Never commit secrets.
43. Quick Daily Cheat Sheet
# See what changed
git status
# See actual changes
git diff
# Stage everything
git add .
# Commit
git commit -m "feat: description"
# Get remote changes
git pull
# Push
git push
# Create feature branch
git switch -c feature/my-feature
# Switch branch
git switch main
# See branches
git branch
# Save unfinished work
git stash push -m "WIP"
# Restore unfinished work
git stash pop
# See history
git log --oneline --graph --decorate --all
# Unstage
git restore --staged <file>
# Discard local file changes
git restore <file>
# Safely undo a commit
git revert <commit>
# Recover lost commits
git reflog
44. Recommended Learning Order
Don't try to learn 50 commands at once.
Level 1 — Must Know
git init
git clone
git status
git add
git commit
git push
git pull
git branch
git switch
git merge
Level 2 — Daily Professional Git
git diff
git log
git show
git fetch
git stash
git restore
git revert
git remote
Level 3 — Advanced
git rebase
git rebase -i
git cherry-pick
git reflog
git tag
git bisect
git worktree
Level 4 — Professional Git Knowledge
Understand:
HEAD
HEAD~1
HEAD^
branches
remote-tracking branches
upstream branches
merge commits
fast-forward merges
three-way merges
rebase
detached HEAD
conflicts
force push
Once you understand these concepts, Git becomes much easier than memorizing commands.
45. One Workflow to Remember
For most feature work:
git switch main
git pull
git switch -c feature/my-feature
# write code...
git status
git diff
git add .
git commit -m "feat: add my feature"
git push -u origin feature/my-feature
Then create your Pull Request.
After it is merged:
git switch main
git pull
git branch -d feature/my-feature
That workflow alone covers a huge percentage of real-world Git usage.