Git Cheat Sheet

Submitted by swarut on Sun, 10/21/2012 - 03:37

Never think that I'll have a chance to write down a cheat sheet for Git before, but this is a special note for my lovely student ;).

Initializing Git in Working Folder
git init

Cloning Git Repo
git clone [repository name]

Viewing Status
git status

Viewing Log
git log

Adding File to Staging
git add [file name]

Committing File to Committed State
git commit -m "message"

Removing File
git rm [filename]
This will remove a file from being tracked by Git, and will physically delete it too.
git rm --cahced [file name]
This will remove a file from being tracked by Git, but will not physically delete it.

Renaming A File
git mv [old file name] [new file name]

Ignoring Changes Made to A Particular File
git checkout [file name]

Creating New Branch
git branch [branch name]

List All Branch
git branch
Use -a option to show remote branches.

Removing A Branch
git branch -d [branch name]
Use -D option for force delete.

Switching between Branch
git checkout [branch name]

Switching between Branch (create if target branch did not exist)
git checkout -b [branch name]

Creating and Checking out The Branch with Remote Tracking
git checkout --track -b [branch name]  origin/[branch name]

Showing The Changes Made on A File
git diff [file name]

Merging A Branch
git merge [branch name]
This will merge [branch name] into the current branch.

Rebasing A Branch
git rebase [branch name]
Rebase current branch over [branch name].

Soft Reseting
git reset --soft HEAD~[n]
Step the commit back to for [n] commits while keeping the changes made within [n] commits in staging state.

Normal Reseting
git reset HEAD~[n]
Step the commit back for [n] commits and leave the changes made within [n] commits in unstaging state.

Hard Reseting
git reset --hard HEAD~[n]
Step the commit back for [n] commits and discard all changes made within [n] commits.

Rewriting Recent Commit Message
git commit --amend -m "new commit message"

Stashing The Change
git stash save "message"

Unstashing The Change
git stash pop

Showing Stash List
git stash list

Pushing to Remote
git push origin [remote branch name]

Pulling from Remote Branch And Merge to Local Branch
git pull origin [remote branch name]
This will pull and merge the commits from remote branch to local branch

Pulling from Remote Branch And Rebase to Local Branch
git pull --rebase origin [remote branch name]
This will pull and rebase the commits from remote branch to local branch