Git Cheatsheet

  • #git

Repository

Create

cd my_repo
git init

Commit

Diff

Show diff.

git diff 
# (q)uit

Show diff for staged changes.

git diff --staged

Stage

Stage all.

git add .

Stage file or files within directory my_dir.

git add my_dir

Stage hunks interactively.

git add -p 
# (y)es or (n)o to each hunk

Commit

git commit

Append changes to last commit.

git commit --amend

Branches

Create

git checkout -b my_branch

Checkout

git checkout my_branch

List

git branch -a

Remove

git branch -D my_branch

Move

Move my_branch to to_branch.

git branch -f my_branch to_branch

Merge

Merge my_branch into master.

merge

git checkout master
git merge my_branch

rebase

Replay master and my_branch commits then merge.

git checkout my_branch
git rebase master

git checkout master
git merge my_branch

Undo

Before push

reset undoes commits up to a certain point by modifying history.

Undo last commit.

git reset --mixed HEAD~1

Undo until x.

git reset --mixed x

After push

revert undoes commits as a new commit.

Undo last commit.

git revert HEAD

Undo multiple commits at x and y.

git revert --no-commit x y

Undo range of commits from x to y, where x is earlier than y.

git revert --no-commit x...y

Restore file

Restore my_file to x.

git reset x my_file