Show Menu
Cheatography

Git command Cheat Sheet by

Frequently used git command cheat sheet

Git - Config

add user name
git config [--global] user.name "­[na­me]­"
add user email
git config [--global] user.email "­[email addres­s]"
show the git config info
git config --list

Git - Create a new repository

Init­ialize the current dir to be reposi­tory
cd <pr­oje­ct_­dir>
git init
touch README
Cloned from remote reposi­tory
git clone <ur­l>

Git - Add and Remove file

Show the current status in the workspace and index
git status
Add file from workspace into index
git add <fi­le1> <fi­le2> ...
Add all files from current dir into index
git add .
Add all tracked file and untracked file from the workspace into index
git add -A .
Delete tracked file in the index
git rm <fi­le1> <fi­le2> ...
Move tracked file in the index into workspace and untrack it
git rm --cached <fi­le1> <fi­le2> ...

Git - Commit

Commit all files in the index into local reposi­tory
git commit -m "­commit messag­e"
Recommit the previous commit with new changed file and commit message
git commit --amend <fi­le1> <fi­le2> ... -m "new messag­e"

Git - Tag

Show all tags
git tag
Create a new tag at current commit object
git tag -a <ta­g_f­lag> -m "tag comment messag­e"
Create a new tag at specified commit object
git tag -a <ta­g_f­lag> <co­mmi­t_i­d> -m "tag comment messag­e"
Show specified tag info in detail
git show <ta­g_f­lag>
 

Git - Undo modifies

Discard tracked files's modifi­cation in the worksp­ace
git checkout -- <fi­le> ...
Move the added file from index into worksp­ace
git reset HEAD <fi­le>...
Reset code with --soft para, changes in the index and workspace are reserved
git reset --soft <co­mmi­t-i­d>
Reset code with --mixed para, changes in the index are discar­ded­,ho­wever changes in the workspace are reserved
git reset --mixed <co­mmi­t-i­d>
Reset code with --hard para,the changes in the index and workspace are discar­ded
git reset --hard <co­mmi­t-i­d>
Revert code and create a new commit object for this operat­ion.In this way, it will not overlap the commit history.
git revert <co­mmi­t-i­d>
Undo the previous commit changes and create a new commit object for this operat­ion
git revert HEAD~1

Git - Branch

List all branches in the local reposi­tory
git branch
List all branches in the local repository and remote reposi­tory
git branch -a
List all local branch info in detail
git branch -vv
Create a new branch
git branch <br­anc­h-n­ame>
Create a new branch with specified commit
git branch <br­anc­h-n­ame> <co­mmi­t-i­d>
Checkout to another branch
git checkout <br­anc­h-n­ame>
Delete branch in local reposi­tory
git branch -d <br­anc­h-n­ame>
Build tracking relati­onship between local-­branch and remote­-br­anch
git branch --set-­ups­tre­am-­to=­ori­gin­/<r­emo­te-­bra­nch­-na­me> <lo­cal­-br­anc­h-n­ame>
Cancle the tracking relati­onship between local-­branch and remote­-br­anch
git branch --unse­t-u­pstream <lo­cal­-br­anc­h-n­ame>
List the changed files between two branches
git diff <br­anc­h1> <br­anc­h2> --stat
 

Git - History

Show all commit log
git log
Show all commit log in shot format
git log --pret­ty=­short
One line show one commit log
git log --pret­ty=­oneline
Show all changed files in every commit log
git log --stat
Show the change history of specified file
git log --follow <fi­le-­nam­e>
Show all diff info between two adjacent commit
git log -p
Show who changed file
git blame <fi­le-­nam­e>
Show the recently commit log and branch checkout log
git reflog

Git - Remote Push And Update

List all remote repo info
git remote -v
Show specified remote repo info
git remote show <re­mot­e>
Add remote repo and named
git remote add <re­mot­e> <ur­l>
Fetch all changed in the remote repo
git fetch <re­mot­e>
Fetch + Merge
git pull <re­mot­e> [remot­e-b­ran­ch]­:[l­oca­l-b­ranch]
Push local repo changes to remote repo
git push <re­mot­e> [local­-br­anc­h]:­[re­mot­e-b­ranch]
Delete remote branch in remote repo
git push <re­mot­e> :[remo­te-­branch]
Set -u parameter and build the tracking relati­onship between local repo and remote repo
git push -u <re­mot­e> [local­-br­anc­h]:­[re­mot­e-b­ranch]

Git - Merge/­Rebase

Merge branch into current branch
git merge <br­anc­h-n­ame>
Rebase into branch
git rebase <br­anc­h-n­ame>
Rebase Frequently Usage
git checkout <fe­atu­re-­bra­nch>
git rebase master
git checkout master
git merge <fe­atu­re-­bra­nch>
Abort Rebase
git rebase --abort
The Golden Rule of Rebasing
Don't rebasing master branch into your personal feature branch
Conf­licts against base file
git diff --base <fi­le-­nam­e>
       

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          screen Cheat Sheet
          rsync Cheat Sheet
          bash Shortcuts Cheat Sheet