This guide contains some of the most commonly used Git commands, what they do, when to use them, along with examples of them in action.
Setup and configuration
- git init - Example: git init
- When to use: Initialize a new Git repository in your current directory when starting a new project or deciding to track an existing project with Git.
 
- Example: 
- git config - Example: git config --global user.name "John Doe"
- When to use: Set your user name and email in the Git configuration when you're setting up Git for the first time on your machine.
 
- Example: 
- git clone [url] - Example: git clone https://github.com/user/repository.git
- When to use: Clone an existing repository to start working on a project that's hosted remotely.
 
- Example: 
Staging and capturing state
- git status - Example: git status
- When to use: Check the status of your working directory and see which files have been modified and staged.
 
- Example: 
- git add [file] - Example: git add README.md
- When to use: Add a file that you've modified or created to the staging area, preparing it for a commit.
 
- Example: 
- git reset [file] - Example: git reset README.md
- When to use: Unstage a file while retaining the changes in your working directory, if you decide not to commit it yet.
 
- Example: 
- git diff - Example: git diff
- When to use: View the changes you've made but not yet staged.
 
- Example: 
- git commit -m “<message>” - Example: git commit -m "Add README file"
- When to use: Commit your staged changes to the repository with a descriptive message, officially recording the snapshot in your project's history.
 
- Example: 
Branching and merging
- git branch - Example: git branch
- When to use: List all branches in your repository to see which branches are currently.
 
- Example: 
- git branch [branch-name] - Example: git branch feature-x
- When to use: Create a new branch when you want to work on a new feature or fix in isolation.
 
- Example: 
- git checkout [branch-name] - Example: git checkout feature-x
- When to use: Switch to another branch to work on it or check its content.
 
- Example: 
- git merge [branch] - Example: git merge feature-x
- When to use: Merge changes from one branch (e.g., a feature branch) into another (e.g., the main branch) after development/testing is complete.
 
- Example: 
Inspection and comparison
- git log - Example: git log
- When to use: Show the commit history for the currently active branch, useful for reviewing changes or finding a specific commit.
 
- Example: 
- git diff branchB...branchA - Example: git diff main...feature-x
- When to use: Compare what is in one branch that is not in another, useful for code reviews and understanding changes before merging.
 
- Example: 
Sharing and updating
- git remote add [alias] [url] - Example: git remote add origin https://github.com/user/repository.git
- When to use: Add a new remote to your local repository, usually done when you've initialized a local repository and want to link it to a remote server.
 
- Example: 
- git fetch [alias] - Example: git fetch origin
- When to use: Fetch updates from a remote repository to keep your local copies of branches up-to-date without merging those changes into your own branches.
 
- Example: 
- git pull - Example: git pull origin main
- When to use: Update your current branch with the latest changes from a remote branch, automatically fetching and then merging.
 
- Example: 
- git push [alias] [branch] - Example: git push origin main
- When to use: Upload your local branch commits to the remote repository to share your changes with others.
 
- Example: 
Tracking file removals
- git rm [file]- Example: git rm README.md
- When to use: Remove files from your working directory and stage the removal for commit, useful when you no longer need a file in the project.
 
- Example: 
Rewriting history
- git rebase [branch] - Example: git rebase main
- When to use: Reapply commits on top of another branch, often used to keep a feature branch up to date with the main branch. This command is particularly useful when you want to create a cleaner, linear project history.
 
- Example: 
- git reset --hard [commit] - Example: git reset --hard HEAD^
- When to use: Reset your current HEAD to a specific commit, discarding all changes in the working directory and staging area since that commit. Use with caution, as this can lead to loss of work.
 
- Example: 
Temporarily saving local commits
- git stash - Example: git stash
- When to use: Temporarily stash changes in your working directory so you can switch branches without committing incomplete work. Ideal for quick context switches.
 
- Example: 
- git stash pop - Example: git stash pop
- When to use: Apply stashed changes back to your working directory, effectively restoring the temporary work you stashed earlier.
 
- Example: 
Graphite CLI: Simplifying Git workflows
The Graphite CLI offers a streamlined way to manage complex Git workflows, especially with stacked pull requests, making it easier to manage large features or fixes across multiple branches.
Key commands
- gt checkout [branch-name] - Example: gt checkout main
- When to use: Quickly switch to the main branch or another branch within your project.
 
- Example: 
- gt create --all --message "[message]" - Example: gt create --all --message "Create login feature"
- When to use: Create a new branch and commit all changes with a descriptive message in one step.
 
- Example: 
- gt submit - Example: gt submit
- When to use: Submit your changes as a pull request to a remote repository, simplifying the process of review and merge.
 
- Example: 
- gt stack --submit --reviewers username - Example: gt stack --submit --reviewers alice
- When to use: Manage stacked pull requests by submitting all related changes at once and assigning a reviewer.
 
- Example: 
- gt sync - Example: gt sync
- When to use: Sync your branch with the latest updates from the base branch, ensuring all changes are up-to-date and conflicts are managed efficiently.
 
- Example: 
The Graphite CLI integrates seamlessly with your existing Git workflows, providing additional tools and commands to enhance productivity and collaboration. Whether you're working on a small project or managing large-scale development workflows, Graphite can simplify and speed up your Git operations.
