/git-use-cases

my documentation of useful git commands while I am learning

Git Cheat Sheet

This is written to teach myself Git and to be a quick reference of useful Git commands.

Table of Contents

Initialisation

Start with a new Git project

$ git init

Start with an existing Git project

$ git clone [url]

Configuration

Set username and email

$ git config --global user.name [name]
$ git config --global user.email [email]

Enable colour

$ git config --global color.ui auto

List all configs

$ git config --list

Work cases

Initialise repository

To start with an empty repository

$ cd [repository]
$ git init
$ git add .
$ git commit -m "Inital commit"

To start with a remote repository

$ cd [repository]
$ git clone [remoteUrl]

Setting up Git aliases for Linux style terminal

If there is no .bash_profile file under ~, create one. Open it with a text editor and enter the following.

# Setting Git commands aliases
alias gt='gitk --all &'
alias gu='git gui &'
alias gs='git status'
alias gf='git fetch'
alias gb='git branch'
alias gc='git checkout'
alias gp='git pull --rebase'
alias ga='git add'
alias grb='git rebase'
alias gps='git push'

Working commands

To get current status of the repository

$ git status

Branches

Display local branches and the current branch is starred

$ git branch

Display all branches, especially to see the branch origin/master

$ git branch -a

Display remote branches

$ git branch -r

Add a new branch

$ git branch [newBranchName]

Delete a branch

$ git branch -d [branchName]

Checkouts

Change to a commit version

$ git checkout [commitId]

Change to a branch

$ git checkout [branchName]

Create and change to a branch

$ git checkout -b [newBranchName]

Remotes

Display remotes

$ git remote

Display remotes with more information

$ git remote -v

Add remote

$ git remote add [remoteName] [url]

Note: Useful remote names: origin or upstream Update changes from local to remote

$ git push [toBranch] [fromBranch]

Update changes from remote to local

$ git pull [fromBranch] [toBranch]

Update changes from remote to local remote branch

$ git fetch [fromBranch]

Merges

Merge two branches (branch1 and branch2) into current branch

$ git merge [branch1] [branch2]

Note 1: If having conflicts, resolve, add and commit

Note 2: Fast-forward merge happens when the branch to merge into is an ancestor of the other

Abort a merge

$ git merge --abort

Staging

Add all current changes to staging area

$ git add .

Add a file to staging area

$ git add [file]

Removes

Remove a file from Git repository and file system

$ git rm [file]

Remove a directotry from Git repository and file system

$ git rm -r [directory]

Remove a file from Git repository but not file system

$ git rm --cached [file]

Remove a directotry from Git repository but not file system

$ git rm --cached -r [directory]

Commits

Commit staged changes with commit message

$ git commit -m "message"

Commit and add local changes in tracked files wiht commit message

$ git commit -am "message"

Logs

Display a log of commits

$ git log

Display a log of commits with more information

$ git log --stat

Display commit tree

$ git log --graph --oneline [branch1] [branch2]

Display a fixed number of commits

$ git log -n [number]

Display a log of commits for branch specified

$ git log [branch]

Show information about a commit

$ git show [commitId]

Stash

Stash modified stracked files temporarily

$ git stash

Restore the most recently stashed files

$ git stash pop

Revert

Revert a commit

$ git revert [commitId]