Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Last active February 25, 2022 20:08
Show Gist options
  • Save bradmontgomery/b1c40cb5c63a901738ce to your computer and use it in GitHub Desktop.
Save bradmontgomery/b1c40cb5c63a901738ce to your computer and use it in GitHub Desktop.
a little git cheat sheet.

git cheat sheet

Interactively stage changes in a file

git add -ip <file>

Create a new, remote tracking branch (without branching from a local branch)

git co -b somebranch upstream/somebranch

Push a local branch to a differently named remote branch

git push origin localbranch:remotebranch

Checkout a branch from a remote fork

  git remote add user git@github.com:user/project.git
  git fetch user
  git co -b user/their-branch
  git pull user their-branch

Pull from someone else's branch on github

git pull git@github.com:user/project.git remotebranch

Push back to someone else's branch on github

git push git@github.com:user/project.git localbranch:remotebranch

Cherry picking commits

  git cherry-pick foo        # Grab the last commit from the foo branch
  git cherry-pick foo foo~1  # Grab the last two commits from foo

Pop an old stash

git stash pop stash@{X}   # where X is the stash number

Rebasing & Conflicts (because I sometimes forget this).

  When you have resolved this problem, run "git rebase --continue".
  If you prefer to skip this patch, run "git rebase --skip" instead.
  To check out the original branch and stop rebasing, run "git rebase --abort"

Need to squash some commits? You do that with rebase. See this handy guide.

Want to complete destroy your last few commits? (never do this if you've shared these commits)

git reset --hard HEAD~X   # where X is the number of commits backwards

Prune remote branches. When you run git branch -a and see references to remote branches (e.g. remotes/origin/deleted-branch) that have been deleted up-stream, you can remove your local references to them with:

git remote prune origin  # prune the remote named "origin"

Need to learn more?

git help 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment