Essential Git commands for initializing, managing, and collaborating on repositories.
┌──────────────────────────┐
│ git init │
│ Create a repository │
└────────────┬─────────────┘
│
▼
┌──────────────────┐
│ git add . │
│ git commit -m ""│
│ Track changes │
└────────┬─────────┘
│
▼
┌──────────────────┐
│ git branch / │
│ git switch │
│ Manage branches │
└────────┬─────────┘
│
▼
┌──────────────────┐
│ git push / pull │
│ Sync with remote │
└────────┬─────────┘
│
▼
┌──────────────────┐
│ git merge │
│ Integrate changes│
└──────────────────┘
💡 This flow captures the typical Git lifecycle: initialize → track → branch → sync → merge.
git init— Initialize a new Git repository in the current directory.git clone <url>— Create a local copy of a remote repository.💡 Tip: Run
git statusright after initializing or cloning to check your repo’s state.
git add <file>— Stage changes for the next commit.git add .— Stage all modified files in the current directory.
git commit -m "commit message"— Save staged changes as a new commit.git status— Show the current state of the working directory and staging area.🧠 Use concise, descriptive commit messages to maintain a clean history.
git remote add origin <url>— Connect your local repository to a remote host.git push— Upload local commits to the remote repository.git pull— Fetch and merge changes from the remote repository.⚡ Run
git pull --rebasefor cleaner, linear commit histories in personal projects.
git branch— List all local branches.git branch <branch-name>— Create a new branch.
git switch <branch-name>— Switch to another branch (recommended overcheckout).git merge <branch-name>— Merge the specified branch into the current one.git branch -d <branch-name>— Delete the local branch (safe). Only deletes if the branch has been fully merged into its upstream.- Use
git branch -D <branch-name>to forcefully delete the branch, even if it has unmerged changes.🪄 Keep your
mainbranch stable; use feature branches for development and experimentation.
- Use
git log— Display the commit history of the repository.git log --oneline --graph --decorate— Show a simplified visual history.
git diff— Compare working directory changes with the last commit.
git restore <file>— Revert unstaged changes in a file.git reset <file>— Unstage a file without discarding changes.git checkout -- <file>— (Legacy) Restore file to last committed state.🧩 Learn the difference between
reset,restore, andrevertin the Advanced guide.
These examples combine basic commands into simple, real-world sequences you’ll use often.
git init
git add .
git commit -m "chore: initial project setup"
git branch -M main
git remote add origin <url>
git push -u origin maingit switch -c feat/new-feature
# make changes
git add .
git commit -m "feat: implement new feature"
git push -u origin feat/new-featuregit switch main
git pull --rebasegit merge feat/new-feature
git branch -d feat/new-feature
git push origin --delete feat/new-feature🧠 These workflows provide a solid foundation before exploring Advanced Git Commands like
rebase,stash, andcherry-pick.
Explore related Git documentation:
- 🧩 Prefixes — Commit and branch naming conventions
- ⚙️ Advanced — Advanced Git commands and workflows
- 🚀 Modern — Modern Git features and tools
- 🔧 Troubleshooting — Common problems and solutions
- ⚙️ Config — Useful aliases and configuration examples
- 📋 Cheat Sheet — Quick command reference
- 💡 Tips — Practical tips and best practices
- 🔗 Resources — External references and learning materials
This guide serves as a quick reference for essential Git commands to keep your workflow efficient and organized.