comprehensive Git command cheat sheet with examples 2025

 Here's a comprehensive Git command cheat sheet with examples:


## 🚀 Basic Setup & Configuration


```Python

# Configure user information

git config --global user.name "Your Name"

git config --global user.email "your.email@example.com"


# Check configuration

git config --list

```


## 📁 Repository Management


```bash

# Initialize a new repository

git init


# Clone an existing repository

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

git clone https://github.com/user/repo.git my-project  # Clone with custom name


# Check repository status

git status

```


## 📊 Tracking Changes


```bash

# Add files to staging area

git add filename.txt           # Add specific file

git add .                      # Add all files

git add *.js                   # Add all JS files

git add src/                   # Add entire directory


# Commit changes

git commit -m "Commit message"

git commit -m "Add login feature"  # Example


# Add and commit in one step

git commit -am "Commit message"

```


## 🔍 Viewing History & Differences


```bash

# View commit history

git log

git log --oneline              # Compact view

git log --graph --oneline      # Graphical view

git log -p                     # Show changes


# Check differences

git diff                       # Unstaged changes

git diff --staged              # Staged changes

git diff commit1 commit2       # Between two commits

```


## 🌿 Branching


```bash

# List branches

git branch                     # Local branches

git branch -r                  # Remote branches

git branch -a                  # All branches


# Create and switch branches

git branch feature-branch      # Create branch

git checkout feature-branch    # Switch to branch

git checkout -b feature-branch # Create and switch


# Delete branches

git branch -d branch-name      # Safe delete

git branch -D branch-name      # Force delete

```


## 🔄 Merging & Rebasing


```bash

# Merge branches

git checkout main

git merge feature-branch


# Rebase branch

git checkout feature-branch

git rebase main


# Resolve merge conflicts

# Edit conflicted files, then:

git add .

git commit -m "Resolve merge conflicts"

```


## 📤 Remote Repositories


```bash

# Manage remotes

git remote add origin https://github.com/user/repo.git

git remote -v                  # View remotes

git remote remove origin       # Remove remote


# Push to remote

git push origin main

git push -u origin main        # Set upstream


# Pull from remote

git pull origin main

git fetch origin               # Download without merging

```


## 🗑️ Undoing Changes


```bash

# Undo unstaged changes

git checkout -- filename.txt


# Unstage files

git reset HEAD filename.txt


# Amend last commit

git commit --amend -m "New message"


# Revert a commit

git revert commit-hash


# Reset to previous commit

git reset --soft HEAD~1       # Keep changes staged

git reset --mixed HEAD~1      # Keep changes unstaged (default)

git reset --hard HEAD~1       # Discard all changes

```


## 🏷️ Tagging


```bash

# Create tags

git tag v1.0.0                 # Lightweight tag

git tag -a v1.0.0 -m "Version 1.0.0"  # Annotated tag


# List tags

git tag


# Push tags to remote

git push origin v1.0.0

git push origin --tags         # Push all tags

```


## 🗂️ Stashing


```bash

# Stash changes

git stash                      # Stash changes

git stash save "Work in progress"

git stash list                 # List stashes

git stash pop                  # Apply and remove

git stash apply                # Apply without removing

git stash drop                 # Remove stash

```


## 🔎 Advanced Commands


```bash

# Search history

git log --grep="bug fix"       # Search commit messages

git log -S "functionName"      # Search code changes


# Clean up

git clean -n                   # Show what will be deleted

git clean -f                   # Delete untracked files


# Submodules

git submodule add https://github.com/user/repo.git

git submodule update --init

```


## 🎯 Common Workflows


### Feature Branch Workflow

```bash

git checkout -b new-feature

# Make changes...

git add .

git commit -m "Implement new feature"

git push origin new-feature

# Create Pull Request on GitHub

```


### Fixing Mistakes

```bash

# Oops, wrong commit message

git commit --amend -m "Correct message"


# Accidentally committed to wrong branch

git reset --soft HEAD~1

git checkout correct-branch

git commit -c ORIG_HEAD

```


This cheat sheet covers the most commonly used Git commands. Save it for quick reference! 🚀

0 Comments