Git Cheat Sheet

Git Basics

Command Description
git config --global user.name "[name]" Sets author name to be used for all your commits.
git config --global user.email "[email]" Sets author email to be used for all your commits.
git init [project-name] Create empty Git repo in specified directory.
git clone [repo] Clone repo with version history located at [repo].
git status List all new or modified files to be committed.
git diff Show file changes not yet staged.
git add [file] Stage all changes in [file] for the next commit.
git diff --staged Shows files differences between staging and the last file version.
git reset [file] Unstages the file, but preserves its contents.
git commit -m “[message]” Commit the staged snapshot to permanent version history.
git log Display the entire commit history for the current branch.

 

Undoing changes

Command Description
git revert [commit] Create new commit that undoes all of the changes made in [commit], then apply it to the current branch.
git reset [file] Remove [file] from the staging area, but leave the working directory unchanged. This un-stages a file without overwriting any changes.
git clean -n Shows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.

 

Rewriting Git History

Command Description
git commit —amend Replace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message.
git rebase [base] Rebase the current branch onto [base]. [base] can be a commit ID, a branch name, a tag, or a relative reference to HEAD.
git reflog Show a log of changes to the local repository’s HEAD. Add --relative-date flag to show date info or --all to show all refs.

 

Git Branches

Command Description
git branch List all of the branches in your repo. Add a branch argument to create a new branch with the name branch.
git checkout -b [branch] Create and check out a new branch named [branch]. Drop the -b flag to checkout an existing branch.
git merge [branch] Merge [branch] into the current branch.

 

Remote Repositories

Command Description
git remote add [name] [url] Create a new connection to a remote repo.
git fetch [remote] Downloads all history from the remote repo.
git merge [remote]/[branch] Combines the remote branch into the current local branch
git pull [remote] Downloads and merges remote’s copy of current branch.
git push [remote] [branch] Uploads the branch and history to [remote].

 

Git Log

Command Description
git log -[limit] Limit number of commits by [limit]. E.g. git log -5 will limit to 5 commits.
git log --oneline Condense each commit to a single line.
git log -p Display the full diff of each commit.
git log —-stat Include which files were altered and the relative number of lines that were added or deleted from each of them.
git log —-author= ”[pattern]” Search for commits by a particular author.
git log —grep=”[pattern]” Search for commits with a commit message that matches [pattern].
git log [since]..[until] Shows commits that occur between [since] and [until]. Args can be a commit ID, branch name, HEAD, or any other kind of revision reference.
git log -- [file] Only display commits that have the specified file.
git log --graph --decorate --graph flag draws a text based graph of commits on left side of commit msgs. --decorate adds names of branches or tags of commits shown.

 

Git Diff

Command Description
git diff HEAD Show difference between working directory and last commit.
git diff --cached Show difference between staged changes and last commit.

 

Git Reset

Command Description
git reset Reset staging area to match most recent commit, but leave the working directory unchanged.
git reset --hard Reset 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].