Git and Git Flow Cheatsheet

Complete reference guide for Git commands and Git Flow workflow. Master version control with essential commands, branching strategies, and best practices.

Git and Git Flow Cheatsheet

Version Control

Essential Tool

Git is the most popular distributed version control system used by developers worldwide. This comprehensive cheatsheet covers all essential Git commands and Git Flow workflow to help you master version control.

Quick Reference

Most Used Commands

Day-to-day commands for tracking changes.

Emergency Commands

Quickly undo mistakes and save work.

Setup

Configuration Commands

Show current configuration

git config --list

Show repository configuration

git config --local --list

Show global configuration

git config --global --list

Show system configuration

git config --system --list

Set user name

git config --global user.name "[firstname lastname]"

Set user email

git config --global user.email "[valid-email]"

Set automatic command line coloring

git config --global color.ui auto

Set global editor for commit

git config --global core.editor vi

Configuration Files

Repository specific configuration file [--local]

<repo>/.git/config

User-specific configuration file [--global]

~/.gitconfig

System-wide configuration file [--system]

/etc/gitconfig

Create

Repository Creation

Clone repository via SSH

git clone ssh://user@domain.com/repo.git

Clone repository via HTTP

git clone http://domain.com/user/repo.git

Create new local repository

git init

Create new repository in directory

git init <directory>

Local Changes

Status and Differences

Changes in working directory

git status

Changes to tracked files

git diff

See changes/difference of a specific file

git diff <file>

Adding Changes

Add all current changes to the next commit

git add .

Add some changes in <file> to the next commit

git add -p <file>

Add only the mentioned files to the next commit

git add <filename1> <filename2>

Committing Changes

Commit all local changes in tracked files

git commit -a

Commit previously staged changes

git commit

Commit with message

git commit -m 'message here'

Commit skipping staging area

git commit -am 'message here'

Commit to some previous date

git commit --date="`date --date='n day ago'`" \
  -am "<Commit Message Here>"

Change last commit

Don't amend published commits!

git commit -a --amend

Amend with previous commit message

Don't amend published commits!

git commit --amend --no-edit

Change committer date of last commit

GIT_COMMITTER_DATE="date" git commit --amend

Change Author date of last commit

git commit --amend --date="date"

Stashing

Move uncommitted changes to another branch

git stash
git checkout branch2
git stash pop

Restore stashed changes back to current branch

git stash apply

Restore particular stash back to current branch

{stash_number} can be obtained from git stash list

git stash apply stash@{stash_number}

Remove the last set of stashed changes

git stash drop

A text search on all files in the directory

git grep "Hello"
git grep "Hello" v2.5

Show commits that introduced a specific keyword

git log -S 'keyword'

Show commits with keyword (regex)

git log -S 'keyword' --pickaxe-regex

Commit History

Viewing History

Show all commits, starting with newest

git log

Show commits (hash and message only)

git log --oneline

Show all commits of a specific user

git log --author="username"

Show changes over time for a specific file

git log -p <file>

Display commits in remote/branch comparison

git log --oneline <origin/master>..<remote/master> \
  --left-right

Who changed, what and when in <file>

git blame <file>

Show Reference log

git reflog show

Delete Reference log

git reflog delete

Move / Rename

File Operations

Rename a file (Rename Index.txt to Index.html)

git mv Index.txt Index.html

Branches & Tags

Branch Management

List all local branches

git branch

List local/remote branches

git branch -a

List all remote branches

git branch -r

Switch HEAD branch

git checkout <branch>

Checkout single file from different branch

git checkout <branch> -- <filename>

Create and switch new branch

git checkout -b <branch>

Switch to previous branch

git checkout -

Create new branch from existing branch

git checkout -b <new_branch> <existing_branch>

Create new branch from existing commit

git checkout <commit-hash> -b <new_branch_name>

Create a new branch based on your current HEAD

git branch <new-branch>

Create new tracking branch from remote

git branch --track <new-branch> <remote-branch>

Delete a local branch

git branch -d <branch>

Rename current branch to new branch name

git branch -m <new_branch_name>

Force delete a local branch

You will lose unmerged changes!

git branch -D <branch>

Apply specific commit from another branch

git cherry-pick <commit hash>

Tag Management

Mark HEAD with a tag

git tag <tag-name>

Mark HEAD with tag and message

git tag -a <tag-name>

Mark HEAD with tag that includes a message

git tag <tag-name> -am 'message here'

List all tags

git tag

List all tags with their messages

git tag -n

Update & Publish

Remote Management

List all current configured remotes

git remote -v

Show information about a remote

git remote show <remote>

Add new remote repository, named <remote>

git remote add <remote> <url>

Rename remote repository

git remote rename <remote> <new_remote>

Remove a remote

git remote rm does not delete the remote repository from the server. It simply removes the remote and its references from your local repository.

git remote rm <remote>

Fetching and Pulling

Download changes without integrating

git fetch <remote>

Download changes and merge into HEAD

git remote pull <remote> <url>

Get all changes from HEAD to local repository

git pull origin master

Get changes without merge

git pull --rebase <remote> <branch>

Pushing

Publish local changes on a remote

git push <remote> <branch>

