Here is a Git command reference, grouped by what they’re used for. With some explain what each command very generally does
Setup and Config
There are two commands that are used quite a lot, from the first invocations of Git to common every day tweaking and referencing, the config and help commands.
Git has a default way of doing hundreds of things. For a lot of these things, you can tell Git to default to doing them a different way, or set your preferences.
This involves everything from telling Git what your name is to specific terminal color preferences or what editor you use. There are several files this command will read from and write to so you can set values globally or down to specific repositories.
The git help command is used to show you all the documentation shipped with Git about any command. you can always run git help <command>.
Getting and Creating Projects
There are two ways to get a Git repository. One is to copy it from an existing repository on the network or elsewhere and the other is to create a new one in an existing directory.
To take a directory and turn it into a new Git repository so you can start version controlling it, you can simply run git init.
The git clone command is actually something of a wrapper around several other commands. It creates a new directory, goes into it and runs git init to make it an empty Git repository, adds a remote (git remote add) to the URL that you pass it (by default named origin), runs a git fetch from that remote repository and then checks out the latest commit into your working directory with git checkout.
For the basic workflow of staging content and committing it to your history, there are only a few basic commands.
The git add command adds content from the working directory into the staging area (or “index”) for the next commit. When the git commit command is run, by default it only looks at this staging area, so git add is used to craft what exactly you would like your next commit snapshot to look like.
The git status command will show you the different states of files in your working directory and staging area. Which files are modified and unstaged and which are staged but not yet committed. In its normal form, it also will show you some basic hints on how to move files between these stages.
The git diff command is used when you want to see differences between any two trees. This could be the difference between your working environment and your staging area (git diff by itself), between your staging area and your last commit (git diff --staged), or between two commits (git diff master branchB).
The git difftool command simply launches an external tool to show you the difference between two trees in case you want to use something other than the built in git diff command.
The git reset command is primarily used to undo things, as you can possibly tell by the verb. It moves around the HEAD pointer and optionally changes the index or staging area and can also optionally change the working directory if you use --hard. This final option makes it possible for this command to lose your work if used incorrectly, so make sure you understand it before using it.
The git rm command is used to remove files from the staging area and working directory for Git. It is similar to git add in that it stages a removal of a file for the next commit.
The git mv command is a thin convenience command to move a file and then run git add on the new file and git rm on the old file.
The git clean command is used to remove unwanted files from your working directory. This could include removing temporary build artifacts or merge conflict files.
Branching and Merging
There are just a handful of commands that implement most of the branching and merging functionality in Git.
The git branch command is actually something of a branch management tool. It can list the branches you have, create a new branch, delete branches and rename branches.
The git checkout command is used to switch branches and check content out into your working directory.
The git merge tool is used to merge one or more branches into the branch you have checked out. It will then advance the current branch to the result of the merge.
The git mergetool command simply launches an external merge helper in case you have issues with a merge in Git.
The git log command is used to show the reachable recorded history of a project from the most recent commit snapshot backwards. By default it will only show the history of the branch you’re currently on, but can be given different or even multiple heads or branches from which to traverse. It is also often used to show differences between two or more branches at the commit level.
The git stash command is used to temporarily store uncommitted work in order to clean out your working directory without having to commit unfinished work on a branch.
The git tag command is used to give a permanent bookmark to a specific point in the code history. Generally this is used for things like releases.
Sharing and Updating Projects
There are not very many commands in Git that access the network, nearly all of the commands operate on the local database. When you are ready to share your work or pull changes from elsewhere, there are a handful of commands that deal with remote repositories.
The git fetch command communicates with a remote repository and fetches down all the information that is in that repository that is not in your current one and stores it in your local database.
The git pull command is basically a combination of the git fetch and git merge commands, where Git will fetch from the remote you specify and then immediately try to merge it into the branch you’re on.
The git push command is used to communicate with another repository, calculate what your local database has that the remote one does not, and then pushes the difference into the other repository. It requires write access to the other repository and so normally is authenticated somehow.
The git remote command is a management tool for your record of remote repositories. It allows you to save long URLs as short handles, such as “origin” so you don’t have to type them out all the time. You can have several of these and the git remote command is used to add, change and delete them.
The git submodule command is used to manage external repositories within a normal repositories. This could be for libraries or other types of shared resources.
The submodule command has several sub-commands (add, update, sync, etc) for managing these resources.
Inspection and Comparison
The git show command can show a Git object in a simple and human readable way. Normally you would use this to show the information about a tag or a commit.
The git shortlog command is used to summarize the output of git log. It will take many of the same options that the git log command will but instead of listing out all of the commits it will present a summary of the commits grouped by author.
The git describe command is used to take anything that resolves to a commit and produces a string that is somewhat human-readable and will not change. It’s a way to get a description of a commit that is as unambiguous as a commit SHA-1 but more understandable.
Git has a couple of commands that are used to help debug an issue in your code. This ranges from figuring out where something was introduced to figuring out who introduced it.
The git bisect tool is an incredibly helpful debugging tool used to find which specific commit was the first one to introduce a bug or problem by doing an automatic binary search.
The git blame command annotates the lines of any file with which commit was the last one to introduce a change to each line of the file and what person authored that commit. This is helpful in order to find the person to ask for more information about a specific section of your code.
The git grep command can help you find any string or regular expression in any of the files in your source code, even older versions of your project.
A few commands in Git are centered around the concept of thinking of commits in terms of the changes they introduce, as though the commit series is a series of patches. These commands help you manage your branches in this manner.
The git cherry-pick command is used to take the change introduced in a single Git commit and try to re-introduce it as a new commit on the branch you’re currently on. This can be useful to only take one or two commits from a branch individually rather than merging in the branch which takes all the changes.
The git rebase command is basically an automated cherry-pick. It determines a series of commits and then cherry-picks them one by one in the same order somewhere else.
The git revert command is essentially a reverse git cherry-pick. It creates a new commit that applies the exact opposite of the change introduced in the commit you’re targeting, essentially undoing or reverting it.
Many Git projects, including Git itself, are entirely maintained over mailing lists. Git has a number of tools built into it that help make this process easier, from generating patches you can easily email to applying those patches from an email box.
The git apply command applies a patch created with the git diff or even GNU diff command. It is similar to what the patch command might do with a few small differences.
The git am command is used to apply patches from an email inbox, specifically one that is mbox formatted. This is useful for receiving patches over email and applying them to your project easily.
The git format-patch command is used to generate a series of patches in mbox format that you can use to send to a mailing list properly formatted.
The git imap-send command uploads a mailbox generated with git format-patch into an IMAP drafts folder.
The git send-email command is used to send patches that are generated with git format-patch over email.
The git request-pull command is simply used to generate an example message body to email to someone. If you have a branch on a public server and want to let someone know how to integrate those changes without sending the patches over email, you can run this command and send the output to the person you want to pull the changes in.
Git comes with a few commands to integrate with other version control systems.
The git svn command is used to communicate with the Subversion version control system as a client. This means you can use Git to checkout from and commit to a Subversion server.
For other version control systems or importing from nearly any format, you can use git fast-import to quickly map the other format to something Git can easily record.
If you’re administering a Git repository or need to fix something in a big way, Git provides a number of administrative commands to help you out.
The git gc command runs “garbage collection” on your repository, removing unnecessary files in your database and packing up the remaining files into a more efficient format.
This command normally runs in the background for you, though you can manually run it if you wish.
The git fsck command is used to check the internal database for problems or inconsistencies.
The git reflog command goes through a log of where all the heads of your branches have been as you work to find commits you may have lost through rewriting histories.