Data report"State of code review 2024" is now liveRead the full report

Git commands cheat sheet

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


This guide contains some of the most commonly used Git commands, what they do, when to use them, along with example of them in action.

  • 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.

  • 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.

  • 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.

  • git status

    • Example: git status

    • When to use: Check the status of your working directory and see which files have been modified and staged.

  • 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.

  • 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.

  • git diff

    • Example: git diff

    • When to use: View the changes you've made but not yet staged.

  • 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.

  • git branch

    • Example: git branch

    • When to use: List all branches in your repository to see which branches are currently.

  • 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.

  • git checkout [branch-name]

    • Example: git checkout feature-x

    • When to use: Switch to another branch to work on it or check its content.

  • 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.

  • 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.

  • 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.

  • 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.

  • 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.

  • 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.

  • 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.

  • 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.

  • 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.

    • 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.

    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.

    • 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.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2