1. cheat-sheets
  2. /git

Git Cheat Sheet - A Detailed Reference

Git is a widely adopted version control system that allows developers to track code changes and collaborate. It's a staple in the software industry, assisting both individuals and teams in managing code revisions efficiently.

In the sections below, we've provided a concise rundown of varied Git commands and practices, catering to a range of expertise levels. From solo projects to collaborative team efforts, this reference is designed to speed up your workflow with Git's robust version control capabilities.

Basic Commands

CommandDescription
git initInitialize a new Git repository
git clone <repo>Clone an existing repository
git add <file>Add a file to the staging area
git commit -m "<message>"Commit changes with a message
git statusView the current status of the repository
git logView the commit history
git diffView differences between committed versions

Branching and Merging

CommandDescription
git branch <name>Create a new branch
git checkout <branch>Switch to a different branch
git merge <branch>Merge a branch into the current branch
git branch -D <branch>Delete a branch
git branch -m <newname>Rename a branch
git branch -aList all local and remote branches
git checkout -b <branch>Create and switch to a new branch

Remote Repositories

CommandDescription
git pullPull changes from a remote repository
git pushPush local commits to a remote repository
git fetchFetch changes from a remote repository without merging them
git push <remote> --delete <branch>Delete a remote branch
git push <remote> <branch>Push a local branch to a remote repository
git push <remote> --tagsPush all tags to a remote repository
git remote -vList all remote repositories
git remote add <name> <url>Add a remote repository
git remote rename <oldname> <newname>Rename a remote repository
git remote remove <name>Remove a remote repository
git remote set-url <name> <newurl>Set the URL for a remote repository

Stashing and Cleaning

CommandDescription
git stashStash changes to temporarily shelve them
git stash listView a list of stashes
git stash applyApply the latest stash
git stash popApply the latest stash and remove it from the stash list
git stash dropRemove the latest stash from the stash list
git stash clearRemove all stashes from the stash list
git clean -fRemove untracked files from the repository

Tagging and Submodules

CommandDescription
git tag <tagname>Create a tag to mark a specific commit
git tag -d <tagname>Delete a tag
git tag -v <tagname>Verify a tag's GPG signature
git submodule <repo>Add a submodule to the repository
git submodule initInitialize submodules in the repository
git submodule updateUpdate submodules to the latest commit
git submodule syncSynchronize submodule URLs with the parent repository
git submodule statusView the status of submodules in the repository
git submodule deinit <repo>Remove a submodule from the repository
git submodule foreach <command>Run a command in each submodule
git submodule summaryView a summary of changes in submodules

Configuration

CommandDescription
git config --global user.name "<name>"Set the user's name for Git commits
git config --global user.email "<email>"Set the user's email for Git commits
git config --global --listView the user's Git configuration
git config --global color.ui autoEnable color output for Git
git config --global core.editor "<editor>"Set the default text editor for Git
git config --global alias.<alias> "<command>"Set an alias for a Git command
git config --global --unset <key>Unset a Git configuration value
git config --global --replace-all <key> "<value>"Replace all instances of a Git configuration value
git config --global credential.helper "<helper>"Set a credential helper for Git
git config --global push.default simpleSet the default push behavior for Git
git config --global merge.conflictstyle diff3Set the default merge conflict style for Git

Miscellaneous Commands

CommandDescription
git reset <file>Unstage a file
git revert <commit>Revert to a previous commit
git cherry-pick <commit>Apply changes from a specific commit
git blame <file>View the commit history for a file
git push <remote> :<branch>Delete a remote branch

Advanced Commands

CommandDescription
git rebase <branch>Rebase the current branch onto another branch
git cherry-pick --continueContinue the process of cherry-picking after resolving conflicts
git bisect startStart a binary search to find the commit that introduced a bug
git bisect good <commit>Mark a commit as good during a binary search
git bisect bad <commit>Mark a commit as bad during a binary search
git bisect resetFinish the binary search and return to the original HEAD
git reflogShow a log of changes to the local repository's HEAD
git fsckCheck the integrity of the Git database
git gcClean up and optimize the Git repository

Downloadable Git Reference PDFs

GitHub has a great pdf that you can print out and put on your wall next to your desk. And yes, GitLab has one too.

There are several Git clients that developers use to interact with repositories. Some notable ones include:

  • Git for Windows: A port of Git command line interface for Windows, featuring a GUI for repository management alongside the command line tools.

  • GitKraken: A cross-platform Git client with a user-friendly graphical interface, ideal for those new to Git.

  • GitHub Desktop: Developed by GitHub for Windows and macOS, this client provides a GUI for managing repositories and integrates well with other GitHub features.

  • SourceTree: An Atlassian Git client for Windows and macOS, providing a GUI for repository management and support for other version control systems like Mercurial.

Additionally, many developers favor the command line interface for Git interactions, appreciating the control it provides. Advanced users might find it especially handy, and the commands listed in our cheat sheet can provide quick help.

Git Facts You May Find Interesting

  • Git was developed by Linus Torvalds in 2005 to manage the Linux kernel development, with a focus on speed, efficiency, and support for distributed workflows.

  • Git's "directed acyclic graph" (DAG) data structure enables the tracking of different versions of the project and the changes made in each version.

  • Other version control systems inspired by Git include Mercurial, Subversion, and Perforce.

  • While primarily written in C, Git also includes scripts in Perl and Shell, enhancing its portability across various operating systems.

  • The Git ecosystem has expanded over the years, with various tools and extensions, like GitHub and GitLab, built around it to facilitate collaborative coding and project management.

Additional Resources

Pro Git Book by Scott Chacon and Ben Straub

NPM Command Cheat Sheet

JavaScript Snippets for Quick Reference