gitsw -t origin/feature-api
gitsw --track origin/release-v2
```bash
This is equivalent to:
```bash
git fetch origin
git checkout -b feature-api origin/feature-api
```text
## How It Works
<Steps>
<Step title="Parse remote/branch format">
The command expects the format `remote/branch` (e.g., `origin/feature-api`)
</Step>
<Step title="Fetch from remote">
gitsw fetches the latest refs from the specified remote
</Step>
<Step title="Check if local branch exists">
If a local branch with the same name already exists, gitsw switches to it
</Step>
<Step title="Create tracking branch">
If the local branch doesn't exist, gitsw creates it tracking the remote
</Step>
<Step title="Switch to the branch">
Finally, gitsw switches to the branch (with all normal stash/install features)
</Step>
</Steps>
## Implementation
From `src/main.rs:303-344`:
```rust
fn track_remote(
remote_branch: &str,
auto_stash: bool,
auto_install: bool,
pull: bool,
) -> Result<()> {
let repo = GitRepo::open()?;
// Parse remote/branch format
let parts: Vec<&str> = remote_branch.splitn(2, '/').collect();
if parts.len() != 2 {
return Err(anyhow!("Invalid format. Use: origin/branch-name"));
}
let remote = parts[0];
let branch = parts[1];
// Fetch from remote first
println!("{} Fetching from '{}'...", "info:".blue().bold(), remote);
repo.fetch(remote)?;
// Check if local branch already exists
if repo.branch_exists(branch) {
println!(
"{} Local branch '{}' already exists, switching to it",
"info:".blue().bold(),
branch
);
return switch_branch(branch, auto_stash, auto_install, false, pull);
}
// Create tracking branch
println!(
"{} Creating branch '{}' tracking '{}'...",
"info:".blue().bold(),
branch.green(),
remote_branch
);
repo.create_tracking_branch(branch, remote_branch)?;
switch_branch(branch, auto_stash, auto_install, false, pull)
}
```bash
## Usage Examples
### Track Remote Branch
```bash
gitsw -t origin/feature-api