Setup & Configuration

git init                              # Initialize a new repository

git clone <repository-url>            # Clone a remote repository

git config --global user.name "Nav"   # Configure username

git config --global user.email "email@example.com"   # Configure email

git remote -v                         # Display configured remotes

Daily Git Workflow ⭐

git status

git add <file>

git add .

git commit -m "Commit message"

git push origin <branch>

git pull origin <branch>

git log --oneline
git status
      ↓
git add
      ↓
git commit
      ↓
git pull
      ↓
git push

This sequence minimizes merge conflicts and ensures your local branch is synchronized before pushing.


Branching & Merging

git branch

git branch <branch-name>

git checkout <branch-name>

git checkout -b <branch-name>

git switch <branch-name>

git merge <branch-name>

git branch -d <branch-name>

git push origin --delete <branch-name>

Resolving a Merge Conflict

# Git marks conflicting sections

# Edit the affected files

git add <resolved-file>

git commit

Fetch vs Pull ⭐

Command Purpose
git fetch Downloads changes from the remote repository without merging them
git pull Performs both fetch and merge

Tip: Use git fetch when you want to inspect incoming changes before merging them into your branch.


Git Stash

git stash

git stash list

git stash apply

git stash pop

git stash drop

When Should You Use Git Stash?

Use Git Stash when you need to switch branches or handle urgent work without committing incomplete changes.

Advertisement

Undoing Changes ⭐

Task Command
Unstage a File git reset <file>
Discard Local Changes git checkout -- <file>
Modify the Last Commit git commit --amend
Undo Last Commit (Keep Changes) git reset --soft HEAD~1
Undo Last Commit (Discard Changes) git reset --hard HEAD~1 ⚠️
Undo a Commit Safely git revert <commit>

git reset vs git revert

  • git reset rewrites commit history and should be used carefully, especially on shared branches.
  • git revert creates a new commit that reverses a previous commit and is the preferred option for shared repositories.

Rebase & Advanced Commands

git rebase <branch>

git rebase -i HEAD~3

git cherry-pick <commit>

git bisect start

git reflog

Important Notes

  • Merge preserves complete branch history by creating a merge commit.
  • Rebase produces a cleaner, linear commit history.

⚠️ Avoid rebasing shared or public branches because it rewrites commit history.


"Oh No" Recovery Guide ⭐

Problem Recommended Solution
Committed to the Wrong Branch git reset --soft HEAD~1, switch branches, then recommit or use git cherry-pick
Deleted a Branch Use git reflog, locate the commit, then recreate the branch
Push Rejected (Non-Fast-Forward) Execute git pull (or git pull --rebase) before pushing again
Detached HEAD Switch back using git checkout <branch>
Bad Merge Use git reset --hard (local only) or git revert -m 1 <merge-commit>
Lost Track of Commits Use git reflog to view all recent repository movements

Force Push

git push --force

⚠️ Force Push rewrites remote history. Use it only on personal branches. When possible, prefer:

git push --force-with-lease

Pull Requests

Platform Terminology
GitHub Pull Request (PR)
GitLab Merge Request (MR)

Both represent the same collaborative workflow:

Create Branch → Push Changes → Open PR/MR → Review → Approve → Merge

Many teams also squash commits before merging to maintain a clean commit history.


Go Deeper

Continue learning with:

  • Git Basics
  • Git Commands & Usage
  • Git Errors & Troubleshooting
  • Complete Git Guide
  • Top Git Interview Questions