# My Git Cheatsheet

This is my Git cheatsheet. I share it in case it's of any use to anyone else.Stash Commands

## Stashing

### Basic Stashing

```bash
git stash                      # Stash changes quickly
git stash save "Description"   # Stash changes with a description
```

### Viewing Stashes

```bash
git stash list                 # List all stashes
git stash show stash@{n}       # Show changes in a specific stash
git stash show -p stash@{n}    # Show full diff of a specific stash
```

### Applying Stashes

```bash
git stash apply                # Apply the most recent stash
git stash apply stash@{n}      # Apply a specific stash
git stash pop                  # Apply and remove the most recent stash
git stash pop stash@{n}        # Apply and remove a specific stash
```

### Managing Stashes

```bash
git stash drop stash@{n}       # Remove a specific stash
git stash clear                # Remove all stashes
```

### Branching from a Stash

```bash
git stash branch new-branch    # Create a new branch from the latest stash
```

Note: Replace `n` with the stash index number (0 is most recent).

---

## Rebase Commands

### Basic Rebasing

```bash
git rebase <base>              # Rebase current branch onto <base>
git rebase -i <base>           # Interactive rebase
```

### Rebase Operations

```bash
git rebase --continue          # Continue rebase after resolving conflicts
git rebase --abort             # Abort the rebase operation
git rebase --skip              # Skip the current commit and continue
```

### Updating a Branch

```bash
git pull --rebase origin <branch>  # Pull changes and rebase instead of merge
```

### Rebase onto Another Branch

```bash
git rebase --onto <newbase> <oldbase> <branch>
```

### Preserving Merges

```bash
git rebase -p <upstream>       # Rebase preserving merge commits
```

Note: Replace `<base>`, `<branch>`, `<newbase>`, `<oldbase>`, and `<upstream>` with appropriate branch or commit references.

---

## Reset Commands

### Soft Reset

```bash
git reset --soft HEAD~1        # Undo last commit, keep changes staged
git reset --soft <commit>      # Reset to specific commit, keep changes staged
```

### Mixed Reset (Default)

```bash
git reset HEAD~1               # Undo last commit, keep changes unstaged
git reset <commit>             # Reset to specific commit, keep changes unstaged
```

### Hard Reset

```bash
git reset --hard HEAD~1        # Undo last commit, discard all changes
git reset --hard <commit>      # Reset to specific commit, discard all changes
git reset --hard origin/<branch>  # Reset to remote branch, discard all local changes
```

### Reset Single File

```bash
git reset <file>               # Unstage a file
git reset --hard <file>        # Discard changes to a file (use with caution)
```

### Reset to a Clean State

```bash
git reset --hard HEAD          # Discard all local changes in working directory
```

### Undo a Reset

```bash
git reflog                     # View history of HEAD changes
git reset --hard HEAD@{n}      # Reset to a previous state (n is the reflog index)
```

Note: Be cautious with `--hard` resets as they discard changes permanently. Always commit or stash important changes before using `--hard`.

Replace `<commit>` with a commit hash, branch name, or relative reference (e.g., HEAD~3).
