Git & GitHub Cheatsheet

Ananya
2 min readFeb 6, 2025

--

1. Configuration

# Set username and email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Check configuration
git config --list

2. Initialize Repository

# Initialize a new Git repository
git init

3. Clone Repository

# Clone a repository
git clone <repository_url>

4. Basic Commands

# Check status of working directory
git status
# Add files to staging area
git add <file>
# Add all files
git add .
# Commit changes
git commit -m "Commit message"
# View commit history
git log

5. Branching

# 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>

6. Merging & Rebasing

# Merge a branch into the current branch
git merge <branch_name>
# Rebase a branch
git rebase <branch_name>

7. Remote Repositories

# Add a remote repository
git remote add origin <repository_url>
# View remote repositories
git remote -v
# Push changes to a remote repository
git push origin <branch_name>
# Pull latest changes
git pull origin <branch_name>

8. Stashing Changes

# Stash changes
git stash
# List stashes
git stash list
# Apply last stash
git stash apply

9. Undoing Changes

# Discard changes in a file
git checkout -- <file>
# Reset last commit (soft reset, keeps changes)
git reset --soft HEAD~1
# Reset last commit (hard reset, removes changes)
git reset --hard HEAD~1

10. GitHub Collaboration

# Fork a repository (on GitHub website)
# Create a pull request (on GitHub website)

11. Git Ignore

# Create a .gitignore file to ignore files
echo "node_modules/" >> .gitignore
git rm -r --cached node_modules/
git commit -m "Updated .gitignore"

12. Useful Shortcuts

# Show last commit
git show
# View differences
git diff

--

--

No responses yet