# Git Cheatsheet

# Git Cheatsheet – The Ultimate Guide

Whether you're just getting started with Git or you're a seasoned developer, having a handy cheatsheet saves time and avoids Googling the same commands over and over again. This guide is organized into **Basic**, **Intermediate**, and **Advanced** sections to cover all levels of Git usage.

---

## Basic Git Commands

These commands help you set up a repository and perform everyday version control tasks.

### Setup

```bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
```

### Create or Clone Repository

```bash
git init                         # Initialize a new Git repo
git clone https://github.com/user/repo.git  # Clone an existing repo
```

### Stage & Commit

```bash
git status                       # See changes
git add file.txt                 # Stage a file
git add .                        # Stage all changes
git commit -m "message"          # Commit staged changes
```

### Branching Basics

```bash
git branch                       # List branches
git switch main                  # Switch to branch 'main'
git switch -c new-feature        # Create and switch to 'new-feature'
git merge main                   # Merge 'main' into current branch
```

---

## Intermediate Git Commands

Useful when working in teams and collaborating via remote repositories.

### Remote Repos

```bash
git remote -v                    # Show remotes
git remote add origin URL        # Add remote repo
git push -u origin main          # Push first time with upstream
git push                         # Push commits
git pull                         # Fetch and merge from origin
git fetch                        # Fetch changes without merging
```

### Working with Files

```bash
git rm file.txt                  # Remove a tracked file
git mv old.txt new.txt           # Rename or move file
git restore file.txt             # Discard local changes
git restore --staged file.txt    # Unstage a file
```

### View History

```bash
git log                          # Full commit history
git log --oneline --graph        # Compact and visual log
git diff                         # View unstaged changes
git diff --staged                # View staged changes
git blame file.txt               # Show who last modified each line
```

---

## Advanced Git Commands

Commands for rewriting history, debugging, and working with complex workflows.

### Rewriting History

```bash
git commit --amend               # Edit the last commit
git rebase -i HEAD~3             # Interactive rebase last 3 commits
git reflog                       # Show all history (even deleted commits)
```

### Cleanups & Resets

```bash
git stash                        # Temporarily save changes
git stash pop                    # Reapply saved changes
git reset HEAD~1                 # Undo last commit (keep changes)
git reset --hard HEAD~1          # Delete last commit and changes
git clean -fd                    # Remove untracked files and folders
```

### Undo Merge or Rebase

```bash
git merge --abort                # Abort a merge
git rebase --abort               # Abort a rebase
```

### Cherry Pick & Bisect

```bash
git cherry-pick <commit>         # Apply a specific commit
git bisect start                 # Start binary search for bugs
git bisect bad                   # Mark current commit as bad
git bisect good <commit>         # Mark known good commit
git bisect reset                 # End bisect session
```

---

## Common Git Bash Aliases for Speed

Typing full Git commands repeatedly can be tedious. These aliases make common tasks faster and easier. Add them to your `~/.bashrc` or `~/.zshrc`.

### Bash Aliases

```bash
alias gs="git status"
alias ga="git add"
alias gaa="git add ."
alias gc="git commit -m"
alias gcm="git commit -m"
alias gsw="git switch"
alias gswc="git switch -c"
alias gb="git branch"
alias gm="git merge"
alias gp="git push"
alias gpl="git pull"
alias gl="git log --oneline --graph --decorate"
alias gd="git diff"
alias gds="git diff --staged"
alias gr="git restore"
alias grs="git restore --staged"
alias gstash="git stash"
alias gpop="git stash pop"
alias grh="git reset --hard"
alias gundo="git reset --soft HEAD~1"
```

### Example Usage

```bash
gs           # git status
gaa          # git add .
gcm "msg"    # git commit -m "msg"
gswc feat-x  # git switch -c feat-x
```

### Reload the Shell

```bash
source ~/.bashrc    # or ~/.zshrc
```

---

## Optional: Git Aliases in Git Config

Prefer aliases within Git itself? Add these:

```bash
git config --global alias.st status
git config --global alias.ci commit
git config --global alias.sw switch
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --decorate"
```

Use them like:

```bash
git st
git sw main
git lg
```

---

## Final Thoughts

This cheatsheet covers the most essential Git commands for day-to-day development and collaboration. As your workflow becomes more advanced, these quick references will help you stay productive and efficient.

Happy coding!
