Skip to content

Instantly share code, notes, and snippets.

@raineorshine
Last active December 22, 2023 15:59
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 27 You must be signed in to fork a gist
  • Save raineorshine/5128563 to your computer and use it in GitHub Desktop.
Save raineorshine/5128563 to your computer and use it in GitHub Desktop.
Cheatsheet: git commands
# adding and committing
git add -A # stages All
git add . # stages new and modified, without deleted
git add -u # stages modified and deleted, without new
git commit --amend # Add staged changes to previous commit. Do not use if commit has been pushed.
git commit --amend --no-edit # Do so without having to edit the commit message.
# remotes - pushing, pulling, and tracking
git fetch # gets remote objects and refs. Needed if new branches were added on the remote.
git remote -v # Lists all remotes (verbose)
git pull origin master # Pulls commits from the 'origin' remote's master branch and stores them in the local repo
git push -u origin master # sets ups tracking so that you can 'git push' without extra args
git push them branch:theirbranch # Push to a contributor branch that has enabled owner edits
git show :/^Merge # show the last commit whose message matches a regex
# branches - creating, checking out, and merging
git branch # list branches
git branch -a # list branches including remotes
git branch <MYBRANCH> # Creates a new branch called "MYBRANCH"
git checkout <MYBRANCH> # Makes MYBRANCH the active branch
git checkout -b <MYBRANCH> # create and checkout a new branch
git branch -d <MYBRANCH> # delete a local branch
git branch -m <MYBRANCH> # rename the current branch
git checkout --track origin/<MYBRANCH> # create a new local branch with the same name as the remote and set "upstream" configuration
git merge <MYBRANCH> # merge the commits from the given branch into the current branch
# cloning
git clone <URL> # clone full history
git clone <URL> <DIR> # clone full history and rename directory
git clone --depth=1 <URL> # clone shallow
git fetch --unshallow # fetch all remote branches despite shallow clone
git remote set-branches --add origin dev # allow set-upstream from shallow clone
# tagging
git tag # list available tags
git tag -l v1.4.2.* # search for specific tags
git tag -a v1.4 -m 'version 1.4' # create an annotated tag
git tag -a v1.2 9fceb02 # tag a specific commit (if you forgot)
git show v1.4 # show the tag data of a specific tag
git tag v1.4 # create a lightweight tag
git push --tag # you have to explicitly push tags to remotes
git log --date-order --graph --tags --simplify-by-decoration --pretty=format:'%ai %h %d' # show tags with creation dates
# diff
git diff --word-diff
git diff --staged # show the changes that have been staged
git diff 0c6de32 HEAD # compare the current commit to a previous commit
# bisect
git bisect start # enter bisect mode
git bisect good # mark current commit as bad
git bisect bad # mark the current commit as good
git bisect log # show the good/bad commits
git bisect reset # exit bisect mode and return to the original commit
# reset
git reset <FILE_OR_DIRECTORY> # unstage
git checkout -- <FILE> # throw away local modifications and reset to last committed version of given file
git checkout 0c6de32 # browse a commit in detached HEAD state. git checkout <BRANCH> to reattach
git checkout HEAD~ # checkout previous commit in detached HEAD state
git reset --hard HEAD # throw away local modifications and reset to latest of current branch
git reset --hard 0c6de32 # throw away local modifications and reset to specific commit
git clean -f # remove untracked files
git revert HEAD # Make a commit that undoes the last commit. Used to reset a commit that
# has already been pushed to a remote.
git rm FILE # Remove a file from the index. Useful if it was not gitignored.
git rm -r FOLDER # Remove a folder from the index. Useful if it was not gitignored.
# checkout next (newer) commit
git_next() {
BRANCH=`git show-ref | grep $(git show-ref -s -- HEAD) | sed 's|.*/\(.*\)|\1|' | grep -v HEAD | sort | uniq`
HASH=`git rev-parse $BRANCH`
PREV=`git rev-list --topo-order HEAD..$HASH | tail -1`
git checkout $PREV
}
# create a new repo from a directory in an old repo, preserving history
git clone <old repo>
cd <old repo>
git remote rm origin
git filter-branch --subdirectory-filter <new repo> -- --all
cd <new repo>
curl -u '<username>' https://api.github.com/user/repos -d '{"name":"<new repo>"}'
git remote add origin <new repo>
git push origin master
# display branches sorted by date
git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate) %(authorname) %(refname:short)'
# Other Awesomeness: http://hub.github.com
@derknorton
Copy link

Excellent, thanks for posting this!

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