Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools
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 status often and review diffs before committing.

Git Mental Model

PartMeaningCommand to inspect
Working treeFiles you are editing now.git status
Staging area / indexChanges selected for the next commit.git diff --staged
CommitA snapshot with message, author, date, and parent links.git show
BranchA movable pointer to a commit.git branch
HEADYour current branch or checked-out commit.git status
RemoteAnother repository such as origin.git remote -v

Daily Workflow

  1. Update your main branch: git switch main then git pull --ff-only.
  2. Create a feature branch: git switch -c feature/login.
  3. Edit files and inspect changes with git status and git diff.
  4. Stage only intended changes with git add file.
  5. Review staged content with git diff --staged.
  6. Commit with a useful message: git commit -m "Add login form validation".
  7. Push and open a pull request: git push -u origin feature/login.

Important Commands Map

GoalCommandPoint
Start repositorygit initCreate local Git history.
Clone repositorygit clone <url>Copy remote repository locally.
Check stategit statusShows branch, staged, unstaged, untracked files.
Stage changesgit addMove selected changes into the index.
Commitgit commitCreate a snapshot.
Fetchgit fetchDownload remote refs without merging.
Pullgit pullFetch plus merge or rebase.
Pushgit pushUpload commits to remote.

Branching and Collaboration

  • Create short-lived branches for focused work.
  • Name branches by purpose, such as feature/payment-filter or fix/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

ActionWhat it doesUse when
MergeCombines histories and may create a merge commit.You want to preserve branch history.
RebaseReplays commits on a new base.You want to clean private feature branch history.
Squash mergeCombines 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

NeedCommandSafe use
Unstage filegit restore --staged fileRemoves file from staging only.
Discard local editsgit restore fileDeletes unstaged edits intentionally.
Undo pushed commitgit revert <commit>Creates a new inverse commit.
Recover lost pointergit reflogFinds previous HEAD/branch positions.
Temporarily save workgit stash push -m "message"Shelves uncommitted changes.

Conflict Handling

  1. Run git status to find conflicted files.
  2. Open each file and read both sides of the conflict.
  3. Choose the final correct behavior, not just the text you wrote.
  4. Remove conflict markers and run tests or manual checks.
  5. Stage resolved files with git add.
  6. 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.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.