Git
Git Version Control Guide
A beginner-friendly and experienced-developer-ready Git guide covering daily workflow, branches, merging, rebasing, recovery, collaboration, and practice questions.
Beginner FriendlyVersion ControlCommandsCollaborationPractice Questions
Published: Sep 2019 Updated: May 2026
Quick Summary
- Git is: a distributed version control system.
- Main use: track code changes, collaborate, branch, review, merge, and recover history.
- Beginner goal: understand working tree, staging area, commits, branches, and remotes.
- Experienced goal: use clean history, safe recovery, conflict handling, and team workflows.
- Best habit: run
git statusoften and review diffs before committing.
Git Mental Model
| 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 |
Daily Workflow
- Update your main branch:
git switch mainthengit pull --ff-only. - Create a feature branch:
git switch -c feature/login. - Edit files and inspect changes with
git statusandgit diff. - Stage only intended changes with
git add file. - Review staged content with
git diff --staged. - Commit with a useful message:
git commit -m "Add login form validation". - Push and open a pull request:
git push -u origin feature/login.
Important Commands Map
| 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. |
Branching and Collaboration
- Create short-lived branches for focused work.
- Name branches by purpose, such as
feature/payment-filterorfix/login-error. - Use pull requests for review, test checks, and discussion.
- Keep main branch protected with required reviews and CI checks.
- Delete merged branches to keep repository references clean.
Merge, Rebase, and Squash
| 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. |
Rule: do not rebase shared commits that other developers already use unless the team coordinates it.
Undo and Recovery
| 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. |
Conflict Handling
- Run
git statusto find conflicted files. - Open each file and read both sides of the conflict.
- Choose the final correct behavior, not just the text you wrote.
- Remove conflict markers and run tests or manual checks.
- Stage resolved files with
git add. - Continue merge or rebase with the command Git suggests.
Advanced Tools
git bisect: binary search to find the commit that introduced a bug.git worktree: check out multiple branches without cloning again.git cherry-pick: apply one selected commit to the current branch.git tag -a: mark releases with metadata.git range-diff: compare two versions of a patch series after rebase.git sparse-checkout: work with only part of a large repository.
25 Most Important Practice Questions
1. What is Git?
Answer: A distributed version control system for tracking file history and collaboration.
2. What is a commit?
Answer: A snapshot of staged changes with metadata and parent links.
3. What is the staging area?
Answer: The index containing changes selected for the next commit.
4. What does git status show?
Answer: Branch state, staged changes, unstaged changes, and untracked files.
5. What is a branch?
Answer: A movable pointer to a commit.
6. What is HEAD?
Answer: Pointer to the current branch or checked-out commit.
7. What is origin?
Answer: The default name for the remote repository cloned from.
8. Fetch vs pull?
Answer: Fetch downloads remote refs; pull fetches and integrates by merge or rebase.
9. Merge vs rebase?
Answer: Merge joins histories; rebase replays commits on a new base.
10. When should you avoid rebase?
Answer: Avoid rebasing shared commits others depend on.
11. What is a fast-forward merge?
Answer: Moving the branch pointer forward when no divergent history exists.
12. What is a merge conflict?
Answer: A situation where Git cannot automatically combine changes.
13. Safest way to undo a pushed commit?
Answer: Use
git revert.14. What does git reset --soft do?
Answer: Moves branch pointer but keeps changes staged.
15. What does git restore do?
Answer: Restores files or unstages changes depending on options.
16. What is git stash?
Answer: Temporarily stores uncommitted work.
17. What is git reflog?
Answer: A log of HEAD and branch reference movements.
18. What is cherry-pick?
Answer: Applying a selected commit onto the current branch.
19. What is bisect?
Answer: Binary search through history to find a bad commit.
20. What is a tag?
Answer: A named reference often used for releases.
21. What is GitHub Flow?
Answer: Main branch plus short-lived feature branches merged by pull request.
22. What is trunk-based development?
Answer: Frequent small merges to the main branch, often behind feature flags.
23. Why write small commits?
Answer: Easier review, revert, bisect, and understand history.
24. Why avoid force push on shared branches?
Answer: It can rewrite history others are using.
25. Best habit before committing?
Answer: Review
git diff and git diff --staged.