Git Cheat Sheet

- Git Basics -
Git Cheat Sheet

What`s inside

Handy git snippets often used by most developers on a daily basis.

 

If you are new to Git and want to get started ASAP.  Head over to the Git Basics section.

 

Usage

Each sections have a group of git snippets which are focused for a particular scenario.

 

Define an author name to be used for all commits by the current user

git config –global user.name “[firstname lastname]”

 

Set an email address that will be associated for all commits by the current user

git config –global user.email “[valid-email-address]”

 

Set automatic command line coloring for Git for easy reviewing

git config –global color.ui auto

 

Create a shortcut for a Git command

git config –global alias.[alias-name] [git-command]

 

Open the global configuration file in a text editor for manual editing

git config –global –edit

 

Initialize an existing directory as a Git repository

git init

 

Retrieve an entire repository from a hosted location

git clone [remote-repo-url]

 

Display modified files in the working directory, staged for your next commit

git status  

 

Add a file(s) to the staging area

git add [file1]  [file2]

 

Add all modified files to the staging area

git add *

 

Unstage a file while retaining the changes in the working directory

git reset [file]     

 

Difference of what is changed but not staged

git diff 

 

Commit the staged content as a new commit snapshot

git commit -m “[message]” 

 

Add a git URL as an alis

git remote add [alias] [remote-repo-url]

 

Fetch all the branches from the remote repository

git fetch [alias]

 

Merge a remote branch with the current local branch to bring it up to date

git merge [alias]/[branch-name]

 

Send the local branch commits to the remote repo branch

git push [alias] [branch-name]

 

Fetch and merge any commits from the tracking remote branch

git pull

 

Push all of your local branches to the specified remote

git push [remote-alias] –all

 

Tags aren`t pushed automatically when you push a branch or use the –all flag. The –tags flag sends all your local tags to the remote repo

git push [remote-alias] –tags

 

List all branches

git branch

 

Create a new branch

git branch [branch-name]

 

Switch and checkout the working directory to another branch

git checkout  

 

Merge the specified branch with the current one

git merge [branch-name] 

 

Show all commits in the current branch`s history

git log

 

Show the commit history of the active branch

git log

 

Limit the number of commits to display

git log -[limit]

 

Condense each commit log to a single line

git log –oneline

 

Display the full diff of each commit

git log -p

 

Include the altered files and the relative number of lines added or deleted from each file.

git log –stat

 

Search for commits by a particular author

git log –author=”[pattern]”

 

Display only the commits for a single file

git log –[file]

 

Show commits on branch1 that are not in branch2

git log branch2..branch1

 

Show commits that changed file, even across renames

git log –follow [file]

 

Difference of what is staged but not yet committed

git diff –staged 

 

Draw a text based graph of commits with names of branches or tags of the commits shown

git log –graph –decorate

 

Show the difference of what is in branch2 that is not in branch1

git diff branch2…branch1

 

Show difference between the working directory and the last commit

git diff HEAD

 

Show the difference between the staged changes and the last commit

git diff –cached

 

Save modified and staged changes

git stash

 

List stack-order of stashed file changes

git stash list

 

Write working from top of stash stack

git stash pop

 

Discard the changes from top of stash stack

git stash drop

 

Apply any commits of current branch ahead of the specified one

git rebase [branch-name]

 

Reset the staging area to match the most recent commit, but leave the working directory unchanged

git reset

 

Reset the staging area and the working directory to match the most recent commit and overwrite all changes in the working directory

git reset –hard

 

Move the current branch tip backward to [commit], reset the staging area to match, but leave the working directory alone

git reset [commit]

 

Clear the staging area, rewrite the working tree from specified commit

git reset –hard [commit]

 

Show a log of changes to the local repo`s HEAD. Add –relative=date flag to show date info or –all to show all refs.

git reflog

 

Delete the file from project and stage the removal for commit

git rm [file]

 

Change an existing file path and stage the move

git mv [existing-path] [new-path]

 

Show all commit logs with indication of any paths that moved

git log –stat -M