Skip to content

Git Cheatsheet

Git

About Git

Git is a distributed version control system that tracks versions of files. It is often used to control source code by programmers who are developing software collaboratively. Design goals of Git include speed, data integrity, and support for distributed, non-linear workflows — thousands of parallel branches running on different computers.

Table of Contents

Basic Commands

  1. Repository Setup

    Terminal window
    git init # Initialize a new repository
    git clone <url> # Clone a repository
    git status # Check repository status
  2. Staging Changes

    Terminal window
    git add <file> # Add file to staging area
    git add . # Add all changes to staging area
    git add -p # Interactive staging
  3. Committing

    Terminal window
    git commit -m "message" # Commit with message
    git commit -am "message" # Add and commit in one command
    git commit --amend # Modify the last commit

Git-Cheatsheet

Branch Management

Terminal window
git branch # List branches
git branch <name> # Create new branch
git checkout <branch> # Switch to branch
git checkout -b <branch> # Create and switch to new branch
git switch <branch> # Switch to branch (new syntax)
git switch -c <branch> # Create and switch to new branch (new syntax)

Remote Operations

Terminal window
git remote add origin <url> # Add remote repository
git remote -v # List remote repositories
git push origin <branch> # Push changes to remote
git pull origin <branch> # Pull changes from remote
git fetch origin # Fetch changes without merging

Undoing Changes

  1. Working Directory

    Terminal window
    git checkout -- <file> # Discard changes in working directory
    git clean -fd # Remove untracked files and directories
  2. Staged Changes

    Terminal window
    git reset <file> # Unstage changes
    git reset # Unstage all changes
  3. Commits

    Terminal window
    git reset --soft HEAD~1 # Undo last commit, keep changes staged
    git reset --hard HEAD~1 # Undo last commit and discard changes
    git revert <commit> # Create new commit that undoes changes

Git Configuration

Terminal window
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global core.editor "code --wait" # Set VS Code as default editor
git config --list # List all configurations

Advanced Operations

Terminal window
git stash # Stash changes
git stash list # List stashes
git stash pop # Apply and remove latest stash
git stash apply # Apply latest stash without removing it

Best Practices

  • Write clear, descriptive commit messages
  • Commit early and often
  • Keep branches focused and short-lived
  • Pull before pushing to avoid conflicts
  • Use .gitignore for files that shouldn’t be tracked
  • Regularly fetch and merge from upstream
  • Use meaningful branch names

Common Workflows

  1. Feature Branch Workflow

    • Create feature branch
    • Make changes and commit
    • Pull latest main/develop
    • Merge or rebase
    • Push and create pull request
  2. Hotfix Workflow

    • Branch from production/main
    • Fix issue and commit
    • Merge to both main and develop
    • Tag release if necessary