Basic setting

  • Set global user information
git config --global user.name "your name"
git config --global user.email "your email"
  • Set user information for current repository
git config user.name "your name"
git config user.email "your email"

Start

  • Create a new repository in current directory:
git init
  • Create a new repository with some directory:
git init <directory>
  • Clone an existing repository:
git clone <path to repository>
  • Add a remote sever:
git remote add origin <server>

Add and remove files

  • Add a file:
git add <file>
  • Add all files in the working directory:
git add -A
  • Remove a file from both the staging index:
git rm --cached <file>
  • Remove a file from both the staging index and the working directory:
git rm <file>
  • Create a commit:
git commit -m "commit message"

Push

  • Synopsis:
git push <remote server> <local branch>:<remote branch>
  • Push to the current branch’s remote:
git push
  • Push the current branch to the configured upstream:
git push origin
  • Push to the master branch:
git push origin master
  • Push to a branch:
git push origin <branch>

Pull

  • Synopsis:
git pull <remote server> <remote branch>:<local branch>
  • Pull from the current branch’s remote:
git pull
  • Pull the current branch from the configured upstream:
git pull origin
  • Pull from master branch:
git pull origin master
  • Pull from a remote branch:
git pull origin <branch>

Branch operations

  • List local branches:
git branch
  • List remote branches:
git branch -r
  • List all (local and remote) branches:
git branch -a
  • Create a new local branch:
git branch <branch>
  • Create a new local branch and switch to it:
git checkout -b <branch>
  • Switch to a local branch:
git checkout <branch>
  • Rename a local branch:
git branch -m <old branch> <new branch>
  • Delete a local branch:
git branch -d <branch>
  • Delete a remote branch:
git push origin --delete <branch>
  • Merge a branch to the current branch:
git merge <branch>

Others

  • Update .gitignore
git rm -r --cached .
git add -A
git commit -m "update .gitignore"
git push origin master
  • Abandon a local change of a file:
git checkout -- <file>
  • Abandon all local changes
git fetch --all
git reset --hard origin/master
git pull

References