Git Cheat Sheet

All in one cheat sheet to master git commands

Git Cheat Sheet

Git setup

Configuring the user information that will be utilized across all local repositories.

git config --global user.name “[firstname lastname]”
set the name that will be associated with your commits and tags.

Example:

   git config --global user.name “Indratej.dev”


git config --global user.email “[valid-email]”
set the email address that will be associated with your commits and tags.

Example:

git config --global user.email "IndratejReddy@developer.com


git config --global color.ui auto
set automatic command line coloring for Git for easy reviewing

Example: git config --global color.ui auto

Start project

configuring user information, initializing and cloning repositories.

git init
create empty Git repo in specified directory. Run with no arguments to initialize an existing directory as a Git repository

Example:

  git init indratej_Devfile


git clone
clone repo located at onto local machine. Original repo can be located on the local filesystem or on a remote machine via HTTP or SSH.

Example:

   git clone https://github.com/techlifejournal/techlifejournal.git


git status
list which files are staged, unstaged, and untracked.
list new or modified files which are not yet committed.

Example:

   git clone https://github.com/techlifejournal/techlifejournal.git


git add [directory]
stage all changes in for the next commit. Replace with a to change a specific file

Example:

   git add  “Indratej.dev”


git reset [file]
unstage a file while retaining the changes in working directory

Example:

   git reset [./yourfilename]


git diff
diff of what is changed but not staged

Example:

    git diff


git diff --staged
diff of what is staged but not yet commited

Example:

   git diff --staged


git commit -m “[descriptive message]”

  • adds a descriptive message to your commits.*

Example:

   git commit -m “[descriptive message]”


BRANCH & MERGE

isolating work in branches, changing context, and integrating changes.

git branch
list your branches. a will appear next to the currently active branch*

Example:

   git branch


git branch [branch-name]
create a new branch at the current commit

Example:

git branch [branch-name]


git checkout
switch to another branch and check it out into your working directory

Example:

git checkout


git merge [branch]
merge the specified branch’s history into the current one

Example:

git merge [branch]


git log
show all commits in the current branch’s history

Example:

git log


INSPECT & COMPARE

Examining logs, diffs and object information.

git log
show the commit history for the currently active branch

Example:

  git log


git log branchB..branchA
show the commits on branchA that are not on branchB

Example:

   git log branchB..branchA


git show [SHA]
show any object in Git in human-readable format

Example:

  git show [SHA]


SHARE & UPDATE

Retrieving updates from another repository and updating local repos

git remote add [alias] [url]
add a git URL as an alias

Example:

  git remote add [alias] [url]


git fetch [alias]
fetch down all the branches from that Git remote

Example:

   git fetch [alias]


git merge [alias]/[branch]
merge a remote branch into your current branch to bring it up to date

Example:

  git merge [alias]/[branch]


git push [alias] [branch]
Transmit local branch commits to the remote repository branch

Example:

  git push [alias] [branch]


git pull
fetch and merge any commits from the tracking remote branch

Example:

  git pull


TRACKING PATH CHANGES

Versioning file removes and path changes

git rm [file]
delete the file from project and stage the removal for commit

Example:

    git rm [file]


git mv [existing-path] [new-path]
change an existing file path and stage the move

Example:

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


git log --stat -M
show all commit logs with indication of any paths that moved

Example:

  git log --stat -M


REWRITE HISTORY

Rewriting branches, updating commits and clearing history

git rebase [branch]
apply any commits of current branch ahead of specified one

Example:

git rebase [branch]


git reset --hard [commit]
clear staging area, rewrite working tree from specified commit

Example:

   git reset --hard [commit]


IGNORING PATTERNS

Preventing unintentional staging or commiting of files

logs/ .notes pattern/
Save a file with desired paterns as .gitignore with either direct string matches or wildcard globs.

Example:

logs/
*.notes
pattern*/


git config --global core.excludesfile [file]
system wide ignore patern for all local repositories

Example:

   git config --global core.excludesfile [file]


TEMPORARY COMMITS

Temporarily store modified, tracked files in order to change branches

git stash
Save modified and staged changes

Example:

  git stash


git stash list
list stack-order of stashed file changes

Example:

  git stash list


git stash pop
write working from top of stash stack

Example:

  git stash pop


git stash drop
discard the changes from top of stash stack

Example:

  git stash drop