# gitsw
A smart Git branch switcher with automatic stash management and dependency installation.


## Features
- **Interactive branch picker** - Fuzzy search through branches with recent ones first
- **Auto-stash on leave** - Automatically stash uncommitted changes when switching branches
- **Auto-unstash on return** - Restore your changes when you come back to a branch
- **Dependency sync** - Detects package manager changes and prompts to run install
- **Recent branches** - Quick access to your most recently used branches
- **Remote tracking** - Easily checkout and track remote branches
## Installation
### Quick Install (macOS/Linux)
```bash
### From crates.io
```bash
cargo install gitsw
```
### From GitHub
```bash
cargo install --git https://github.com/yigiterdev/gitsw
```
### From source
```bash
git clone https://github.com/yigiterdev/gitsw.git
cd gitsw
cargo install --path .
```
### Pre-built binaries
Download the latest release for your platform from [Releases](https://github.com/yigiterdev/gitsw/releases).
| macOS (Apple Silicon) | [gitsw-aarch64-apple-darwin.tar.gz](https://github.com/yigiterdev/gitsw/releases/latest) |
| macOS (Intel) | [gitsw-x86_64-apple-darwin.tar.gz](https://github.com/yigiterdev/gitsw/releases/latest) |
| Linux (x64) | [gitsw-x86_64-unknown-linux-gnu.tar.gz](https://github.com/yigiterdev/gitsw/releases/latest) |
| Windows (x64) | [gitsw-x86_64-pc-windows-msvc.zip](https://github.com/yigiterdev/gitsw/releases/latest) |
## Usage
### Interactive Mode
Just run `gitsw` without arguments to get an interactive branch picker:
```bash
gitsw
```
Use arrow keys to navigate, type to filter, Enter to select.
### Basic Switching
```bash
gitsw main # Switch to main branch
gitsw feature-auth # Switch to feature-auth branch
```
If you have uncommitted changes, you'll be prompted to stash, discard, or abort.
### Create New Branch
```bash
gitsw -c new-feature # Create and switch to new-feature
```
### Pull After Switch
```bash
gitsw main -p # Switch to main and pull latest
gitsw -p feature # Switch to feature and pull
```
### Track Remote Branch
```bash
gitsw -t origin/feature-api # Fetch, create local branch, and track remote
```
### View Status
```bash
gitsw -s
```
Output:
```
Branch: feature-auth
Changes: 3 modified, 1 untracked
Stash: present
Package manager: npm
Tracking: origin/feature-auth
```
### Recent Branches
```bash
gitsw -r
```
Output:
```
Recent branches:
* [1] feature-auth (current)
[2] main (2 hours ago)
[3] develop [stash] (yesterday)
[4] feature-api (3 days ago)
```
### List Stashes
```bash
gitsw -l
```
Shows all branches that have gitsw-managed stashes.
### Delete Branch
```bash
gitsw -d old-feature # Delete branch (cleans up associated stash)
```
## All Options
```
Usage: gitsw [OPTIONS] [BRANCH]
Arguments:
[BRANCH] Target branch to switch to (interactive picker if omitted)
Options:
-l, --list List branches with stashes
-r, --recent Show recent branches
-s, --status Show current status (branch, stash, changes)
-d, --delete <BRANCH> Delete a branch (with stash cleanup)
-t, --track <REMOTE/BRANCH> Track and switch to a remote branch
--no-stash Skip automatic stash behavior
--no-install Skip automatic package install
-c, --create Create branch if it doesn't exist
-p, --pull Pull latest changes after switching
-h, --help Print help
-V, --version Print version
```
## Package Manager Support
gitsw automatically detects and handles lock file changes for:
| npm | `package-lock.json` |
| yarn | `yarn.lock` |
| pnpm | `pnpm-lock.yaml` |
When switching branches, if the lock file has changed, you'll be prompted to run the appropriate install command.
## How It Works
1. **On branch leave**: If you have uncommitted changes, gitsw offers to stash them with a branch-specific identifier
2. **On branch switch**: gitsw checks out the target branch
3. **On branch return**: If a stash exists for the branch, gitsw automatically applies and drops it
4. **Lock file check**: Compares lock file hash with stored value, prompts for install if changed
State is stored in `.git/git-switch.json` within your repository.
## Examples
```bash
# Daily workflow
gitsw # Pick branch interactively
gitsw main -p # Update main branch
gitsw -c fix/bug-123 # Start new bugfix branch
gitsw -r # See recent branches
gitsw -t origin/release # Track release branch from remote
# Skip prompts
gitsw main --no-stash # Switch without stashing (may fail with conflicts)
gitsw feature --no-install # Switch without install prompt
```
## License
MIT License - see [LICENSE](LICENSE) for details.
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
### Development Setup
```bash
# Clone the repo
git clone https://github.com/yigiterdev/gitsw.git
cd gitsw
# Install pre-commit hooks
./scripts/setup-hooks.sh
# Build and test
cargo build
cargo test
```
The pre-commit hook will automatically run `cargo fmt --check` and `cargo clippy` before each commit.