A practical Git guide for beginners who want a clear mental model and professionals who need safer daily workflows, cleaner commits, better collaboration, recovery steps, and interview-ready explanations.
git status often and review diffs before committing.If you are new to Git, start by understanding the journey of a change: you edit a file, stage the change, commit it, and push it to a remote repository. Do not try to memorize every command on day one. Learn the small daily loop first.
If you already use Git at work, focus on the professional habits: keep commits focused, read diffs before committing, avoid rewriting shared history, write useful messages, and use recovery commands carefully. These habits matter more than knowing obscure commands.
| Part | Meaning | Command to inspect |
|---|---|---|
| Working tree | Files you are editing now. | git status |
| Staging area / index | Changes selected for the next commit. | git diff --staged |
| Commit | A snapshot with message, author, date, and parent links. | git show |
| Branch | A movable pointer to a commit. | git branch |
| HEAD | Your current branch or checked-out commit. | git status |
| Remote | Another repository such as origin. | git remote -v |
git switch main then git pull --ff-only.git switch -c feature/login.git status and git diff.git add file.git diff --staged.git commit -m "Add login form validation".git push -u origin feature/login.git config --global user.name "Asha Rao"
git config --global user.email "asha@example.com"
git config --global init.defaultBranch main
git config --list --show-origin
The name and email become commit metadata. Use an address approved by your organization or a hosting-provider privacy address when needed; never put passwords or access tokens in Git configuration committed to a repository.
git remote add origin git@github.com:example/acme-app.git
git remote -v
git push -u origin main
# Change an existing remote URL later
git remote set-url origin git@github.com:example/new-name.git
The -u option records the upstream so later git pull and git push know which remote branch to use. A remote is a named URL, not a separate copy magically kept in sync; use git fetch --prune to refresh remote-tracking references.
/vendor/
/node_modules/
/dist/
.env
.env.*
!.env.example
*.log
.idea/
.vscode/
.gitignore affects untracked files; it does not untrack a secret or build output already committed. Remove an intended tracked file from the index with git rm --cached path. If a secret reached history, revoke or rotate it immediately—merely deleting the file is not enough.For authoritative command behavior and version-specific options, use the official Git reference.
| Goal | Command | Point |
|---|---|---|
| Start repository | git init | Create local Git history. |
| Clone repository | git clone <url> | Copy remote repository locally. |
| Check state | git status | Shows branch, staged, unstaged, untracked files. |
| Stage changes | git add | Move selected changes into the index. |
| Commit | git commit | Create a snapshot. |
| Fetch | git fetch | Download remote refs without merging. |
| Pull | git pull | Fetch plus merge or rebase. |
| Push | git push | Upload commits to remote. |
feature/payment-filter or fix/login-error.| Action | What it does | Use when |
|---|---|---|
| Merge | Combines histories and may create a merge commit. | You want to preserve branch history. |
| Rebase | Replays commits on a new base. | You want to clean private feature branch history. |
| Squash merge | Combines feature branch into one target-branch commit. | Feature commits are noisy and main should stay concise. |
git fetch origin
git rebase origin/main
git push --force-with-lease origin feature/login
--force-with-lease refuses to replace the remote branch when it has moved beyond the remote-tracking state you expected. That protects against accidentally erasing a teammate's newly pushed commit. It still rewrites history, so reserve it for your own coordinated feature branch; use git revert on protected or shared branches.
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
git config --global commit.gpgsign true
git commit -m "Fix login validation"
git log --show-signature -1
Signing lets reviewers verify that a commit was signed by a configured key; it does not prove the code is safe. Register the public signing key with your Git hosting provider and follow your team's key-storage and rotation policy.
| Need | Command | Safe use |
|---|---|---|
| Unstage file | git restore --staged file | Removes file from staging only. |
| Discard local edits | git restore file | Deletes unstaged edits intentionally. |
| Undo pushed commit | git revert <commit> | Creates a new inverse commit. |
| Recover lost pointer | git reflog | Finds previous HEAD/branch positions. |
| Temporarily save work | git stash push -m "message" | Shelves uncommitted changes. |
git status to find conflicted files.git add.git worktree add ../acme-hotfix -b hotfix/login main
cd ../acme-hotfix
# edit, test, commit, and push the hotfix
git push -u origin hotfix/login
# Run from the original repository after finishing
git worktree remove ../acme-hotfix
A worktree gives another branch its own directory while sharing the repository's object database. This is useful for an urgent fix when the primary directory contains unfinished work.
git bisect start
git bisect bad
git bisect good v2.4.0
# Test the checked-out midpoint, then mark it:
git bisect good # or: git bisect bad
git bisect reset
Each good/bad answer halves the remaining search range. If one command reliably returns success for good commits and failure for bad ones, automate the test with git bisect run <test-command>.
git cherry-pick: apply one selected commit to the current branch.git tag -a: create an annotated release tag.git range-diff: compare two versions of a patch series after rebase.git sparse-checkout: work with only part of a large repository.git status show?git revert.git reset --soft do?git restore do?git stash?git reflog?git diff and git diff --staged.Explore 500+ free tutorials across 20+ languages and frameworks.