Master these ten Git commands and you’ll handle about 90 percent of everyday workflows. Most developers overcomplicate Git; learning these commands cuts the noise and gets work done.
TL;DR: – Learn these ten commands: git init, git clone, git status, git add, git commit, git branch, git checkout, git merge, git rebase, git push.
- Use
status,add, andcommitdaily to keep changes clear and atomic. - Use
branch,checkout, andmergefor safe feature work; userebasefor a linear history when working locally. - Push to a host like GitHub, GitLab, or Bitbucket and pick the hosting plan that fits team size.
10 Essential Git Commands Every Developer Should Know
These ten commands cover most daily Git tasks: init, clone, status, add, commit, branch, checkout, merge, rebase, push.
Below we break each command down with what it does, when to use it, practical examples, and common flags.
git init
git init creates a new Git repository in the current folder.
Use it when you start a new project or turn an existing folder into a repo. After you run git init you’ll see a .git folder, get local history, and be able to make commits. Example: git init then git add . and git commit -m "Initial commit". If you work from a remote first, skip init and git clone instead.
Why it matters: git init gives you version control on a folder. Without it you can’t commit, branch, or push.
git clone
git clone copies an existing remote repository to your machine.
Use it when you want a full working tree and history from a remote host. Example: git clone https://github.com/owner/repo.git. For SSH access use git clone git@github.com:owner/repo.git. Cloning sets up the origin remote and checks out the default branch.
Common option: --depth 1 makes a shallow clone so you save time and disk if you only need the latest commit.
git status
git status shows the current branch, staged changes, unstaged changes, and untracked files.
Run it before committing to see what Git sees. It tells you whether files are ready to commit or if you forgot to stage updates. Example: git status -s shows a compact, two-letter summary per file.
Tip: make git status a reflex before any commit or push. It helps avoid accidental commits.
git add
git add stages changes to be included in the next commit.
Use git add <file> or git add -p to stage specific hunks. git add . stages new and modified files in the current folder, but it won’t stage deleted files unless you use git add -A. Staging lets you make focused commits that address a single change or bug.
Common flag: -p gives you an interactive prompt to pick hunks for precise commits.
git commit
git commit records staged changes into the local repository as a new snapshot.
A good commit message explains the why, not just the what. Example: git commit -m "Fix login redirect when token missing". Use git commit --amend to fix the last commit if it has a typo or a tiny change, but don’t amend commits that are already pushed to shared remotes.
Practice: make small, single-purpose commits for easier reviews and rollbacks.
git branch
git branch lists, creates, or deletes branches.
Use branches for features, experiments, or fixes. Create a branch with git branch feature/login then switch to it with git checkout feature/login. git branch -d deletes a merged branch, and -D forces deletion.
Why branches: they isolate work. Keep main or master stable and use topic branches for everyday changes.
git checkout
git checkout switches branches or restores files to a specific commit.
Use git checkout branch-name to move between branches. To restore a single file to the last commit use git checkout -- file.txt. Note: newer Git recommends git switch for branch switching and git restore for file recovery. Example for the newer command: git switch -c feature/x creates and switches.
Safety note: only run git checkout when your working tree is clean or when you don’t mind losing uncommitted changes.
git merge
git merge combines another branch into the current branch.
Run git checkout main then git merge feature/login to bring feature changes into main. If there are conflicts Git will pause and ask you to resolve them. After you fix conflicts, git add the files and git commit.
Merge types: fast-forward merges happen when branches haven’t diverged; true merges create a merge commit that records the join point.
git rebase
git rebase reapplies commits from one branch onto another to create a linear history.
Use git rebase main while on a feature branch to move your feature commits on top of the latest main commits. Rebase cleans up history but rewrites commits, so never rebase public commits that others rely on.
When to choose: prefer rebase for tidy pull requests; prefer merge when you need to preserve the original timeline and context.
git push
git push uploads local commits to a remote repository.
After committing, run git push origin feature/login to publish your branch. git push --set-upstream origin feature/login links the local branch to the remote one so future pushes are easier. Use git push --force-with-lease only when you need to overwrite remote history and you know what you’re doing.
Host integration: pushing is how CI systems and teammates see your work.
Quick comparison table: commands at a glance
| Command | Purpose | Typical flags | Risk level |
|---|---|---|---|
git init | Start repo | none | Low |
git clone | Copy remote | --depth 1 | Low |
git status | Check state | -s | None |
git add | Stage changes | -p, -A | Low |
git commit | Save snapshot | -m, --amend | Medium |
git branch | Manage branches | -d, -D | Low |
git checkout | Switch/restore | -- | Medium |
git merge | Combine branches | --no-ff | Medium |
git rebase | Rewrite history | -i | High (public risk) |
git push | Upload commits | --force-with-lease | Medium-High |
When to use merge vs rebase
Merge preserves the exact history and is safe for shared branches. Rebase creates a linear history and is useful for cleaning up local commits before publishing. Use merge when multiple people worked on a branch or when you need a historical record of the branch junction. Use rebase for tidy pull requests and smaller teams where you can coordinate rewritten history.
A quick rule: never rebase commits that are already pushed to a shared remote without team agreement.
Git hosting comparison
This table shows popular hosts and how they map to team needs and workflows.
| Host | Free tier | Team tiers | Notable feature | Pricing page |
|---|---|---|---|---|
| GitHub | Yes | Team, Enterprise | Large ecosystem, Actions CI | https://github.com/pricing |
| GitLab | Yes | Starter, Premium, Ultimate | Built-in CI/CD, self-host option | https://about.gitlab.com/pricing/ |
| Bitbucket | Yes | Premium, Standard | Tight Jira integration | https://bitbucket.org/product/pricing/ |
Pick GitHub for open source scale and community tools, GitLab if you want built-in CI or a self-host option, and Bitbucket when Jira integration matters.
Practical workflows and examples
- Daily work:
git status,git add -p,git commit -m "small change",git push. - Feature flow:
git checkout -b feature/x, work with many commits,git rebase mainlocally to update, thengit push --set-upstream origin feature/x. Open a merge request or pull request on the host. - Hotfix: branch from
main, fix,git commitandgit push, then merge back tomainand tag a release.
Example: Fix a bug and keep history tidy
git checkout -b fix/login-redirect- Make changes.
git add -pthengit commit -m "Fix login redirect when token missing"git fetch originthengit rebase origin/main- Resolve conflicts if any,
git push --force-with-lease origin fix/login-redirect - Open PR and merge to main.
Safe habits and common mistakes
- Habit: run
git statusbefore every commit and push. - Mistake:
git push --forcewithout--force-with-leasecan overwrite others’ work. Use--force-with-leaseto lower the chance of clobbering someone else. - Habit: make small, focused commits with clear messages.
- Mistake: rebasing public history without coordination causes confusion.
Frankly, what most people get wrong here is thinking force pushes are routine. They’re not. Coordinate first.
Tools and GUIs that help
Use the official Git documentation for commands and flags: Git documentation.
Try GitHub Desktop, Sourcetree, or GitKraken if you prefer a GUI.
For CI and automation, configure GitHub Actions or GitLab CI in your repo. See GitHub quickstart for setup help.
The reality is a GUI can speed onboarding, but the CLI is still the best for scripting and tricky merges.
Quotes from developers
Developers on Stack Overflow and GitHub threads often report that learning git rebase unlocked cleaner pull requests and fewer merge commits. See community discussions on rebase vs merge on Stack Overflow for more context: https://stackoverflow.com/search?q=git+rebase+merge.
Table: When to use which command (short guide)
| Goal | First command to run | Follow ups |
|---|---|---|
| Start a repo | git init | git add, git commit |
| Get remote code | git clone | git checkout branch |
| Prepare a commit | git add -p | git commit |
| Share work | git push | Open PR on host |
| Update branch | git fetch | git rebase or git merge |
Who this guide is for
This guide is for developers who write code every day, code reviewers, DevOps engineers, and team leads. If you work on a team with shared repositories, these commands will make branching, sharing, and reviewing work predictable and faster.
Resources and further reading
- Official Git manual at the Git website: Git documentation.
- GitHub quickstart and guides: GitHub Docs.
- GitLab CI/CD and repository guidance: GitLab Docs.
- Bitbucket product docs: Bitbucket documentation.
Frequently Asked Questions
What is the difference between git merge and git rebase?
Merge creates a new commit that joins two branch histories, preserving the exact branching timeline. Rebase moves your commits onto another base to make a linear history, rewriting commit hashes; don’t rebase commits others rely on.
How do I undo my last commit?
Use git commit --amend to modify the last commit if it hasn’t been pushed. Use git revert <commit> to create a new commit that undoes a previous commit safely for shared history.
When is it safe to use git push –force?
It’s safe to use git push --force-with-lease only when you understand you’re rewriting remote history and you’ve confirmed no one else has built on the commits you will replace. Coordinate with your team before forcing.
How do I recover a deleted branch?
If the branch was recently deleted and you know a commit on it, you can recreate it with git checkout -b branch-name <commit-ish> or find the commit in git reflog and then create the branch pointing to that commit.
When should I use git stash?
Use git stash when you need to switch branches but have uncommitted changes you aren’t ready to commit. Apply them later with git stash pop. For longer work, create a temporary branch instead.
Are GUI tools worth using with Git?
Yes, GUIs like GitHub Desktop and Sourcetree help visualize branches and conflicts, which speeds up onboarding and reviews. Use the CLI for scripting and advanced workflows.