diff --git a/README.md b/README.md index ea0bd31..fc05c39 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,17 @@ A cheatsheet of Git commands. -- [cd, ls](#terminal-navigation) -- [git init, git config](#git-setup) -- [status, log](#state-checking) -- [editor](#git-editor) -- [git remote](#remote-branch) -- [git add, git commit](#add-and-commit) -- [delete local and remote branch](#delete-local-and-remote-branch) +- [terminal](#terminal) +- [setup](#setup) +- [state](#state) +- [branch](#branch) +- [remote](#remote) +- [commit](#commit) +- [stash](#stash) +- [sync](#sync) +- [rewrite history](#rewrite-history) -## terminal navigation +## terminal ```bash # navigate to a particular folder cd "" @@ -22,11 +24,8 @@ cd ls ``` -## git setup +## setup ```bash -# initialize a git repository -cd "" -git init # check global and local git settings git config --global -l git config --local -l @@ -38,9 +37,14 @@ git config --global core.editor "nano -w" cd "" git config --local user.name "" git config --local user.email "" +# initialize a local git repository +cd "" +git init +# clone a remote repository +git clone ``` -## state checking +## state ```bash # check git status git status @@ -50,7 +54,23 @@ git log --oneline # condense each commit to a single line git log --oneline -n 5 # print only the last 5 commits ``` -## remote branch +## branch +```bash +# list all local branches +git branch +# create a local branch +git branch +# switch between branches and update working directory +git checkout +# delete local branch only if its commits are merged upstream +git branch -d +# delete local branch unconditionally +git branch -D +# delete remote branch +git push origin : +``` + +## remote ```bash # view remote settings git remote -v @@ -64,34 +84,51 @@ git remote add upstream git remote set-url upstream ``` -## add and commit +## commit ```bash # stage one file git add # stage all files (danger, this stages all changes) git add . +# unstage one file +git reset HEAD # commit with message git commit -m "Message" +# amend a local commit +git commit --amend # this will open up the git editor +# push local commits to origin +git push origin ``` -## delete local and remote branch +## stash ```bash -# delete local branch only if its commits are merged upstream -git branch -d -# delete local branch unconditionally -git branch -D -# delete remote branch -git push origin : +# list all stashed files +git stash list +# stash all modified files without committing +git stash +# restore the most recent stashed files +git stash pop +# drop the most recent stashed files +git stash drop ``` -## useful workflows +## sync ```bash -# sync a fork with an upstream repository +# sync a local fork with changes made in upstream/master git fetch upstream git checkout master git merge upstream/master -# sync a branch with new changes made in master +git push origin master +# sync a local branch with changes made in origin/master git fetch origin git checkout git merge origin/master ``` + +## rewrite history +```bash +# reset local and remote branches to a previous commit +git checkout +git reset --hard +git push -f origin +```