# Mastering Advanced Git Commands: From Stashing to Rebasing

## 📦 What is `git stash` & `git stash pop`

### ✅ Why Do We Use It?

`git stash` is used to **temporarily save changes** that you are not ready to commit. It’s like putting your current changes in a drawer, so you can come back to them later.

`git stash pop` restores those saved changes.

### 🧪 Scenario:

You’re in the middle of fixing a bug, but suddenly you need to switch branches to work on a hotfix. You don’t want to commit incomplete code. Here's what you do:

```bash
git stash        # Saves current changes
git checkout hotfix-branch
```

After finishing the hotfix:

```bash
git checkout original-branch
git stash pop    # Restores the changes
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1752197193469/dd3bf045-0dbb-4e88-baf7-14cc59b8af4e.png align="center")

📚 Working with Multiple Stashes

You can have more than one stash. To manage them:

```bash
git stash list        # Shows all stashes
git stash show stash@{1}   # View a specific stash
git stash apply stash@{1} # Apply a specific stash without deleting
```

---

## 🍒 What is `git cherry-pick`

### ✅ Why Do We Use It?

`git cherry-pick` allows you to **apply a specific commit** from one branch to another, without merging everything.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1752197214493/7ebb56cf-a918-4e34-8a8f-3f54d1471029.png align="center")

### 🧪 Scenario:

Let’s say your teammate fixed a bug in `feature-a`, and you want the same fix in `main`:

```bash
git checkout main
git cherry-pick <commit-hash>
```

This will bring just that commit into your current branch.

---

## 🔖 What is `git tag`

### ✅ Why Do We Use It?

Tags are used to mark **specific points in history**—usually for version releases.

### 🧪 Scenario:

You’ve released version 1.0.0 of your app:

```bash
git tag v1.0.0
git push origin v1.0.0
```

This creates a tag so others can check out that exact version.

---

## 🧼 What is Git Squash

### ✅ Why Do We Use It?

`git squash` is used to **combine multiple commits into one single commit**. This is especially helpful when you’ve made many small or experimental commits and want to clean them up before merging to `main` or creating a pull request.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1752197241724/a0e2db23-3906-4803-b1c3-1698d709e961.png align="center")

### 🧪 Scenario:

Imagine you created 6 small commits while building a login feature:

* Fixed typo
    
* Added basic login UI
    
* Added validation
    
* Fixed error message
    
* Refactored code
    
* Final adjustments
    

Instead of keeping all 6 in history, you can squash them into one:

```bash
git rebase -i HEAD~6
```

In the interactive window, mark the first commit as `pick` and the rest as `squash` or `s`. This will merge them into a single commit with a unified message.

📌 **Use Case:** Ideal before merging a feature branch to keep history clean and meaningful.

---

## ⏪ What is `git revert`

### ✅ Why Do We Use It?

`git revert` is used to **undo changes made by a previous commit** by creating a new commit that reverses the changes. It’s different from `git reset` because it doesn’t remove commits from history—it just cancels their effect.

### 🧪 Scenario:

Suppose you made a commit that introduced a bug:

```bash
git commit -m "Added buggy feature"
```

To undo this, run:

```bash
git revert <commit-hash>
```

This creates a new commit like:

```bash
git commit -m "Revert 'Added buggy feature'"
```

📌 **Use Case:** Best used in shared/public branches to safely undo mistakes without rewriting history.

---

## 🔁 Git Rebase and Its Types

### ✅ Why Do We Use It?

Rebase is used to **move or reapply commits on top of another commit or branch**. This helps keep a cleaner, linear project history.

### 🔧 Three Types:

1. \`\` – Moves HEAD to a previous commit, but keeps changes staged.
    
2. \`\` *(default)* – Moves HEAD and un-stages the changes.
    
3. \`\` – Resets HEAD and discards all changes completely.
    

> 🔁 Note: These are often used to clean up local history and are dangerous if used on shared branches.

### 🧪 Scenario:

You made 3 messy commits and want to clean the last one:

```bash
git reset --soft HEAD~1   # Keep changes staged
# or
git reset --mixed HEAD~1  # Keep changes in working directory
# or
git reset --hard HEAD~1   # Remove changes completely
```

📌 **Use Case:** Great for undoing local commits or cleaning up code before committing the final version.

---

## 🆚 Revert vs Rebase

| Feature | `git revert` | `git rebase` |
| --- | --- | --- |
| Purpose | Undo a specific commit | Move commits onto another base |
| Safety | Safe for public branches | Risky if rewriting shared history |
| History | Preserves all commits | Rewrites history (can look cleaner) |
| Common Use | Remove a bug without deleting commit | Clean up or reapply commits in correct order |

---

## ❓ How to Choose Between Revert and Rebase

* Choose \`\` when:
    
    * You need to undo something in a shared/public branch
        
    * You want to keep a safe, traceable history
        
* Choose \`\` when:
    
    * You’re working on local or feature branches
        
    * You want a clean, linear history before merging
        

---

### 🙌 Thank You!

Thank you for taking the time to read this blog.  
If you found it helpful, consider sharing it with your peers or bookmarking it for future reference.  
Keep scripting, keep automating — and keep learning! 💻🚀
