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
mainfor production,developfor integration,feature/*for new features,release/*for version preparation, andhotfix/*for urgent patches. - Trunk-based development: Short-lived branches merged to
mainmultiple 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
developorreleasebranches.
Essential Commands
git init— Initializes a new repository in the current directorygit clone <url>— Clones a complete remote repository including full historygit add <file>— Stages changes for the next commitgit commit -m "message"— Saves changes to history with a descriptive messagegit push— Uploads local commits to the remote repositorygit pull— Fetches and merges changes from the remote repositorygit branch— Lists, creates, or deletes branchesgit merge <branch>— Merges a branch into the current branchgit rebase— Reorganizes commits to maintain a linear historygit stash— Temporarily saves uncommitted changesgit log— Shows the commit history of the repositorygit 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 --rebasebefore pushing to avoid unnecessary merge commits and keep history clean.