checkout
Switch branches or restore files from a commit.
Synopsis
rit checkout <branch> # Switch to a branch
rit checkout <commit-hash> # Switch to a commit (detached HEAD)
rit checkout -- <file> # Restore a file from HEAD
rit checkout <commit> -- <file> # Restore a file from a specific commit
Description
The checkout command allows you to:
- Switch branches - Move HEAD to point to a different branch and update the working directory
- Checkout a commit - Move to a specific commit (detached HEAD state)
- Restore files - Restore individual files from a commit or branch
Switching Branches
When you checkout a branch:
- HEAD is updated to point to the branch
- The working directory is updated to match the branch's commit
- Files are written from the branch's tree
Detached HEAD
When you checkout a commit hash directly (not a branch), you enter "detached HEAD" state:
- HEAD points directly to the commit hash
- You can make commits, but they won't be on any branch
- Useful for inspecting old commits
Restoring Files
You can restore individual files from any commit or branch:
rit checkout HEAD -- <file>- Restore from current commitrit checkout <branch> -- <file>- Restore from a branchrit checkout <commit> -- <file>- Restore from a specific commit
Options
-f,--force- Force checkout, overwriting local changes (use with caution)
Examples
Switch to a branch
$ rit branch
* main
feature
$ rit checkout feature
Switched to branch 'feature'
Checkout a specific commit
$ rit checkout abc1234
Note: checking out 'abc1234'.
You are in 'detached HEAD' state.
Restore a file from HEAD
$ rit checkout -- file.txt
Updated 'file.txt'
Restore a file from a branch
$ rit checkout feature -- file.txt
Updated 'file.txt'
Force checkout (overwrite local changes)
$ rit checkout -f main
Switched to branch 'main'
How It Works
Branch Checkout
- Resolve the branch name to a commit hash
- Read the commit to get its tree hash
- Recursively read the tree and write files to the working directory
- Update HEAD to point to the branch
File Checkout
- Resolve the reference (branch/commit) to a commit hash
- Navigate through the tree to find the file
- Read the blob and write it to the working directory
Tree Writing
When writing a tree to the working directory:
- Directories are created as needed
- Files are written with their blob contents
- Executable permissions are preserved (on Unix systems)
Current Branch vs Detached HEAD
After checkout:
- Branch checkout: HEAD contains
ref: refs/heads/<branch> - Commit checkout: HEAD contains the commit hash directly
You can check your state with:
$ cat .rit/HEAD
ref: refs/heads/main # On a branch
# or
abc1234... # Detached HEAD
Warnings
- Local changes: If files have been modified, checkout will warn and skip them (unless
--forceis used) - Untracked files: Untracked files are not removed during checkout
- Detached HEAD: Commits made in detached HEAD state won't be on any branch