What is git?

Gir is the open source version control system. You can use git so that you can keep track of your work. You can easily find your changes related to your file and clear history.

How to check git version?

To check the current version of git use following command:

git --version

How to initialize a git repo?

To add git repo in your current project go to your project root folder and run following command:

# initialize git in current directory
git init

# initialize new git repo in a directory specified
git init [directory]

# attach newly initialize git repo to point to remote url
git remote add origin https://github.com/user/repo.git

# verify remote repo
git remote -v

How to clone git repo?

Followings are some of the commands related to git cloning:

# clone remote repo on local using ssh
git clone ssh://doe@example.com/path/to/project.git

# clone git repo to new directory on your local
git clone [repo] [folder]

# clone specific remote branch on local machine
git clone -branch [branch_name] git://remoterepository.git

How to check file status in git?

To list all new or modified files that needs to be committed. Use following command:

git status

How to check file differences in git?

If you made some changes to your file and now you want to know where changes you have made you can use following command:

# checks the file difference before staging
git diff

# checks the file difference after staging
git diff --staged

How to add files to git?

To add a file to git you need to run following command:

# to add single file
git add test.txt

# to add all files
git add *

# stage a directory
git add [directory]

# choose files to stage
git add -p

How to commit git changes?

# first of all add files to stage
git add [file]

# commit changes locally
git commit -m "sample message related to your commit"

# push your changes to server
git push

How to configure git username and password?

To configure your git username and password so that you can see them when you commit your file. Run following commands:

# set git username
git config --global user.name "FirstName LastName"

# set git user email
git config --global user.email "test@email.com"

# set default editor
git config --global core.editor vim

# set an alias for commit command
git config --global alias.ci commit

How to check commit history using git?

To Display the entire commit history using the default format use following command:

# display entire commit history
git log

# The --graph option draws an ASCII graph representing the branch structure 
git log --graph --oneline --decorate

# display user, commit and date in pretty format
git log --pretty=format:"%cn committed %h on %cd"

# display only the 3 most recent commits
git log -3

# shows commits that were created after July 1st, 2014
git log --after="2014-7-1"

# using relative references like "1 week ago" and "yesterday"
get log --after="yesterday"

# commits added between July 1st, 2014 and July 4th, 2014
git log --after="2014-7-1" --before="2014-7-4"

# fetch commits for author
git log --author="John"

# commits by either Doe or John
git log --author="John\|Doe"

# find specific string in commit message
git log --grep="JRA-111:"

# show changes that happened to a particular file
git log -- foo.php

# commits that introduce or remove a particular line of source code
git log -S"Hello, World!"

# list all merges
git log --merges

# hide merges from commits
git log --no-merges

# how far feature has progressed since it forked off of master
git log master..feature

How to play with git branches?

Followings are the lists of commands that are used related to branch operations:

# list all local branches
git branch

# create a new branch from current branch
git branch [new_branch]

# swith to another branch
git checkout [branch_name]

# merge branch into current branch
git merge [branch_name]

# delete local branch
git branch -d [branch_name]

How to temporarily stash changes?

To stash changes temporarily and re-apply them when you needed use following git command:

# stash current changes
git stash

# check the status of changes
git status

# re-apply the same changes you stashed before and remove them from stash list
git stash pop

# you can also apply previous stash without deleting the stash history
git stash apply

# how to stash changes with un staged or untracked files
git stash -u

# list all previous stashes
git stash list

# add message related to your stash
git stash save "message text"

# apply specific stash
git stash pop stash@{2}

# check the summary for stash
git stash show -p

# clear or delete all stashes
git stash clear

How to tag git branch?

Followings are some of the useful commands to deal with git tags:

# create a new tag
git tag [name]

# create a tag with taggers name, email and date
git tag -a [name]

# list all the tags
git tag

# filter specific tags
git tag -l *-rc*

# checkout previous tag
git checkout [name]

# delete a local tag
git tag -d [name]

# push all local tags to remote
git push --tags

# push single tag to remote
git push origin [name]

How to undo changes using git?

Followings are some of the tricks that you can use to undo changes:

# check list of latest commits
git log --oneline

# if you do not want some of the recent commits
# find a commit where your branch was good
git checkout [commit_hash]

# create a new branch to ignore latest commits
# you can now start working on this new branch
# if you want you can delete the other branch
git checkout -b [new_branch]

# if you do not want to follow above steps
# you can revert your changes using reset command
# commit history is reset to that specified commit
git reset --hard [commit_hash]

# commit your changes
git commit -m "Reverting to the state of the project at commit_hash"

# you can also use following command to undo specific commit
git reset [commit_hash]

if you know any good git related commands please mention in the comments I will update the cheat sheet.