Git Cheat Sheet, Tips and Best Practices

Even though there are many different Git visual tools available for us to use it is not always that we have a visual tool available and have to open a terminal window to use Git.

Unfortunately, the longer we use Git visual tool, the more there are chances we forget how to use some of Git commands and their command line options and switches.

On this page I will list all of the most commonly used Git commands that I use myself and will refer to this page whenever I am not very sure how to use one of them :).

Starting up with Git

Clone an existing repository

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git clone ssh://user@domain.com/repo.git
git clone ssh://user@domain.com/repo.git
git clone ssh://user@domain.com/repo.git

Create new local repository

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git init
git init
git init

Working with Branches

List all branches (both local and remote)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git branch -a
git branch -a
git branch -a

List all branches (both local and remote)
Also, shows the last commit message and if it is forward or behind

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git branch -av
git branch -av
git branch -av

List local branches and which remotes they track

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git branch -vv
git branch -vv
git branch -vv

Create a new branch based on current HEAD

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git branch <branch-name>
git branch <branch-name>
git branch <branch-name>

Create a new tracking branch based on a remote branch
A ‘tracking branch‘ in Git is a local branch that is connected to a remote branch. When you push and pull on that branch, it automatically pushes and pulls to the remote branch that it is connected with.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout --track <remote/branch>
git checkout --track <remote/branch>
git checkout --track <remote/branch>

Switch to a different branch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout <branch-name>
git checkout <branch-name>
git checkout <branch-name>

Rename current branch 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git branch -m <new-branch-name>
git branch -m <new-branch-name>
git branch -m <new-branch-name>

Delete local branch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git branch -d <branch-name>
git branch -d <branch-name>
git branch -d <branch-name>

Delete remote branch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git branch -dr <remote/branch>
git branch -dr <remote/branch>
git branch -dr <remote/branch>

or you can also do:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git push origin :<branch-name>
git push origin :<branch-name>
git push origin :<branch-name>

or you can also delete remote branch this way:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git push origin --delete <branch-name>
git push origin --delete <branch-name>
git push origin --delete <branch-name>

Reset and go back to the latest version of master

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git reset --hard origin/master
git reset --hard origin/master
git reset --hard origin/master

Update your local branch with the latest from Master

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout master
git pull
git checkout <you-branch-name>
git rebase master
git checkout master git pull git checkout <you-branch-name> git rebase master
git checkout master 
git pull   
git checkout <you-branch-name>
git rebase master

Merge your local branch into Master and Push

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout master
git merge --ff-only <your-local-branch>
git push
git checkout master git merge --ff-only <your-local-branch> git push
git checkout master  
git merge --ff-only <your-local-branch> 
git push

Force push your branch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git push -f origin <branch-name>
git push -f origin <branch-name>
git push -f origin <branch-name>

Local Changes

List updated files in your working directory

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git status
git status
git status

See what has changed in selected file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git diff <file-path>/<file-name>
git diff <file-path>/<file-name>
git diff <file-path>/<file-name>

Stage new and modified files

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git add .
git add .
git add .

Stage a specific file only 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git add <file-name>
git add <file-name>
git add <file-name>

Stages modified and deleted

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git add -u
git add -u
git add -u

Stages all

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git add -A
git add -A
git add -A

Commit changes with a message

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git commit -m "Short description of this commit"
git commit -m "Short description of this commit"
git commit -m "Short description of this commit"

Update message of a previous commit

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git commit --amend
git commit --amend
git commit --amend

For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git commit --amend -m "Added new line #4"
git commit --amend -m "Added new line #4"
git commit --amend -m "Added new line #4"

Publish your local branch to remote

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git push -u origin <branch-name>
git push -u origin <branch-name>
git push -u origin <branch-name>

Remove file from being tracked: 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git rm --cached sites/default/settings.php
git rm --cached sites/default/settings.php
git rm --cached sites/default/settings.php

List all stashes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash list
git stash list
git stash list

Reapply a specific stash from the list of stashes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash apply stash@{2}
git stash apply stash@{2}
git stash apply stash@{2}

Temporarily shelve (or stash) changes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash
git stash
git stash

Temporarily stash changes including new/untracked files

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash -u
git stash -u
git stash -u

Reapply previously stashed changes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash pop
git stash pop
git stash pop

 Reapply previously stashed changes and keep them in your stash

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash apply
git stash apply
git stash apply

Stash changes with a spcific message

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git stash save "Your message here"
git stash save "Your message here"
git stash save "Your message here"

Delete file from local Git repository, but keep it in your working directory as an ignored file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git rm --cached <file-name>
git rm --cached <file-name>
git rm --cached <file-name>

