Skip to content

Instantly share code, notes, and snippets.

@mvance
Last active May 12, 2022 17:30
Show Gist options
  • Save mvance/78d50e1ee766a3ee27f1 to your computer and use it in GitHub Desktop.
Save mvance/78d50e1ee766a3ee27f1 to your computer and use it in GitHub Desktop.
Git in ~25 Commands

Git in ~25 Commands

Stash ←→ Workspace★ ←→ Index (or “Staging” or “Cache”) ←→ Local Repository ←→ Remote Repository

Key

italics variable
[brackets] optional argument

Setup

git config --global user.name ‘name specify name to associate with all of your commits
git config --global user.email name@example.com specify email address to associate with all of your commits

Create

git init create a repository in the current directory
git clone url clone a remote repository into a subdirectory

View

git status show status of the working tree
git diff [path] show diff of changes between the working tree and HEAD
git diff --cached show diff between staged changes and HEAD
git log [from[..to]] view commit log for a given revision

Change

git add directory file1 file2 git add directory, file, or files recursively
git mv /source/path destination/path move file or directory to new location
git rm path remove file from working tree

Undo / Redo

git reset --soft HEAD^ undo previous commit and keep changes in the working tree
git reset --hard HEAD^ reset the working tree to the last commit (discard all pending changes)
git commit -a --amend replace last commit with a new one
git checkout -- discard uncommitted changes to the specified file in the working directory
git revert reference creates a new commit that reverses changes from previous commit(s)
test sha1-A = $(git merge-base sha1-A sha1-B) tests whether merging sha1-B into sha1-A is achievable as a fast forward; non-zero exit status is false

Update

git fetch [repository] fetch changes from a repostory
git pull [repository] fetch and merge changes from a repository

Branch

git branch list local branches
git checkout branch switch working tree to the specified branch
git checkout - switch back to previous branch
git checkout -b branch create, then switch to a new branch with the specified name
git merge branch merge changes from the specified branch to the current working directory
git mergetool open the default merge tool, to resolve merge conflicts
git log view commit log

Record

git commit [-m ‘commit message’] commit files that have been stages
git tag create a tag pointing to the current revision

Publish

git push [repository] [branch] push changes to a remote repository
git push -u origin master pushes the “master” branch to “origin” remote and sets up tracking
git remote list remote repositories
git remote add remote url add remote to list of tracked repositories

Bonus

git help [command] display “man” (manual) page for a git command
git command -h display a compact reference for the specified command
git stash save name create a new stash with the name specified
git stash apply name apply an existing stash to the working directory
git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short a colorful graphical version of the git log
git describe —tags shows the name of the tag assigned to the current working directory, if there is one
git bisect perform bisect debugging to find which commit introduced a bug
git config —list view all current configuration settings
git cherry-pick reference selectively merge individual commits from another local branch
git rebase —interactive HEAD~3 squash commits with rebase
git reflog use reflog to recover from major mistakes

Status flags

M modified file has been modified
C copy-edit file has been copied and modified
R rename-edit file has been renamed and modified
A added file has been added
D deleted file has been deleted
U unmerged file has conflicts after a merge

Object references

master default development branch
origin default upstream branch
HEAD current branch
HEAD^ parent of HEAD
HEAD^4 great-great grandparent of HEAD
foo..bar from reference foo to reference bar

Glossary

repository a collection of commits, each of which is a snapshot of the project’s working tree at a particular time
index a staging area for commits on their way to the local repository
working tree a directory on the filesystem with a repository associated with it
commit a snapshot of a working tree at a particular point in time
branch a line of development to which changes can be made
tag a name for a commit that is (intended to be) permanently associated with a commit
master a default branch
HEAD specifies what branch is currently checked out
SHA-1 has a 40-character hexadecimal checksum string, calculated based on the contents of a file or directory structure in git

Reference

Git Immersion an excellent interactive tutorial (if you read only one resource, make it this one)
Try Git an interactive in-browser tutorial on the basics of git
Git Parable a story that helps explain what git is doing behind the scenes
Git Ready a series of mini-tutorials organized by skill level
Git for Ages 4 and Up video of a presentation explaining git commands using Tinkertoys
Git Reference a concise introduction to the 20 most commonly used commands
A Visual Git Reference visual explanations of common commands (great example of how staging works)
A successful Git branching model a workflow for managing releases using Git
gitflow a suite of commands to streamline the workflow above
git-achievements turns learning git into a game
Pro Git a free online copy of the comprehensive Pro Git book
Install Bash git completion enable tab completion of git commands
Git cheat sheet, extended edition a printable cheat sheet, similar in scope to this presentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment