Git Cheat Sheet

this is a cheat sheet for all the most common commands in git as a helpful reminder and a reference for git version control

Git Basics

CommandDescription
git init <directory>create empty git repo in directory (directory is optional)
git clone <repo>clone repo located at onto local machine
git config user.name <name>Define author name to be used for all commits in current repo
git add <directory>Stage all changes in for the next commit
git commit -m "<message>"Commit the staged snapshot, use <message> to add message
git statusList which files are staged, unstaged, and untracked.
git logDisplay the entire commit history using the default format
git diffShow upstaged changes between your index and working directory.

Undoing Changes

CommandDescription
git revert <commit>Create a new commit that undoes all the changes made in the specified commit, then applies it to the current branch.
git reset <file>Remove the file from the staging area but leave the working directory unchanged. This unstages the file without overwriting changes.
git clean -nShows which files would be removed from the working directory. Use the -f flag to force the clean operation.

Rewriting Git History

CommandDescription
git commit --amendReplace the last commit with the staged changes and last commit combined. nothing staged = edit message
git rebase <base>Rebase the current branch onto <base> (commit ID, branch name, tag, reference to HEAD)
git reflogShow a log of changes to the local repository’s HEAD -all tag to see all

Git Branches

CommandDescription
git branchList all of the branches in your repo. Add <branch> to create new branch
git checkout -b <branch>create and checkout a new branch
git merge <branch>Merge <branch> into the current branch

Remote Repositories

CommandDescription
git remote add <name> <url>Create a new connection to a remote repo. use <name> as shortcut in other commands
git fetch <remote> <branch>Fetches a specific <branch>, from the repo. remove <branch> to fetch all refs
git pull <remote>Fetch the specified remote’s copy of current branch and immediately merge it into the local copy.
git push <remote> <branch>Push the branch to , along with necessary commits and objects

Additional Options +

Git Config

CommandDescription
git config --global user.name <name>Define the author name to be used for all commits by the current user.
git config --global user.email <email>Define the author email to be used for all commits by the current user.
git config --global alias.<alias-name> <git-command>Create a shortcut for a Git command. E.g. alias.glog sets git glog equivalent to git log --graph --oneline.
git config --system core.editor <editor>Set the text editor used by commands for all users on the machine. <editor> should be the command that launches the desired editor (e.g., vi).
git config --global --editOpen the global configuration file in a text editor for manual editing.

Git Log

CommandDescription
git logDisplay the commit history for the current branch, showing commit messages, authors, and dates.
git log <file>View the commit history for a specific file, including changes made to that file over time.
git log --onelineShow a condensed, one-line summary of each commit in the history.
git log --graphDisplay the commit history as a text-based graph, showing branching and merging visually.
git log --since=<date>View commits since a specific date, e.g., git log --since=2023-01-01.
git log --author=<author>Filter the commit history by the author’s name or email, e.g., git log --author="John Smith".
git log --grep=<search-term>Search commit messages for a specific keyword or phrase, e.g., git log --grep="bug fix".
git log --statDisplay a summary of changes (insertions and deletions) made in each commit.
git log --follow <file>Track the history of a file even if it has been renamed or moved.
git log --mergesShow only merge commits in the commit history.
git log --no-mergesExclude merge commits from the commit history.
git log --pretty=format:"<format>"Customize the log output format using placeholders, e.g., git log --pretty=format:"%h %s".
git log <branch1>..<branch2>Display the commits that are in branch2 but not in branch1, showing the branch’s divergence.
git log --graph --decorate --oneline --allA comprehensive view that combines graph visualization with additional details and all branches.

Git Diff

CommandDescription
git diff HEADShow difference between working dir and last commit
git diff --cachedShow difference between staged changes and last commit

Git Reset

CommandDescription
git resetReset staging area to match most recent commit, but leave the working directory unchanged.
git reset --hardReset staging area and working directory to match most recent commit and overwrites all changes in the working directory.
git reset <commit>Move the current branch tip backward to <commit>, reset the staging area to match, but leave the working directory alone.
git reset --hard <commit>Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after <commit>.

Git Rebase

CommandDescription
git rebase -i <base>Interactively rebase the current branch onto <base>. Launches an editor to enter commands for how each commit will be transferred to the new base.

Git Pull

CommandDescription
git pull --rebase <remote>Fetch the remote’s copy of the current branch and rebase it into the local copy. Uses git rebase instead of merge to integrate the branches.

Git Push

CommandDescription
git push <remote> --forceForces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing.
git push <remote> --allPush all of your local branches to the specified remote.
git push <remote> --tagsTags aren’t automatically pushed when you push a branch or use the --all flag. The --tags flag sends all of your local tags to the remote repo.

Git Sync with Upstream

CommandDescription
git remote add upstream <URL-of-original-repo> ensure that you have the original repository set as a remote
git fetch upstreamRetrieve the changes from the upstream repository
git checkout main THEN git merge upstream/mainMerge the upstream changes into your local branch