Delete file from local Git repo AND from local file system

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git rm <file-name>
git rm <file-name>
git rm <file-name>

Committing an ignored file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git add -f <file-name>
git commit -m "Force adding file which is on the list of ignored files"
git add -f <file-name> git commit -m "Force adding file which is on the list of ignored files"
git add -f <file-name>
git commit -m "Force adding file which is on the list of ignored files"

Take specific, single commit from one branch and apply it onto another

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git cherry-pick <commit-hash>
git cherry-pick <commit-hash>
git cherry-pick <commit-hash>

Remove untracked files at specified path only

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git clean -f <path>
git clean -f <path>
git clean -f <path>

Remove untracked files and directories

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git clean -df
git clean -df
git clean -df

 

Merging or Squashing Commits

If you have 2 or 3 latest commits and would like to merge or squash them into a single commit with a new message, you can you the command below. The postfix the at end of HEAD~ is the number if commits you would like to merge together into a single one.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git reset --soft HEAD~2 && git commit
git reset --soft HEAD~2 && git commit
git reset --soft HEAD~2 && git commit

 

Undoing Changes

Return to old version of the file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout <file-name>
git checkout <file-name>
git checkout <file-name>

Check out a file from a specified commit

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout <commit> <file-name>
git checkout <commit> <file-name>
git checkout <commit> <file-name>

and If you decide you don’t want to keep the this version of the file, you can then check out the most recent version you have with the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout HEAD <file-name>
git checkout HEAD <file-name>
git checkout HEAD <file-name>

Update all files in the working directory to match the specified commit

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout <commit>
git checkout <commit>
git checkout <commit>

Undo the changes introduced by the commit
Generate a new commit that undoes all of the changes introduced in <commit>, and then apply it to your current branch.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git revert <commit>
git revert <commit>
git revert <commit>

Unstage the specified file but keep the changes made

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git reset <file>
git reset <file>
git reset <file>

Reset the staging area and keep the changes made

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git reset
git reset
git reset

Reset the staging area and override the changes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git reset --hard
git reset --hard
git reset --hard

Reset the staging area to a specified commit

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git reset <commit>
git reset <commit>
git reset <commit>

Permanently undo committed snapshots up to specified commit

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git reset --hard <commit>
git reset --hard <commit>
git reset --hard <commit>

Remove untracked files from your working directory

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git clean -f
git clean -f
git clean -f

Show you which untracked files are going to be removed without actually removing it 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git clean -n
git clean -n
git clean -n

 

Working with Tags

In git you can tag specific points in history as being important. Developers use this functionality to mark release points. For example v1.0 or v1.2.3 and so on. And then you can create a new branch at a specific tag with git checkout -b <branch-name> <tag-name>

Mark current commit with a tag

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git tag <tag-name>
git tag <tag-name>
git tag <tag-name>

Tag a specific commit 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git tag <tag-name> a4db8cc
git push --tags
git tag <tag-name> a4db8cc git push --tags
git tag <tag-name> a4db8cc
git push --tags

List all tags

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git tag
git tag
git tag

Checkout by specific Tag

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout tags/<tag name>
git checkout tags/<tag name>
git checkout tags/<tag name>

 

Git Best Practices

  • Agree with your team on a Workflow
  • Commit Often
  • Don’t Commit Half-Done Work
  • Test Before You Commit and spend an extra minute to look at the changes you made with git diff before you make a commit
  • Do not use git add . all the time. Consider using git add [file-name] to make sure you stage only the files you indeed mean to commit
  • Write short but still descriptive commit messages
  • Only use git reset to undo local changes—you should never reset snapshots that have been shared with other developers
  • Keep master branch as to be always production-like and deployable
  • To work on a new feature, create a new branch out of a dev branch. When feature is ready, merge feature branch back to dev branch. When dev is well tested and ready, merge dev branch into production-like master branch.
  • For immediate and urgent fixes found in production, branch off a master branch to work on a fix. When fix is ready merge it back to master branch.
  • To keep your feature branch fresh and up to date with the latest changes in master, use rebase

If you are interested in learning more about how to work with Git, you can check out these video courses below:

Version Control

There is just no way you can successfully manage your app and its releases without a good knowledge of version control system like Git. Every developer you join or every software team you join to work on an app will require you to be able to work with a version control system. It is a must to have knowledge and I suggest you learn it and the sooner the better.

Git Complete: The definitive, step-by-step guide to Git. Video Course.

icon icon

Git Complete Mastery with GitHub : 100% Hands-on Git Video Course

iconGit Complete Mastery with GitHub

Git Going Fast: One Hour Git Crash Video Course

iconGit Going Fast: One Hour Git Crash Course

Leave a Reply

Your email address will not be published. Required fields are marked *