Skip to content

Instantly share code, notes, and snippets.

@rgreenjr
Last active December 20, 2023 13:30
Show Gist options
  • Save rgreenjr/96d182fe3c47ed6fce82 to your computer and use it in GitHub Desktop.
Save rgreenjr/96d182fe3c47ed6fce82 to your computer and use it in GitHub Desktop.
Git Cheat Sheet

Git Cheat Sheet

Common Commands

# stage all modified files being tracked
git add -u
# revert file
git checkout -- filepath
# change comment of last commit
git commit --amend
# unstage all files
git reset HEAD
# unstage one file
git reset HEAD -- filepath
# undo last commit (leaving files in index)
git reset --soft HEAD~
# undo last commit and unstage all files
git reset HEAD~
# revert to last local commit (throwing away changes)
git reset --hard
# revert changes of a merge
git reset --hard ORIG_HEAD
# show difference between the HEAD and the index
git diff --staged
# show diff between HEAD and both staged and unstaged changes (what would be committed with 'git commit -a')
git diff HEAD
# diff two branches
git diff --stat branch1...branch2
# show history
git log --graph --oneline
# find the most recent common ancestor of two branches
git merge-base branch1 branch2
# remove deleted files from git
git status | grep deleted | awk '{print \$3}' | xargs git rm
# show information about your remotes
git remote show origin
# set up tracking remote branch
git remote add origin [email protected]:rgreenjr/foobar.git
# push local branch with tracking to remote repo
git push -u origin FEATURE_BRANCH
# pull remote branch to local repo
git fetch origin
git checkout --track origin/branch_name
# delete remote branch
git push origin --delete branch_name
# cleanup stale remote branches
git remote prune origin
# rebase development branch onto local feature branch (usually prior to merging feature branch onto development branch)
git checkout branch_name
git rebase development
# merge local feature branch onto development without fast-forwarding
git checkout development
git merge --no-ff branch_name
# add tag
git tag 1.2.3
git push origin v1.2.3

Contributing to Open Source

Creating a Fork

# clone the origianl repo
git clone [email protected]:USERNAME/FORKED_PROJECT.git
# add 'upstream' repo to list of remotes
git remote add upstream [email protected]:UPSTREAM_USER/ORIGINAL_PROJECT.git
# verify the new remote named 'upstream'
git remote -v
# create a new branch
git checkout -b FEATURE_BRANCH
# push new feature branch to cloned repo with tracking
git push -u origin FEATURE_BRANCH

Submitting a Pull Request

# fetch upstream master and merge with your repo's master branch
git fetch upstream
git checkout master
git merge upstream/master
# if there were any new commits, rebase your development branch
git checkout FEATURE_BRANCH
git rebase master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment