Back to Git Topics
Gitbeginner Level 80 min read Afzaal Suleman

Git Essential Commands Developer Cheat Sheet

Here is a curated guide of essential Git commands categorized by how and when you use them, from daily essentials to lifesavers when things go wrong.

Git Study Chapter · Official Reference

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 push

2. 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 --list

Check a specific setting:

git config user.name
git config user.email

Set the default branch name:

git config --global init.defaultBranch main

Set 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.editor

3. Create a Repository

Initialize Git inside an existing project:

git init

Check the repository:

git status

Clone an existing repository:

git clone <repository-url>

Clone into a specific directory:

git clone <repository-url> my-project

4. Daily Essentials

These are the commands you will use constantly.

Check status

git status

Shows:

  • modified files

  • staged files

  • untracked files

  • current branch


Stage a specific file

git add filename

Example:

git add app/Models/User.php

Stage multiple files:

git add file1.php file2.php

Stage 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 dependencies

Stage 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 diff

View changes for a specific file:

git diff filename

View staged changes:

git diff --staged

Compare two commits:

git diff <commit1> <commit2>

6. Viewing Commit History

Basic history:

git log

Compact history:

git log --oneline

Recommended visual history:

git log --oneline --graph --decorate --all

Show the last 5 commits:

git log -5

Show a specific commit:

git show <commit-hash>

Example:

git show a82f91c

7. Branches

Branches allow you to work on features without directly modifying main.

List local branches:

git branch

List all branches:

git branch -a

Create a branch:

git branch feature/authentication

Create and switch:

git switch -c feature/authentication

Older alternative:

git checkout -b feature/authentication

Switch branches:

git switch main

or:

git switch feature/authentication

8. 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-error

A common workflow:

git switch main
git pull

git switch -c feature/payment

Then work:

git add .
git commit -m "feat: add payment integration"
git push -u origin feature/payment

9. Merge

Suppose you are on:

main

and want to merge:

feature/payment

Run:

git switch main
git pull
git merge feature/payment

Then:

git push

Delete the local branch after merging:

git branch -d feature/payment

Force delete:

git branch -D feature/payment

Be careful with -D because it can delete an unmerged branch.


10. Remote Repositories

View remote repositories:

git remote -v

Add 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 origin

11. Push

First push a new branch:

git push -u origin feature/authentication

After that:

git push

Push current branch:

git push -u origin HEAD

The -u establishes the upstream relationship, so future pushes can simply use:

git push

12. Pull vs Fetch

This is important.

git fetch

Downloads remote changes but does not merge them:

git fetch

Think:

"Show me what changed remotely."


git pull

Usually means:

git fetch
git merge

So:

git pull

downloads and integrates remote changes.


Safer inspection workflow

Before merging remote changes:

git fetch
git log --oneline --graph --all

Then 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 stash

Better, descriptive version:

git stash push -m "WIP: checkout page"

List stashes:

git stash list

Apply the latest stash:

git stash apply

Apply and remove it from stash:

git stash pop

Apply a specific stash:

git stash apply stash@{2}

Delete a stash:

git stash drop stash@{0}

Delete all stashes:

git stash clear

14. Stashing Untracked Files

Normally:

git stash

does 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 filename

Example:

git restore resources/js/app.tsx

This discards your uncommitted changes to that file.


Unstage a file

git restore --staged filename

Your 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~1

Removes the latest commit but keeps changes staged.


Mixed reset

git reset HEAD~1

Removes the latest commit and unstages the changes, but keeps the files modified.


Hard reset

git reset --hard HEAD~1

Removes 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-edit

Change 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 reflog

It shows where HEAD has been.

For example, if you accidentally run:

git reset --hard HEAD~3

and lose commits, check:

git reflog

You may find the previous commit:

HEAD@{1}
HEAD@{2}

Then recover it:

git reset --hard <commit-hash>

Remember:

reflog can often rescue you from your own Git mistakes.


20. Cherry-Pick

Suppose another branch contains one useful commit:

abc1234

You can apply only that commit to your current branch:

git cherry-pick abc1234

Useful 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/main

Conceptually:

Before:

A---B---C  main
     \
      D---E  feature

After rebase:

A---B---C---D'---E'  feature

Rebase 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~3

You might see:

pick abc123 Add login
pick def456 Fix login
pick ghi789 Update login

You can change them to:

pick abc123 Add login
squash def456 Fix login
squash ghi789 Update login

This 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/payment

You must manually decide what the final code should be.

After fixing:

git add .

For a merge:

git commit

For a rebase:

git rebase --continue

Abort a merge:

git merge --abort

Abort a rebase:

git rebase --abort

24. .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.cache

Typical Node/React example:

node_modules/
.env
.env.local
dist/
.next/
coverage/

Check whether Git is ignoring a file:

git check-ignore -v filename

25. Important Files You Should NEVER Accidentally Commit

Be careful with:

.env
.env.local
API keys
private keys
passwords
node_modules/
vendor/
large generated files

For example, Laravel normally keeps:

.env

out of Git and provides:

.env.example

for other developers.


26. Tags

Tags are useful for releases.

Create a tag:

git tag v1.0.0

List tags:

git tag

Create an annotated tag:

git tag -a v1.0.0 -m "Release version 1.0.0"

Push a tag:

git push origin v1.0.0

Push all tags:

git push --tags

Delete local tag:

git tag -d v1.0.0

27. 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 filename

Example:

git blame app/Models/User.php

28. Search the Codebase

Search tracked files for text:

git grep "calculateDelivery"

Search a specific branch:

git grep "calculateDelivery" origin/main

This 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 --oneline

See changes between branches:

git diff main..feature/payment

See commits in either branch but not both:

git log --oneline --left-right main...feature/payment

30. Clean Untracked Files

See what would be deleted:

git clean -n

Delete untracked files:

git clean -f

Delete untracked files and directories:

git clean -fd

Always use:

git clean -n

first.


31. Git Aliases

Aliases create shortcuts.

Status:

git config --global alias.st status

Checkout/switch:

git config --global alias.co switch

Visual 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 last

32. 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.