Git command line

Wrote by: hoangnguyen     Create date: 17/04/2012     2188 views

Set up Git Configuration

git config --global "" /* Setup email is used to commit */

git config --global "your user name here" /* Setup username is used to commit */

git config --global core.editor "vi" /* Choose editor used by GIT */

git config --global color.ui true /* Setup color ui for command line */

git config --list /* See Git configuration */


To initialise a local repository 

git init 

Add a file to the repo

git add 


Commit the change to git

git commit -m "Message goes here" 


See the commits

git log 


Basic Commands 

git status  /*  the command 'git status' tells which files are not added or committed from Working to Staging to Repository */

git commit -m "" /*  Commits and changes to all files that are in Staging into Repo  */

git diff /*  show changes between Working and Local Repo, no file supplied shows all files  */

git diff --staged /*  shows changes between Staged and Local Repo  */

git rm file.txt /*  will remove file from working then git commit -m "" to also remove from Repo */

git rm --cached file.txt /* leaves copy of file in Working but removes from Staging and Repo */

git mv /*  rename or move files - then git commit -m "" to move to Repo */

git commit -am "text goes here" /* adds all files straight to Repo from Staging if they have changes - meaning they skip git add */

git checkout -- file.txt /*  restore Repo file to Working Directory using current branch  */

git reset --soft HEAD^ /* restore repo file to staging */

git reset HEAD file.txt /*  Move a Stage file out of Stage back to Working */

git commit --amend -m "message" file.txt /* Change last commit to Repo (only last one can change) */


Resetting & Reverting

/* Reverting --soft --mixed --hard will go back to previous commits*/

git log /* gets the sha1s so you can see the coomits where you want revert  back to  */

git reset --soft sha /* changes Repo but not Staging or Working */

git reset --mixed sha /* changes Repo and Staging but not Working */

git reset --hard sha /* changes all 3 Tiers */

git clean -f /* remove untracked files from Working  */


Ignore file

.gitignore /* ignores files to track in Working / track the .gitignore file */

Global Ignore /* create in home folder  */ 
/* Add in  */

git config --global core.excludesfile ~/.gitignore_global /* add to gitconfig */


Stop tracking changes 

git rm --cached file.txt /* leaves copy in Repo and Working */


Commit log

git ls-tree HEAD
git ls-tree master
git log --oneline
git log --author="Neil"
git log --grep="temp"


Show commit

git show dc094cb /*  show SHA1 */


Commands on branch

git branch /*  Show local branches * is the one we are on */

git branch -r /* Shows remote branches */

git branch -a /* Shows local and remote */

git branch newbranch /* creates a new branch */

git checkout newbranch /* switch to new branch */

git checkout -b oldbranch /* creates and switches to new branch  */

git push origin newbranch /* Push new branch to remote */

/* Diff in Branches */

git diff master..otherbranch /*  shows diff */

git diff --color-words master..otherbranch /*  shows diff in color */

git branch --merged /*  shows any merged branches */

/* Rename Branch */

git branch -m oldname newname

/* Delete  Branch */

git branch -d nameofbranch

/* Merge Branch  */

git merge branchname /* be on the receiver branch to merge the other branch */

/* Merge Conflicts between the same file on 2 branches are marked in HEAD and other branch */

git merge --abort /*  Abort basically cancels the merge */


 Manually Fix Files and commit - The Stash

git stash save "text message here"

git stash list /* shows whats in stash */

git stash show -p stash@{0} /* Show the diff in the stash */

git stash pop stash@{0} /*  restores the stash deletes the tash */

git stash apply stash@{0} /*  restores the stash and keeps the stash */

git stash clear /*  removes all stash */

git stash drop stash@{0}


Remotes commands

git remote add origin /*  origin can be named whateve followed by the remote */

git remote /* to show all remotes */

git remote show origin /*to see remote URL*/

git remote remove origin /* to remove remote */

git remote rm origin /* to remove remote */


Clone project. Push from local to remote

/* Cloning a GitHub Repo - create and get the URL of a new repository from GitHub, then clone that to your local repo, example below uses local repo named 'nameoffolder' */

git clone nameoffolder

git push -u origin master /* push to remote(origin) and branch(master)

/* Push to Remote from Local - more - since when we pushed the local to remote we used -u parameter then the remote branch is tracked to the local branch and we just need to use... */

git push

git push origin newbranch /* Push a branch to a remote */

Fetch changes from a cloned Repo

git fetch origin /*  Pulls down latest committs from remote origin/master not origin, also pull down any branches pushed to Repo
Fetch before you work
Fetch before you pull
Fetch often */


Merge with origin/master 

git merge origin/master


Fetch + Merge data ==> Pull

git pull /* you can also do git pull which is = git fetch + git merge
Checkout/Copy a remote branch to local */


Get remote branch

git branch branchname origin/branchname /*  this will bring the remote branch to local and track with the remote */


Delete branch

git branch -d branchname


Checkout and switch branch and track to remote 

git checkout -b nontracking origin/nontracking


Remove remote branch

git push origin --delete branch


Undoing changes

git checkout path-to-file /*restores a file before it is staged */

git reset HEAD path-to-file /*if it is staged - restores a file from last commit and then git checkout path-to-file */

git checkout HEAD^ path-to-file /*if is staged and committed - restores from last commit */

git reset --hard HEAD^ /*restore prior commit */



git tag -a v1.0.0 -m "add message here" /*tagging a commit with a version number*/

git push --tags /* pushes tag info to master remote */

/*You can checkout a commit and add a tag to that commit by checking out its SHA */

git checkout f1f4a3d /*checking out a commit - see the commit SHAS by git log */


Nguyễn Ngọc Hoàng - Software Engineer
Ngoc Hoang Nguyen

If we cannot do anything, due to we haven't yet acquainted with it.

Go on will also reach to destination.

Follow me

Please wait