Navigate back to the homepage

Git cheatsheet

Muhammet Ucan
June 2nd, 2019 · 2 min read

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.

  1. workspace (local computer)
  2. staging area (index)
  3. local repository (HEAD)
  4. remote repository (HEAD)

HEAD is a pointer that points to latest commit

Git summary

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 area
2git 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 log
2git log --author="Muhammet Uçan"
3git log --oneline #gives one line of logs
4git log --format=oneline #gives full SHA
5git log --oneline -x #gives one line of logs for x commits
6git 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 commits
11git log --graph #graph of commits and branchs
12git 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 area
2git diff --staged #between staging and local repository
3git 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 branch
2git checkout --<file> #revert back to prev version of file
3git 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 HEAD
2git reset mixed #default, changes HEAD and staging area, working dir stays same
3git 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 HEAD
2master^
3HEAD~x #x number of parent of HEAD
4HEAD^^ #grandparent

Listing tree of repository

1git ls-tree <HEAD/master/SHA> <dir/file>

Branches

1git branch #list of branches
2git branch <branch> #create new branch
3git checkout <branch> #change branch, HEAD points to branch
4git checkout -b <branch> #create and checkout
5git 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 master
2git merge --no-ff <branch> #dont make fast forward merge
3git 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 stash
2git stash list #list all items in stash
3git stash show stash{x} #shows files in stash with number x
4git stash show -p stash{x} #shows diffs in stash with number x
5git stash pop stash{x} #removes from stash and puts into working dir, default pops first stash
6git stash apply stash{x} #not removes from stash and puts into working dir
7git stash drop stash{x} #delete stash without applying
8git 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.

Git push

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.

Git pull
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 master
2git push #since branch is tracked

Pushing to updated remote repository

1git fetch
2git merge #or git pull as combo
3git 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.

More articles from Blog

My way of creating blog

Simple yet flexible way of writing blog using GitHub Pages and Netlify CMS

June 26th, 2021 · 2 min read

Cost of not doing is bigger than cost of doing

Everything resembles software for me, a real world analogy for a software issue

June 14th, 2021 · 3 min read
© 2019–2021 Blog
Link to $https://github.com/mhmmtucan/Link to $https://www.linkedin.com/in/muhammetucan/Link to $https://instagram.com/mhmmtucanLink to $https://twitter.com/mhmmtucan