---
visibility: public
---

# Git Cheat Sheet & Git Flow

**repo:** [arslanbilal/git-cheat-sheet](https://github.com/arslanbilal/git-cheat-sheet)  
**category:** [[development-environment|Development Environment]]
**related:** [[git-add-ons|Git Add Ons]]

---

# Git and Git Flow Cheat Sheet 
[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/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](#-setup)
- [⚙️ Configuration Files](#️-configuration-files)
- [🆕 Create Repository](#-create-repository)
- [📝 Local Changes](#-local-changes)
- [🔍 Search](#-search)
- [📖 Commit History](#-commit-history)
- [📁 [Move](/@harrisonqian/awesome/wiki/programming-languages/move) / Rename](#-move--rename)
- [🌿 Branches & Tags](#-branches--tags)
- [🔄 Update & Publish](#-update--publish)
- [🔀 Merge & Rebase](#-merge--rebase)
- [↩️ Undo](#️-undo)
- [🌊 Git Flow](#-git-flow)
- [🌍 Other Languages](#-other-languages)

---

## 🔧 Setup

### View Configuration

**Show current configuration:**
```bash
git config --list
```

**Show repository configuration:**
```bash
git config --local --list
```

**Show global configuration:**
```bash
git config --global --list
```

**Show system configuration:**
```bash
git config --system --list
```

### User Configuration

**Set your name for version history:**
```bash
git config --global user.name "[firstname lastname]"
```

**Set your email address:**
```bash
git config --global user.email "[valid-email]"
```

### Display & Editor Settings

**Enable automatic command line coloring:**
```bash
git config --global color.ui auto
```

**Set global editor for commits:**
```bash
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:**
```bash
git clone ssh://user@domain.com/repo.git
```

**Via HTTPS:**
```bash
git clone https://domain.com/user/repo.git
```

### Initialize New Repository

**Create repository in current directory:**
```bash
git init
```

**Create repository in specific directory:**
```bash
git init <directory>
```

---

## 📝 Local Changes

### Check Status & Differences

**View working directory status:**
```bash
git status
```

**Show changes to tracked files:**
```bash
git diff
```

**Show changes in specific file:**
```bash
git diff <file>
```

### Staging Changes

**Add all current changes:**
```bash
git add .
```

**Add specific files:**
```bash
git add <filename1> <filename2>
```

**Interactively add parts of a file:**
```bash
git add -p <file>
```

### Committing Changes

**Commit all tracked file changes:**
```bash
git commit -a
```

**Commit staged changes:**
```bash
git commit
```

**Commit with message:**
```bash
git commit -m 'message here'
```

**Skip staging and commit with message:**
```bash
git commit -am 'message here'
```

**Commit with specific date:**
```bash
git commit --date="`date --date='n day ago'`" -am "<Commit Message Here>"
```

### Modify Last Commit

> ⚠️ **Warning:** Don't amend published commits!

**Amend last commit:**
```bash
git commit -a --amend
```

**Amend without changing commit message:**
```bash
git commit --amend --no-edit
```

**Change committer date:**
```bash
GIT_COMMITTER_DATE="date" git commit --amend
```

**Change author date:**
```bash
git commit --amend --date="date"
```

### Stashing Changes

**Save current changes temporarily:**
```bash
git stash
```

**Apply last stashed changes:**
```bash
git stash apply
```

**Apply specific stash:**
```bash
git stash apply stash@{stash_number}
```
> Use `git stash list` to see available stashes

**Remove last stash:**
```bash
git stash drop
```

**Move uncommitted changes to another branch:**
```bash
git stash
git checkout branch2
git stash pop
```

---

## 🔍 Search

### Text Search

**Search for text in all files:**
```bash
git grep "Hello"
```

**Search in specific version:**
```bash
git grep "Hello" v2.5
```

### Commit Search

**Find commits that introduced specific keyword:**
```bash
git log -S 'keyword'
```

**Search with regular expression:**
```bash
git log -S 'keyword' --pickaxe-regex
```

---

## 📖 Commit History

### Basic History

**Show all commits (detailed):**
```bash
git log
```

**Show commits (one line each):**
```bash
git log --oneline
```

**Show commits by specific author:**
```bash
git log --author="username"
```

**Show changes for specific file:**
```bash
git log -p <file>
```

### Advanced History

**Compare branches:**
```bash
git log --oneline <origin/master>..<remote/master> --left-right
```

**Show who changed what and when:**
```bash
git blame <file>
```

### Reference Logs

**Show reference log:**
```bash
git reflog show
```

**Delete reference log:**
```bash
git reflog delete
```

---

## 📁 Move / Rename

**Rename a file:**
```bash
git mv Index.txt Index.html
```

---

## 🌿 Branches & Tags

### List Branches

**List local branches:**
```bash
git branch
```

**List all branches (local + remote):**
```bash
git branch -a
```

**List remote branches:**
```bash
git branch -r
```

**List merged branches:**
```bash
git branch --merged
```

### Switch & Create Branches

**Switch to existing branch:**
```bash
git checkout <branch>
```

**Create and switch to new branch:**
```bash
git checkout -b <branch>
```

**Switch to previous branch:**
```bash
git checkout -
```

**Create branch from existing branch:**
```bash
git checkout -b <new_branch> <existing_branch>
```

**Create branch from specific commit:**
```bash
git checkout <commit-hash> -b <new_branch_name>
```

**Create branch without switching:**
```bash
git branch <new-branch>
```

**Create tracking branch:**
```bash
git branch --track <new-branch> <remote-branch>
```

### Branch Operations

**Checkout single file from different branch:**
```bash
git checkout <branch> -- <filename>
```

**Apply specific commit from another branch:**
```bash
git cherry-pick <commit hash>
```

**Rename current branch:**
```bash
git branch -m <new_branch_name>
```

**Delete local branch:**
```bash
git branch -d <branch>
```

**Force delete local branch:**
```bash
git branch -D <branch>
```
> ⚠️ **Warning:** You will lose unmerged changes!

### Tags

**Create tag at HEAD:**
```bash
git tag <tag-name>
```

**Create annotated tag:**
```bash
git tag -a <tag-name>
```

**Create tag with message:**
```bash
git tag <tag-name> -am 'message here'
```

**List all tags:**
```bash
git tag
```

**List tags with messages:**
```bash
git tag -n
```

---

## 🔄 Update & Publish

### Remote Management

**List configured remotes:**
```bash
git remote -v
```

**Show remote information:**
```bash
git remote show <remote>
```

**Add new remote:**
```bash
git remote add <remote> <url>
```

**Rename remote:**
```bash
git remote rename <remote> <new_remote>
```

**Remove remote:**
```bash
git remote rm <remote>
```
> ℹ️ **Note:** This only removes the remote reference locally, not the remote repository itself.

### Fetch & Pull

**Download changes without merging:**
```bash
git fetch <remote>
```

**Download and merge changes:**
```bash
git pull <remote> <branch>
```

**Get changes from main branch:**
```bash
git pull origin master
```

**Pull with rebase:**
```bash
git pull --rebase <remote> <branch>

---
*truncated — [full list on GitHub](https://github.com/arslanbilal/git-cheat-sheet)*
