# checkout a local copy of a remote branch and switch to it
git checkout -b feature/branch-name origin/feature/branch-name
# set the url of a remote
git remote set-url origin http://user@remote-url:port/path/repo.git
# push to a remote and update the remote tracking branch
git push -u origin <branch name>
# checkout a github pull request locally (pull request #1234 into local branch pr-1234)
# see https://blog.scottlowe.org/2015/09/04/checking-out-github-pull-requests-locally/
git fetch origin pull/1234/head:pr-1234
git checkout pr-1234
# pull updates to github pull request into local branch pr-1234
git pull origin pull/1234/head:pr-1234
# show changes to a file (-p patch)
git log -p filename
# reset a file to last commit
git checkout path/to/file
# clean up working copy (reset changes to all files (f) and directories (d))
git clean -fd
# reset a branch to a commit (delete commits after that)
git reset --hard <sha>
# prune stale remote tracking branches
git remote prune origin
# delete local branch
git branch -d <branch name>
# delete local branch irrespective of merged status
git branch -D <branch name>
# delete remote branch
git push origin --delete <branch name>
# delete local branches with no remote tracking branch
# see http://erikaybar.name/git-deleting-old-local-branches/
git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs --no-run-if-empty git branch -d # or -D to force
# create an annotated tag**
git tag -a v1.2 -m "Version 1.2"
# push the tag
git push v1.2
# push all tags
git push origin --tags
# create a tag on an old commit with the date of the commit
git checkout XXXX
GIT_COMMITTER_DATE="$(git show --format=%aD | head -1)" git tag -a v1.2 -m "Version 1.2"
# squash commits on a branch
git checkout -b testbranch
# make many commits
git reset --soft master
# changes from those commits are staged
git add .
git commit -m 'One commit.'
# drop the most recent stash
git stash drop
# drop the nth stash
git stash drop stash@{n}
# drop all stashes
git stash clear