Skip to content

ashwanikumar04/git-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

This document lists some of the common commands used for Git.

Cheat Sheet

Commands

History

Detailed

git log

Short

git shortlog
git log --oneline --no-merges

Pretty

git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
git lg

Clone

Https

git clone https://github.com/ashwanikumar04/git-cheatsheet.git

SSH

git clone [email protected]:ashwanikumar04/git-cheatsheet.git

Single branch

Sometimes when the repository is very large, we only want to clone a single branch. Use below command to clone a single branch

git clone [email protected]:ashwanikumar04/git-cheatsheet.git -b master --single-branch

Config

The following command makes the output of git commands colorful.

git config --global color.ui true

Local

git config user.name "Ashwani Kumar"
git config user.email "[email protected]"

Global

git config --global user.name "Ashwani Kumar"
git config --global user.email "[email protected]"

Alias

git config --global alias.st status

Just type git st whenever we want to see the status of the repository

git config --global alias.co checkout

Just type git co whenever we want to check out a branch

git config --global alias.st status

Just type git ci whenever we want run a commit

Status

git status Shows the status with tracked and un-tracked files

Add

git add . Adds all the files in the current directory to staging.

git add -A Adds all files in the current repository (even new files that are not yet tracked)

git add -u Add all files that are already being tracked (ignore new files)

Commit

Undo

git reset --soft HEAD^ Undo last commit of entire repo, but leave files staged.

git reset --hard HEAD^ Completely blow away last commit. Changes files to state of previous commit.

git reset --hard HEAD^^ Completely blow away last two commits. Changes files to state of previous commit.

git reset --hard HEAD^^^ Completely blow away last three commits. Changes files to state prior to last third commit.

Returns files to state they were in, after specified commit

git reset --hard <sha-of-commit>

git push origin HEAD --force

Commit

git commit -m "Add file to repository" Commit staged files to the repository

git commit --amend -m "New Message" Changes the commit message for the last commit

git commit -am "New Message" Lets us add and commit all tracked, modified files in one step.

Checkout

git checkout -b <name_of_branch> Create a branch and check it out in one step

git branch <name_of_branch> Create a branch, but stay in current branch.

git checkout <name_of_branch> Check out an already created branch

git branch See a list of all branches, highlights the currently checked out branch

git checkout master Checkout master branch

git branch -d <name_of_branch> Remove branch locally, but only if we have merged branch.

git branch -D <name_of_branch> Remove branch even if we haven't merged changes.

Stash

git stash Use this to stash all the changes to the current repository

git stash apply Use this to apply the last stash

Push

git push --set-upstream origin <branch> git push

Pull

git pull origin/master

References

  1. Treehouse
  2. CheatSheet