Back to tools

Git Commands Cheatsheet

Forgot how to do something in Git? Find the exact command here.

Git: Distributed Version Control

Git is a distributed version control system created by Linus Torvalds in 2005 to manage Linux kernel development. Unlike client-server systems like Subversion (SVN), every developer has a complete local copy of the repository with full history, enabling offline work.

Today, over 93% of professional developers use Git as their primary version control system, according to the Stack Overflow 2022 survey. Services like GitHub, GitLab, and Bitbucket host millions of Git repositories.

Common Workflows

  • Git Flow: The most popular branching model. Uses main for production, develop for integration, feature/* for new features, release/* for version preparation, and hotfix/* for urgent patches.
  • Trunk-based development: Short-lived branches merged to main multiple times daily. Ideal for teams practicing CI/CD and continuous deployment.
  • GitHub Flow: A simplified Git Flow. Create a branch, commit, open a pull request, review, and deploy. No develop or release branches.

Essential Commands

  • git init — Initializes a new repository in the current directory
  • git clone <url> — Clones a complete remote repository including full history
  • git add <file> — Stages changes for the next commit
  • git commit -m "message" — Saves changes to history with a descriptive message
  • git push — Uploads local commits to the remote repository
  • git pull — Fetches and merges changes from the remote repository
  • git branch — Lists, creates, or deletes branches
  • git merge <branch> — Merges a branch into the current branch
  • git rebase — Reorganizes commits to maintain a linear history
  • git stash — Temporarily saves uncommitted changes
  • git log — Shows the commit history of the repository
  • git diff — Shows differences between unstaged changes

Tips and Best Practices

  • Atomic commits: Each commit should represent a single logical change. Do not mix bug fixes with new features in the same commit.
  • Descriptive messages: Use the standard format: subject line (max 50 chars) followed by a body explaining the what and why of the change.
  • .gitignore: Keep an up-to-date .gitignore to avoid committing keys, passwords, compiled files, and dependencies (node_modules, vendor, .env).
  • Do not commit secrets: If you accidentally commit a key, deleting the file is not enough — you must rotate the key and rewrite history with git filter-branch.
  • Pull before push: Always run git pull --rebase before pushing to avoid unnecessary merge commits and keep history clean.