Skip to content

tsukiy0's blog

Git Cheatsheet

April 14, 2018

Basics

  • Create repository

    mkdir repo
    cd repo
    git init
  • Clone repository

    git clone git@github.com:user/repo.git repo
    cd repo
  • Commit

    git add .
    git commit
    # we can stage globs
    git add some_dir/**/*.js

Branching

  • Create

    # create
    git branch feature
    # checkout
    git checkout feature
    git checkout -b feature
  • Delete

    git branch -D feature
  • Move

    # checkout any branch except the one being moved
    git checkout feature2
    # move `feature` to `feature2`
    git branch -f feature feature2
  • Forgot to create a branch

    # create new branch
    git checkout -b feature
    # move `master` back to `origin/master`
    git branch -f master origin/master

Merge

git checkout master
git merge feature

Rebase

  1. Replay feature commits on master

    git checkout feature
    git rebase master
  2. Merge

    git checkout master
    git merge feature

Undo

Before push

reset modifies history.

# undo last commit
git reset --mixed HEAD~1
# undo up to and excluding `x`
git reset --mixed x

After push

revert creates a new undo commit and does not modify history.

Undo last commit.

# undo last commit
git revert HEAD
# undo x and y
git revert --no-commit x y
# undo commits between and including x and y
git revert --no-commit x..y

Restore file

git checkout x -- file

History

  • Search commits

    git log --grep x
  • Search diffs

    git log -S x

tsukiy0