Everybody knows git, yes! However, knowing the name of the thing and knowing the thing is completely different aspects. So, in this post first I’ll note the most basic commands and then more detailed ones. Bear in mind that, this cheatsheet completely written for a reminder purpose therefore it covers all commands even veeeery simple ones. Let’s get started.
There are 4 different phase/directory/stage for git lifecyle. Below there is a diagram to understand visually. Thinking each stage as a directory make it easier to understand. Each stage has unique set of commands to pass differences to another stage.
- workspace (local computer)
- staging area (index)
- local repository (HEAD)
- remote repository (HEAD)
HEAD is a pointer that points to latest commit

Getting manaul for commands
1git help <command>
Adding file to staging area
1git add <file>
Remove file from staging area
1git rm --staged <file> #removes from staging area2git reset HEAD <file> #as default mixed
Making a snapshot for file changes
1git commit -m "commit message"2git commit -am "commit message" #add staging and commit
- Commit message should start with issue number, like A-12345
- Should be present tense and be like a title for to-do
- Long description should be added to commit message after a new line break
There is a whole specification just for the how a commit message should be.
Checking for git logs
1git log2git log --author="Muhammet Uçan"3git log --oneline #gives one line of logs4git log --format=oneline #gives full SHA5git log --oneline -x #gives one line of logs for x commits6git log --since="yyyy-MM-dd" --until="yyyy-MM-dd"7git log --since="2 days ago"8git log --since="3.weeks"9git log --grep="regex"10git log <SHA>..<SHA> <file> #comparing commits11git log --graph #graph of commits and branchs12git log --oneline --graph --all --decorate
Blame for changes
1git blame <file>
Looking for details of commit
1git show <SHA>
Showing status of repository and working directory
1git status
Showing differences
1git diff #between workspace and staging area2git diff --staged #between staging and local repository3git diff <SHA> #between workspace and local repository
Changing working directory either to a branch or previous version of file
1git checkout <branch> #changes working directory to branch2git checkout --<file> #revert back to prev version of file3git checkout <SHA> --<file> #take file from specific snapshot, this action puts file to staging area
Only latest commit can be changed or removed
1git commit --amend -m "commit message"
Making opposite of commit
1git revert <SHA>
Changing pointer of HEAD (repository)
1git reset soft #only changes pointer of HEAD2git reset mixed #default, changes HEAD and staging area, working dir stays same3git reset hard #copy all files from repository to working and staging dir
Ignoring files
- add .gitignore to ignore files
- *.js ignores all files with js suffix
- !index.js excludes file from ignoring
- directory/subdir/ ignores all the file in directory
- Ignore compiled code, compressed file, logs and dbs, OS generated file, user uploaded assets
- Tracked files will not be ignored until git stopped tracking, in order to do that delete from staging area
Parent commit
1HEAD^ #parent of HEAD2master^3HEAD~x #x number of parent of HEAD4HEAD^^ #grandparent
Listing tree of repository
1git ls-tree <HEAD/master/SHA> <dir/file>
Branches
1git branch #list of branches2git branch <branch> #create new branch3git checkout <branch> #change branch, HEAD points to branch4git checkout -b <branch> #create and checkout5git branch -d <branch> #delete branch
- Before making checkout for branch make sure all the changes either stashed or commited
Merging
1git merge <branch> #merge branch to master, before doing that checkout to master2git merge --no-ff <branch> #dont make fast forward merge3git merge --ff-only <branch> #make merge iff fast-forward merge possible
- Fast forward merges happen when there is no new commit, it places commit from branch to master branch
Aborting merge
1git merge --abort
Resolving Conflicts
- In order to resolve merge conflicts, manually solve problem and commit from master branch.
Tips for resolving merge
- keep line codes short
- commit often
- do not edit whitespaces frequently
- merge whenever possible
Stashing
- Use when changing branches and not want to commit changes
- Adds tracked files in working directory
- Not associated with any directory
- Can stay even if branch checked out to another branch
- Does not have a SHA value
1git stash save "stash message" #puts to stash2git stash list #list all items in stash3git stash show stash{x} #shows files in stash with number x4git stash show -p stash{x} #shows diffs in stash with number x5git stash pop stash{x} #removes from stash and puts into working dir, default pops first stash6git stash apply stash{x} #not removes from stash and puts into working dir7git stash drop stash{x} #delete stash without applying8git stash clear #delete all stashes at once
Remote repository
When new changes made in local repository in order to update remote repository push
command is used. Also, in local machine a pointer called origin/master
tries to sync with remote repository.

When someone made a change in remote repository, in order to sync with local origin/master
and remote master
, fetch
command is used. After that, merge
command is used in order to merge origin/master
and master
on local. Or, pull
command can be used as a combination of fetch and merge.

1git pull = git fetch + git merge
In above representation it seems that local repository has different branch for origin/master and master, however they are just pointers that points to the same commit.
Tips for fetch
- Fetch before work
- Fetch before push
- Fetch before merge
- Fetch often
Pushing changes to remote repository
1git push origin master2git push #since branch is tracked
Pushing to updated remote repository
1git fetch2git merge #or git pull as combo3git push
Deleting a remote branch
1git push origin --delete <branch>
In this post, I tried to summarize basic structure of a git and list some useful commands. Even though in general usage git seems to be not very complicated, in some scenarios it requires deep understanding what is going on. Also using well developed IDE obstructs having familiarity with git commands. So it would be logical to occasionally repeat these commands.