Git

My Git Cheet Sheet

Posted by Lawrence Sun on 2014-01-09

When I saw the Error on my rails sample_app and couldn’t find the solution the other day, I wanted to abort the changes and intended to go back to the last commit. I searched on stackoverflow and followed the instruction, ran something like
git reset –Hard
Then… I messed up the files and commits, unsurprisingly.

So I decided to learn some basic command of Git apart from
git add .
git commit -m “I need a cupa”
git push

After watching and practing Git command from Git Real on Code School, I generated a basic cheat sheet for myself. Here it is.

  • Create

    1
    2
    3
    4
    # Start a new repo
    $ git init
    # Clone existing repo
    $ git clone ssh://user@domain.com/rego.git
  • Local Change

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # Check what's changed
    $ git status
    # Add all current changes to the next commit (staging area)
    $ git add .
    # Add some files
    $ git add <filename>
    # Add files in /doc directory
    $ git add doc/
    # Add all txt files
    $ git add *.txt
    # Commit previously staged changes
    $ git commit -m "Desperately need coffee"
    # Commit all changes from tracked(not new) files
    $ git commit -a -m "Coffee..."
    # Change the last commit
    $ git commit --amend
    # Show difference
    $ git diff
    # Show staged difference
    # git diff --staged
  • Remote

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # Add a remote
    $ git remote add <remote> <url>
    # Delete a remote
    $ git remote -mv <remote>
    # List all remote repos
    $ git remote -v
    # Push local branch to remote repo, -u to set default
    $ git push -u <remote> <branch>
    # Pull changes down from the remote. Including fetch(sync) and merge.
    $ git pull
  • History

    1
    2
    3
    4
    5
    6
    # Show all commits
    $ git log
    # Show commits with graph
    $ git log --online --graph
    # Show who chaged what
    $ git blame <filename>
  • Undo

    1
    2
    3
    4
    5
    6
    # Discard all the changes since last commit
    $ git checkout -- <filename>
    # Undo last commit and put changes into staging
    $ git reset --soft HEAD^
    # Undo last 2 commits and all changes
    $ git reset --hard HEAD^^
  • Tags and Branches

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    # Mark current commit with a tag
    $ git tag <name>
    # List existing branches
    $ git branch
    # Create a new branch
    $ git branch <new_branch>
    # Create a new branch and switch to it
    $ git checkout -b <new_branch>
    # Switch HEAD branch
    $ git checkout <branch>
    # Check remote branches
    $ git branch -r
    # Delete a local branch
    $ git branch -d <branch>
    # Delete a remote <branch>
    $ git push origin <branch>
    # Clean up deleted remote branches
    $ git remote prune origin
  • Merge and Rebase

    1
    2
    3
    4
    5
    6
    # Merge <branch> into current HEAD
    $ git merge <branch>
    # Fetch(sync) remote but don't merge
    $ git fetch
    # Rebase current HEAD onto <branch>
    $ git rebase <branch>
  • Config

    1
    2
    3
    4
    5
    6
    7
    # Global user name and email
    $ git config --global user.name "name"
    $ git config --global user.email "email"
    # Alias
    $ git config alias.<name> <command>
    # Colour UI
    $ git config --global color.ui true