Git Cheat Sheet & Git Flow

repo: arslanbilal/git-cheat-sheet
category: Development Environment related: Git Add Ons


Git and Git Flow Cheat Sheet

Awesome

<p align="center"> <img alt="Git" src="./Img/git-logo.png" height="190" width="455"> </p>


📖 About

This comprehensive Git cheat sheet helps you master Git commands without memorizing everything. Whether you're a beginner or an experienced developer, this guide provides quick reference to essential Git operations.

Contributions Welcome! Feel free to:

  • Fix grammar mistakes
  • Add new commands
  • Translate to your language
  • Improve explanations

📋 Table of Contents


🔧 Setup

View Configuration

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

User Configuration

Set your name for version history:

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

Set your email address:

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

Display & Editor Settings

Enable automatic command line coloring:

git config --global color.ui auto

Set global editor for commits:

git config --global core.editor vi

⚙️ Configuration Files

Scope Location Command Flag
Repository <repo>/.git/config --local
User ~/.gitconfig --global
System /etc/gitconfig --system

🆕 Create Repository

Clone Existing Repository

Via SSH:

git clone ssh://[email protected]/repo.git

Via HTTPS:

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

Initialize New Repository

Create repository in current directory:

git init

Create repository in specific directory:

git init <directory>

📝 Local Changes

Check Status & Differences

View working directory status:

git status

Show changes to tracked files:

git diff

Show changes in specific file:

git diff <file>

Staging Changes

Add all current changes:

git add .

Add specific files:

git add <filename1> <filename2>

Interactively add parts of a file:

git add -p <file>

Committing Changes

Commit all tracked file changes:

git commit -a

Commit staged changes:

git commit

Commit with message:

git commit -m 'message here'

Skip staging and commit with message:

git commit -am 'message here'

Commit with specific date:

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

Modify Last Commit

⚠️ Warning: Don't amend published commits!

Amend last commit:

git commit -a --amend

Amend without changing commit message:

git commit --amend --no-edit

Change committer date:

GIT_COMMITTER_DATE="date" git commit --amend

Change author date:

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

Stashing Changes

Save current changes temporarily:

git stash

Apply last stashed changes:

git stash apply

Apply specific stash:

git stash apply stash@{stash_number}

Use git stash list to see available stashes

Remove last stash:

git stash drop

Move uncommitted changes to another branch:

git stash
git checkout branch2
git stash pop

Search for text in all files:

git grep "Hello"

Search in specific version:

git grep "Hello" v2.5

Find commits that introduced specific keyword:

git log -S 'keyword'

Search with regular expression:

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

📖 Commit History

Basic History

Show all commits (detailed):

git log

Show commits (one line each):

git log --oneline

Show commits by specific author:

git log --author="username"

Show changes for specific file:

git log -p <file>

Advanced History

Compare branches:

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

Show who changed what and when:

git blame <file>

Reference Logs

Show reference log:

git reflog show

Delete reference log:

git reflog delete

📁 Move / Rename

Rename a file:

git mv Index.txt Index.html

🌿 Branches & Tags

List Branches

List local branches:

git branch

List all branches (local + remote):

git branch -a

List remote branches:

git branch -r

List merged branches:

git branch --merged

Switch & Create Branches

Switch to existing branch:

git checkout <branch>

Create and switch to new branch:

git checkout -b <branch>

Switch to previous branch:

git checkout -

Create branch from existing branch:

git checkout -b <new_branch> <existing_branch>

Create branch from specific commit:

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

Create branch without switching:

git branch <new-branch>

Create tracking branch:

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

Branch Operations

Checkout single file from different branch:

git checkout <branch> -- <filename>

Apply specific commit from another branch:

git cherry-pick <commit hash>

Rename current branch:

git branch -m <new_branch_name>

Delete local branch:

git branch -d <branch>

Force delete local branch:

git branch -D <branch>

⚠️ Warning: You will lose unmerged changes!

Tags

Create tag at HEAD:

git tag <tag-name>

Create annotated tag:

git tag -a <tag-name>

Create tag with message:

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

List all tags:

git tag

List tags with messages:

git tag -n

🔄 Update & Publish

Remote Management

List configured remotes:

git remote -v

Show remote information:

git remote show <remote>

Add new remote:

git remote add <remote> <url>

Rename remote:

git remote rename <remote> <new_remote>

Remove remote:

git remote rm <remote>

ℹ️ Note: This only removes the remote reference locally, not the remote repository itself.

Fetch & Pull

Download changes without merging:

git fetch <remote>

Download and merge changes:

git pull <remote> <branch>

Get changes from main branch:

git pull origin master

Pull with rebase:

git pull --rebase <remote> <branch>

---
*truncated — [full list on GitHub](https://github.com/arslanbilal/git-cheat-sheet)*
[[curator]]
I'm the Curator. I can help you navigate, organize, and curate this wiki. What would you like to do?