Git Basics Cheat Sheet

New

Essential Git commands for daily development workflow

Getting Started

git init

Initialize a new Git repository

git init

git clone

Clone a repository from remote

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

git config

Set user configuration

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

Basic Commands

git status

Check the status of working directory

git status

git add

Stage files for commit

git add .

git commit

Commit staged changes

git commit -m "Add new feature"

git log

View commit history

git log --oneline

git diff

Show differences between commits

git diff HEAD~1

Branching

git branch

List all branches

git branch

git checkout

Switch to a branch

git checkout feature-branch

git checkout -b

Create and switch to new branch

git checkout -b new-feature

git merge

Merge branches

git merge feature-branch

git branch -d

Delete a branch

git branch -d feature-branch

Remote Operations

git remote

Manage remote repositories

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

git fetch

Download objects from remote

git fetch origin

git pull

Fetch and merge from remote

git pull origin main

git push

Push commits to remote

git push origin main

git push -u

Set upstream branch

git push -u origin feature-branch

Undoing Changes

git reset

Reset HEAD to specified commit

git reset --hard HEAD~1

git revert

Create new commit that undoes changes

git revert HEAD

git checkout --

Discard changes in working directory

git checkout -- filename

git stash

Temporarily save changes

git stash push -m "WIP"

git stash pop

Apply and remove stashed changes

git stash pop

Common Workflows

Feature Branch Workflow

Standard workflow for new features

# Create and switch to feature branch
git checkout -b feature/new-feature

# Make changes and commit
git add .
git commit -m "Add new feature"

# Push to remote
git push -u origin feature/new-feature

# Create pull request on GitHub/GitLab
# After review, merge to main

# Clean up
git checkout main
git pull origin main
git branch -d feature/new-feature

Hotfix Workflow

Quick fix for production issues

# Create hotfix branch from main
git checkout main
git pull origin main
git checkout -b hotfix/critical-bug

# Fix the issue
git add .
git commit -m "Fix critical bug"

# Push and merge
git push origin hotfix/critical-bug
git checkout main
git merge hotfix/critical-bug
git push origin main

# Clean up
git branch -d hotfix/critical-bug

Daily Workflow

Common daily Git operations

# Start of day
git checkout main
git pull origin main

# Work on feature
git checkout feature-branch
git add .
git commit -m "Update feature"

# End of day
git push origin feature-branch

Tips & Best Practices

Write clear, descriptive commit messages

Pull before pushing to avoid conflicts

Use feature branches for new development

Check status frequently with git status

Use git stash to save work in progress