Git Command Cheat Sheet
Git is an open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git was developed by Linus Torvalds to help manage the Linux kernel development.
Git Basics
git init <directory>Create an empty Git repository in the specified directory. Without an argument, initializes the current directory.
git clone <repo>Clone the repository at <repo> to the local machine. The source repo can be on the local filesystem or a remote machine via HTTP or SSH.
git config user.name <name>Define the author name for all commits in the current repository.
git add <directory>Stage changes in <directory> for the next commit.
git commit -m "<message>"Commit the staged snapshot using <message> as the commit message.
git statusList which files are staged, unstaged, and untracked.
git logDisplay the entire commit history in the default format.
git diffShow differences between the working directory and the staging area.
Undoing Changes
git revert <commit>Create a new commit that undoes all changes made in <commit> and apply it to the current branch.
git reset <file>Remove <file> from the staging area but keep the working directory unchanged. This unstages the file without overwriting any changes.
git clean -nShow which files will be removed from the working directory. Use the -f flag instead of -n to execute the cleanup.
Rewriting History
git commit --amendModify the most recent commit. If there are staged changes, they will be included in the modified commit.
git rebase <base>Rebase the current branch onto <base>. <base> can be a commit ID, branch name, tag, or HEAD.
git reflogView the log of all operations on all branches (including deleted commits and resets).
Git Branching
git branchList all branches. Add a <branch> argument to create a new branch named <branch>.
git checkout -b <branch>Create and check out a new branch named <branch>. Omit -b to check out an existing branch.
git merge <branch>Merge the <branch> branch into the current branch.
Remote Repositories
git remote add <name> <url>Add a new remote repository. You can then use <name> as a shortcut for <url> in other commands.
git fetch <remote> <branch>Fetch all updates for the specified <branch> from the repository, but do not merge with the local branch.
git pull <remote>Fetch the remote copy of the current branch and immediately merge it into the local copy.
git push <remote> <branch>Push the branch to <remote>, including necessary commits and objects. Creates a named branch in the remote repo if it doesn’t exist.
git config
git config --global user.name <name>Define the author name for all commits by the current user.
git config --global user.email <email>Define the author email for all commits by the current user.
git config --global alias.<alias-name> <git-command>Create a shortcut (alias) for a Git command.
git config --system core.editor <editor>Set the text editor used by commands for all users on the machine.
git config --global --editOpen the global configuration file in a text editor for manual editing.
git log
git log -<limit>Show the last <limit> commit entries.
git log --onelineDisplay commit history with each commit compressed to a single line.
git log -pDisplay commit history with full diffs for each commit.
git log --statDisplay commit history with stats on which files were modified and the relative lines added/deleted.
git log --author="<pattern>"Search for commits by a specific author.
git log --grep="<pattern>"Search commit history using regex matching.
git log <since>..<until>Show commits that occurred between <since> and <until>.
git log -- <file>Only show commit entries that include the specified file.
git diff
git diff HEADCompare the working directory with the repository (after git commit).
git diff --cachedCompare the staging area (after git add) with the repository.
git reset
git resetReset the staging area to match the most recent commit, leaving the working directory unchanged.
git reset --hardReset both the staging area and working directory to the most recent commit, overwriting all changes.
git reset <commit>Move the current branch tip back to <commit> and reset the staging area, without touching the working directory.
git reset --hard <commit>Same as above, but also resets the working directory. Deletes uncommitted changes and all commits after <commit>.
git pull
git pull --rebase <remote>Fetch the remote copy and rebase the local copy onto it instead of merging.
git push
git push <remote> --forceForce push changes.
git push <remote> --allPush all local branches to the specified remote.
git push <remote> --tagsPush all local tags to the remote repository.