# 🧠 Essential Git Commands Cheat Sheet

Whether you're new to Git or want to keep your workflow sharp, here’s a handy list of commands that cover the most common — and a few life-saving — actions.

## Clone the repo

```powershell
git clone <repository-url>
```

## 🔍 See What's Going On

```powershell
git status
```

Shows what's changed, what's staged, and what's untracked.

```powershell
git log --oneline
```

View commit history (detailed or summarized).

## ➕ Stage & Commit Changes

```bash
git add <file>     # Stage a single file
git add .          # Stage everything
```

```bash
git commit -m "Your message here" Creates a commit with your staged changes.
```

> 💡 **Git Tip:** Think of Git as a three-step process:  
> 1️⃣ *Working Directory* – where you make edits  
> 2️⃣ *Staging Area* – where you select what to commit  
> 3️⃣ *Repository* – where your project history lives
> 
> Staging gives you control over what gets committed, so you can group related changes together before saving them permanently.

## 🔄 Stash Changes

```powershell
git stash -m "WIP: login bug"   # Save changes with a label
git stash list                  # View stashes
git stash apply [stash@{id}]    # Reapply a specific stash
```

> 💡 **Git Tip:** Need to switch tasks but don’t want to commit your work yet?  
> Use `git stash` to save your changes temporarily and return to a clean working state.  
> Later, use `git stash apply` to get your work back.
> 
> 🛠️ Pro Tip: Use `git stash list` to see all stashes, and `git stash drop` to delete one you no longer need

## 🌿 Branching

```powershell
git branch                    # List branches
git branch feature-xyz        # Create a new branch
git checkout feature-xyz      # Switch to it
git checkout -b feature-xyz   # Create and switch to a new branch in one line
git switch feature-xyz        # Newer switch syntax
```

---

## ⬇️⬆️ Sync with Remote

```powershell
git pull          # Pull latest changes
git push          # Push local commits

# Sets the upstream tracking branch when pushing for the first time
git push -u origin [branch-name]

# Force-push your local branch — use with caution! Ideal after a rebase:
git push --force
```

## 🔧 Undoing Mistakes

```powershell
git restore <file>            # Discard local changes
git restore --staged <file>   # Unstage a file
```

---

## 🧨 Life-Saving Commands

Pull the latest from main and rebase your current branch in one single step:

```powershell
git fetch && git rebase origin/main
```

> 💡 **Git Tip:** Keep your branch up-to-date and avoid merge commits by rebasing onto the latest `main`. Perfect for clean history before a PR.

Remove commits but keep the changes:

> 💡 Need to rewrite history? `--soft` lets you uncommit changes but keeps them staged so you can tweak and recommit.

```powershell
git reset [commit-hash] --soft
```

Pull a file from another branch to modify it locally:

> 💡 Want just one file from another branch? This command pulls it into your current branch without switching context.

```powershell
git checkout other-branch -- path/to/file
```
