GIT CHEAT SHEET

Debashish kumar sahoo
3 min readMar 6, 2022

Most of use may be familiar with Git, GitHub. Many of us would have worked with these. But a lot of people has some confusion with these. Let’s clear them all. Here you will get to know about all the git commands that are used generally by everyone.

Git is the free and open source distributed version control system that is responsible for everything GitHub related things that locally happens on your computer. So let’s dive into the list of commands.

Setup

Configuring user information used across all local repositories.

  • git config — -global user.name “[userName]”

Set a name that is identifiable for credit when review version history

  • git config — -global user.email “[userEmail]”

Set an email address that will be associated with each history marker

  • git config — -global color.ui auto

Set automatic command line coloring for Git for easy reviewing

Init

Configuring user information, initializing & cloning repositories.

  • git init

Initialize an existing directory as a Git repository

  • git clone [url of repository]

Retrieve an entire repository through an url

Stage & Snapshot

Working with snapshots and the Git staging area

  • git status

show modified files in working directory, staged for the next commit

  • git add [file/folder name]

add files as it that you want to commit next

  • git reset [file]

unstage a file while retaining changes in working directory

  • git diff

difference of what changed but not staged

  • git diff — staged

difference of what is staged but not committed yet

  • git commit -m [“Message”]

commit your staged content as a new commit snapshot

Branch & Merge

Isolating work in branches, changing context and integrating changes

  • git branch

List all your branches. A * will appear next to the current active branch

  • git branch [branch name]

Create new branch at the current commit

  • git checkout

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

  • git merge [branch]

merge the specified branch’s history into the current one

  • git log

show all commits in the current branch’s history

Inspect & Compare

Examining logs, diffs and object information

  • git log branchB..branchA

show the commit history for the currently active branch

  • git log — follow [file]

show the commits that changed file, even across renames

  • git diff branchA…branchB

show the difference of what is in branchA that is not in branchB

  • git show [SHA]

show any object in Git in human readable format

Tracking Path Changes

Versioning file removes and path changes

  • git rm [fileName]

delete the file from project and stage the removal for commit

  • git mv [existingPath] [newPath]

change an existing file path and stage the move

  • git log — stat -M

show all commit logs with indication of any paths that moved

Share And Update

Retrieving updates from another repository and updating local repos

  • git remote add [alias] [url]

add a git url as an alias

  • git fetch [alias]

fetch down all the branches from that Git remote

  • git merge [alias] / [branch]

merge a remote branch into your current branch to make it up to date

  • git push [alias] [branch]

transmit local branch commits to the remote repository branch

  • git pull

fetch and merge any commits from the tracking remote branch

Rewrite History

Rewriting branches, updating commits and clearing history

  • git rebase [branch]

apply any commits of current branch ahead of specific one

  • git reset — hard [commit]

clear staging area, rewrite working tree from specified commit

Temporary Commits

Temporarily store modified, tracked files in order to change branches

  • git stash

save modified and staged files

  • git stash list

list stack-order of stashed file changes

  • git stash pop

write working from top of stash stack

  • git stash drop

discard the changes from top of stash stack

I have tried to cover the maximum Git commands here. I may have missed few but I hope it will be useful for all of you.

Thank you!

--

--

Debashish kumar sahoo

Senior Software Engineer || Mobile App Dev || iOS || DSA