[Git] Git Cookbook

Git is a popular distributed version control system. As a professional developer, you have a high chance to use Git as a main tool. Git is actually nothing but a file content tracker. This post summarizes the commands that can be used everyday.

Stages (Areas)

  • Untracked
  • Working Area (Added or Modified)
  •  Index (Staging) Area
  •  Repository (Committed)
  •  Stash


Starting – Creating Local Repositories

Clone from an existing repository

$ git clone https://github.com/user/project-name.git
$ cd project-name
$ git status

From an existing folder of untracked files

$ git init
$ git status
$ git add -all   (add all new and modified files to staging)
$ git commit -m "Init"

From the empty folder

$ git init


Checking Status and Logs


$ git --version

$ git status
$ git status --short (-s)

Logs – Viewing History

$ git log       (view all history)
$ git log -2    (number of commits)
$ git log --oneline   (single line summary)
$ git log --pretty=oneline
$ git log --stat  (file info)
$ git log --graph --decorate --oneline  (easier to see the history)

$ git show _ref  (detailed info)


$ git count-objects
$ git cat-file -p _ref  (shows the content of the /.git/objects)

$ git-blame 1.txt

git-blame” shows what revision and last modified author of the each line of a file.




$ git config --list

Config Levels

$ git config          (repository)
$ git config --global (user)
$ git config --system (system)

Setting Configs

$ git config --global user.name "My Name"
$ git config --global user.email "name@my.com"


Working in different Areas

Moving from Working area to Staging (Add)

$ git add --all
$ git add .
$ git add 1.txt

Moving from Staging to Repository (Commit)

$ git commit -m "msg"
$ git commit --amend -m "fix" (fix the current commit)

Moving from Working area to Repository directly (For modified and deleted, not for new)

$ git commit -a -m "msg"   (--all)

Moving from Staging to Working area (reset HEAD)

$ git reset HEAD .\1.txt
$ git rm --cached .\1.txt

Moving from Working to Trash (discard the change)

$ git checkout -- .\1.txt


Removing and Renaming


$ git rm .\1.txt   (repository -> trash: file is deleted)
$ git status       (deleted)
$ git reset HEAD .\1.txt  (unstage ->  copy Repository to Staging)
$ git checkout -- .\1.txt  (rollback)

$ git rm --cached .\1.txt  (Repository -> Working: file remains)


$ git mv .\1.txt .\2.txt  (staged)




$ git diff          (Working - Staging)
$ git diff --cached (Staging - Repository)
$ git diff --staged (Staging - Repository)

$ git diff _ref1.._ref2
$ git diff head~1..head


Working with Branches


HEAD is a reference to the current branch.

Listing and Checking Branches

$ git branch
$ git branch --list
$ git branch --all (-a)   (including remote)
$ git branch --remote

Creating a New Branch

$ git checkout master       (move to the starting point)

$ git branch branch_name    (create a branch)
$ git checkout branch_name  (move to the branch)

$ git checkout -b branch_name (short form: create + move)

Moving to another Branch

$ git checkout master
$ git checkout branch_name
$ git checkout _ref   (detached HEAD)


$ git checkout master     (move to the base)
$ git merge branch_name   (merge branch to base, new commit is created)

Handling Merge Conflict

-- change the file manually
$ git add
$ git commit -m "merge"



Reset: It moves the current branch and optionally copies date from repository to other areas:

  •  – – soft: Move to #ref only, stating/working remain
  •  – – mixed: Default, Move to #ref and copy it to staging, Working changes remain
  •  – – hard: Move to #ref and copy it to staging and working – changes are gone
$ git reset --soft _ref
$ git reset --mixed _ref
$ git reset _ref
$ git reset --hard _ref

HEAD Reset: If you provide HEAD for #ref, it copies the current repository to other areas

$ git reset HEAD .\1.txt  (mixed: repository -> staging)
$ git reset --hard HEAD   (cleanup all working and staging)
$ git reset --hard HEAD~1 (remove the latest commit)

Note that the “reset –hard” does not work with a path

$ git reset --hard HEAD .\1.txt  (X)

Revert: Revert some existing commits

$ git revert _ref




$ git tag   (list)

$ git tag -a tag_name -m "msg"  (annotation)
$ git tag tag_name -m "msg"     (lightweight)

$ git cat-file -p tag_name



Working with remote repository

$ git fetch remote-name branch-name
$ git merge
$ git pull remote-name branch-name (short form)

$ git push
$ git push remote_name branch_name
$ git push origin master

Adding a remote repository

$ git remote add remote_name url
$ git remote add origin https://github.com/user/project-name.git

Listing remote repositories

$ git remote --verbose (-v)



Change the commit history, flatten branches

$ git rebase master



Temporary storage

$ git stash list

$ git stash
$ git stash --include-untracked

$ git stash apply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s