Git cheat-sheet

This page lists down common Git commands.

Common Commands


Check Git Config

git config --list	

Push to an existing Git repository

git clone git@github.com:sunieldalal/FrontEndSpa.git
cd FrontEndSpa

git remote add origin git@github.com:sunieldalal/FrontEndSpa.git
git push -u origin master

Create and Push to a new repository

mkdir FrontEndSpa
cd FrontEndSpa

git init
touch README.md

git add README.md
git commit -m "first commit"
git remote add origin git@github.com:sunieldalal/FrontEndSpa.git

git push -u origin master

Commit new change to repository

git status	

git add hello.html
git commit -m "First Commit"
	
git push origin master

Pull / update the repository

git pull

Drop all your changes

git fetch origin
git reset --hard origin/master

Create Tag

-- Find the Commit Id
git log
git log --since=2015-01-08 --until=2015-09-10

-- Tag It
git tag 1.0.0 abcdefghijk

-- Check it Out
git checkout 1.0.0

OR

-- If you are planning to tag existing state
git tag -a part1 -m 'Release contains release v1.1'
git checkout part1

-- push the tag to origin
git push origin part1 

-- Get specific tag
git clone -b 'part1' --single-branch --depth 1 https://github.com/sunieldalal/insightapp.git

Create Branch

-- create a new branch "feature1" and switch to it
git checkout -b feature1

-- switch back to master
git checkout master

-- and delete the branch again
git branch -d feature1
	
-- Push the branch to your remote repository so that others can see it
git push origin feature1

Revert last local commit

-- If you do not want to keep your changes in local filesystem

git commit -m "some mistake"
git reset --hard HEAD^

-- If you want to keep your changes in local filesystem

git commit -m "some mistake"
git reset --soft HEAD~

-- Edit the files and add the file again
git add *

-- commit now
git commit -c ORIG_HEAD 
git push

Revert last remote commit

-- Revert to last change
Git revert HEAD
-- switch back to master
git cherry-pick change#

git push

Set username/email id


-- Set username / email id globally
git config --global user.name "Sunil Dalal"
git config --global user.email "sunildalal@youremail.com"

-- Set username / email id locally
git config user.name "Sunil Dalal"
git config user.email "sunildalal@youremail.com"

Set https certificates

openssl pkcs12 -in mycert.p12 -nocerts -out mycert.key -nodes
openssl pkcs12 -in mycert.p12 -clcerts -nokeys -out mycert.crt
openssl pkcs12 -in mycert.p12 -cacerts -nokeys -out cacert.crt
git config --local http.sslCert "/mycert.crt"
git config --local http.sslKey "/mycert.key"
git config --local http.sslCaInfo "/cacert.crt"
git config --local http.sslVerify "false"
git pull git pull 

Other Interesting References


http://ndpsoftware.com/git-cheatsheet.html#loc=workspace http://justinhileman.info/article/git-pretty/git-pretty.png

Version History


Date Description
2015-08-15    Initial Version
2016-03-04    Added “Create Branch commands”