Delete a branch on the remote (since Git v1.5.0)

git push <remote> :<branch>

Delete a branch on the remote (since Git v1.7.0)

git push <remote> --delete <branch>

Publish your tags

git push --tags

Merge Tools

Configure merge tool globally

git config --global merge.tool meld

Use configured merge tool to solve conflicts

git mergetool

Merge & Rebase

Merging

Merge branch into your current HEAD

git merge <branch>

List merged branches

git branch --merged

Rebasing

Rebase current HEAD onto <branch>

Don't rebase published commit!

git rebase <branch>

Abort a rebase

git rebase --abort

Continue a rebase after resolving conflicts

git rebase --continue

Mark file as resolved

git add <resolved-file>

Remove resolved file

git rm <resolved-file>

Squashing commits

git rebase -i <commit-just-before-first>

Now replace this:

pick <commit_id>
pick <commit_id2>
pick <commit_id3>

to this:

pick <commit_id>
squash <commit_id2>
squash <commit_id3>

Undo

Reset Operations

Discard all local changes

git reset --hard HEAD

Undo last git add

git reset HEAD

Discard local changes in a specific file

git checkout HEAD <file>

Revert a commit (by producing a new commit with contrary changes)

git revert <commit>

Reset HEAD and discard all changes

git reset --hard <commit>

Reset HEAD to remote branch state

git reset --hard <remote/branch>

Reset HEAD and preserve unstaged changes

git reset <commit>

Reset HEAD and preserve local changes

git reset --keep <commit>

Remove accidentally committed files

git rm -r --cached .
git add .
git commit -m "remove xyz file"

Git Flow

Improved Git-flow workflow for better branch management.

Installation

OSX Homebrew

brew install git-flow-avh

OSX Macports

port install git-flow

Linux (Debian-based)

sudo apt-get install git-flow

Windows (Cygwin)

You need wget and util-linux to install git-flow.

wget -q -O - --no-check-certificate \
  https://raw.githubusercontent.com/petervanderdoes/gitflow/develop/contrib/gitflow-installer.sh \
  install <state> | bash

Getting Started

Git flow needs to be initialized in order to customize your project setup. Start using git-flow by initializing it inside an existing git repository:

Initialize with questions

git flow init

Initialize with default values

git flow init -d

Features

Develop new features for upcoming releases. Typically exist in developers repos only.

Start a new feature

This action creates a new feature branch based on 'develop' and switches to it.

git flow feature start MYFEATURE

Finish up a feature

Finish the development of a feature. This action:

  1. Merges MYFEATURE into 'develop'
  2. Removes the feature branch
  3. Switches back to 'develop' branch
git flow feature finish MYFEATURE

Publish a feature

Are you developing a feature in collaboration? Publish a feature to the remote server so it can be used by other users.

git flow feature publish MYFEATURE

Get a published feature

Get a feature published by another user.

git flow feature pull origin MYFEATURE

Track a origin feature

You can track a feature on origin by using

git flow feature track MYFEATURE

Make a Release

Support preparation of a new production release. Allow for minor bug fixes and preparing meta-data for a release.

Start a release

To start a release, use the git flow release command. It creates a release branch created from the 'develop' branch.

git flow release start RELEASE [BASE]

Publish the release branch

It's wise to publish the release branch after creating it to allow release commits by other developers.

git flow release publish RELEASE

Track a remote release

git flow release track RELEASE

Finish up a release

Finishing a release performs several actions:

  1. Merges the release branch back into 'master'
  2. Tags the release with its name
  3. Back-merges the release into 'develop'
  4. Removes the release branch
git flow release finish RELEASE

Don't forget to push your tags with git push --tags

Hotfixes

Hotfixes arise from the necessity to act immediately upon an undesired state of a live production version. May be branched off from the corresponding tag on the master branch that marks the production version.

Start a hotfix

Like the other git flow commands, a hotfix is started with

git flow hotfix start VERSION [BASENAME]

Finish a hotfix

By finishing a hotfix it gets merged back into develop and master. Additionally the master merge is tagged with the hotfix version

git flow hotfix finish VERSION

Git Flow Workflow Visualization

The Git Flow workflow provides a robust framework for managing releases and features:

  • Master branch: Production-ready code
  • Develop branch: Integration branch for features
  • Feature branches: New feature development
  • Release branches: Prepare for production release
  • Hotfix branches: Quick fixes for production issues

Git Flow is excellent for teams with scheduled releases, but consider GitHub Flow or GitLab Flow for continuous deployment workflows.

Best Practices

Follow these Git best practices for better collaboration and code management.

  • Write meaningful commit messages - Use conventional commits format
  • Use branches for features - Keep master/main clean
  • Review code before merging - Use pull/merge requests
  • Keep commits atomic - One logical change per commit
  • Test before pushing - Ensure code works locally
  • Use .gitignore - Don't commit build artifacts or sensitive files

Learn More

Explore comprehensive Git documentation


Written by

Deepak Jangra

Created At

Thu Jun 12 2025

Updated At

Fri Jun 13 2025

Cheatsheets

Your go-to resource for quick reference guides on essential development tools and technologies.

© 2025 Deepak Jangra. All rights reserved.