Intro

Staging

Show unstaged differences since last commit

git diff

Options

--staged includes staged changes

Remove all changes on file since last commit

git checkout -- <file name>

Undo last commit

git reset --soft HEAD^

Options

--soft put changes into staging
--hard erase all changes

Add file to last commit:

git add <file>
git commit --amend

Options

-m "<message>" overwrites the previous commit message

Remove untracked files

git clean -fd

Remote

Show all remotes

git remote -v

Add and remove a remote

git remote add <remote name> <url>
git remote remove <remote name>

Set a different url to a existing remote

git remote set-url <remote name> <url>

Lists all remote branches and gives information

git remote show origin

Delete a remote branch:

git push <remote name> :<branch name> # Note the colon!

Remove deleted remote branches from local

git remote prune <remote name>

Reset local branch to remote

git reset --hard origin/branch-name

Tagging

List tags

git tag

Add tag

git tag -a <tag name> -m <tag message>
git push --tags

Rebase

Interactive with the last 3 commits, from oldest to newest:

git rebase -i HEAD~3

Commands

pick puts commit in temporary area while rebasing.
reword change a commit message.
edit this will stop the rebase and put you into staging at the point of that commit.
squash that commit into the previous one, when rebasing change the commit message for both of the commits.

Stashing

git stash list
git stash save <message> # save and message optional
git stash apply <stash name> # stash@{0} is default if stash name is not specified
git stash drop <stash name> # stash@{0} is default if stash name is not specified
git stash pop # does apply + drop
git stash clear # deletes all stashes

Options

--keep-index stash only unstaged files.
--include-untracked stash untracked files too.

Create a new branch with the contents of the stash

git stash branch <new branch name> <stash>
# stash is stash@{0} for example

Rewriting history

git filter-branch --tree-filter <command>

# Examples
git ... --tree-filter 'rm -f passwords.txt' -- --all # all commits in all branches
git ... --tree-filter 'find . -name "*.mp4" -exec rm{} \;' HEAD # only current branch

To run against each commit without checking it out first (faster):

git filter-branch --index-filter 'git rm --cached --ignore-unmatch password.txt'

Delete all commits without contents:

git filter-branch -f --prune-empty -- --all

Cherry-pick

git log # find out the commit id
git checkout <branch name>
git cherry-pick <commit id>
git cherry-pick <initial commit id>..<final commit id>

Options

Reflog

Deleted commit

git reflog # find the commit id
git reset --hard <commit id> # or commit name, eg HEAD@{0}

Deleted branch

git log --walk-reflogs
git branch <branch name> <commit id>

Reverting

Revert a single file

git checkout [commit ID] -- path/to/file

where commit ID is the version of the file you want to reverto to.

Commit the change.