5.git
Cheat Sheet

Git Commands Cheat Sheet

Configuration

Setting up Git

# Set up your name
git config --global user.name "Your Name"
 
# Set up your email
git config --global user.email "[email protected]"
 
# Check your Git configuration
git config --list

Repository

Initialize a New Git Repository

git init

Clone an Existing Repository

git clone <repository_url>

Basic Snapshotting

Check the Status of Your Repository

git status

Add Changes to the Staging Area

# Add a specific file
git add <file_name>
 
# Add all files
git add .

Commit Changes

# Commit with a message
git commit -m "Your commit message"

View Commit History

git log
 
# View commit history in a single line format
git log --oneline

View Changes

# View changes in the working directory
git diff
 
# View changes between commits
git diff <commit1> <commit2>

Branching and Merging

List Branches

git branch

Create a New Branch

git branch <branch_name>

Switch to a Branch

git checkout <branch_name>

Create and Switch to a New Branch

git checkout -b <branch_name>

Merge a Branch into the Current Branch

git merge <branch_name>

Delete a Branch

git branch -d <branch_name>

Remote Repositories

List Remote Repositories

git remote -v

Add a Remote Repository

git remote add <remote_name> <repository_url>

Fetch Changes from a Remote Repository

git fetch <remote_name>

Pull Changes from a Remote Repository

git pull <remote_name> <branch_name>

Push Changes to a Remote Repository

git push <remote_name> <branch_name>

Undoing Changes

Unstage a File

git reset <file_name>

Revert Changes in a File

# Discard changes in the working directory
git checkout -- <file_name>

Reset to a Previous Commit

# Soft reset (keeps changes in the working directory)
git reset --soft <commit_hash>
 
# Hard reset (discards changes)
git reset --hard <commit_hash>

Stashing

Stash Changes

git stash

List Stashes

git stash list

Apply Stashed Changes

git stash apply

Drop a Stash

git stash drop

Additional Commands

Show Commit Details

git show <commit_hash>

Tagging

# Create a tag
git tag <tag_name>
 
# List tags
git tag

Viewing a Specific File from a Commit

git show <commit_hash>:<file_path>