Knowledge / Computer Science and Engineering / Git and version control

Git and version control

Computer Science and Engineering (B.E./B.Tech) · Software engineering · free study notes for exams, placements and interviews

In short

Version control is the practice of recording every change to a set of files so that any past state can be recovered, compared and attributed. Git is a distributed version control system (created by Linus Torvalds in 2005 for the Linux kernel) in which every developer holds a complete copy of the repository and its history, so commits, branches, diffs and log searches work offline. It is the backbone of the software engineering lab, the mini-project and the final-year project in Indian universities, and it is assumed knowledge in almost every IT interview — TCS, Infosys, Wipro, Cognizant, Accenture, Tech Mahindra and every product company expect fluency with branch, merge, conflict resolution and pull requests. GATE CS does not ask Git commands directly, but university papers ask the concepts (centralized vs distributed, merge vs rebase, repository structure) and viva/placement rounds ask the commands and the recovery scenarios. Day one of an Indian internship typically means cloning from an internal GitLab, Bitbucket or Azure DevOps server and raising your first pull request.

Explained

Why version control, and the two architectures

How Git stores data — the object model

The three areas and the basic workflow

Branching, merging and rebasing

Collaboration models, branching strategies and tooling

Key points

Formulas and examples

Object id = SHA-1( type + " " + size + "\0" + content ) - 160 bits = 40 hexadecimal characters; git hash-object computes it.
Ancestry: HEAD~n = nth ancestor along *first parents*; HEAD^n = the nth parent of a merge commit. So HEAD~2 = grandparent, HEAD^2 = second parent of a merge.
Commits in a range: git rev-list --count A..B = commits reachable from B but not from A.
Bisect tests needed = ceil( log₂ N ), where N = number of suspect commits.
Abbreviated-hash space: k hex characters → 16k distinct prefixes; collisions become likely around √(16k) objects (birthday bound).
Semantic version = MAJOR.MINOR.PATCH (+ optional -pre.1, +build).

Example 1 - Bisecting a regression. A bug exists at main but not at tag v1.4; git rev-list --count v1.4..main returns 1,000.

Example 2 - Merge vs rebase, counted. Your feature/upi has 4 commits; main has advanced by 6 commits since you branched.

Commits added to mainCommits rewrittenResulting shape
git merge feature/upi (no-ff)4 + 1 merge = 502 lines joining at a merge commit
git rebase main then fast-forward4 (new ids)4single straight line
Squash-merge14 collapsedone commit per feature

Example 3 - Undoing safely. You pushed 3 bad commits to a shared release branch.

Example 4 - Recovery with reflog. You ran git reset --hard HEAD~5 on your own branch by mistake.

git reflog                 # e.g. ab12cd3 HEAD@{1}: commit: add OTP retry
git branch rescue ab12cd3  # or: git reset --hard HEAD@{1}

Example 5 - Resolving a conflict.

$ git merge feature/pay
CONFLICT (content): Merge conflict in src/pay.java
Automatic merge failed; fix conflicts and then commit the result.
  1. git status → both modified: src/pay.java; 2. open the file, keep the correct logic, delete all three markers; 3. git add src/pay.java; 4. git commit (default message Merge branch 'feature/pay'); 5. re-run the tests — a textually clean merge can still be semantically broken. git merge --abort returns to the pre-merge state at any point before step 4.

Abbreviation check. 7 hex characters give 16⁷ = 268,435,456 prefixes, so collisions become likely around √(2.68×10⁸) ≈ 16,400 objects; a repository with a million objects will certainly collide at 7 characters, which is why Git's core.abbrev auto lengthens the prefix (commonly 10-12) in large repos.

GitSVN (centralized)
History locationEvery cloneServer only
Offline commit/log/diffYesNo
Branch costA pointer, instantServer-side copy, heavier
Revision idSHA-1 content hashIncrementing integer
Merge trackingNative, cheapAdded later, weaker
Partial checkout of a subtreeAwkward (sparse-checkout)Natural
Large binariesWeak (needs LFS)Better, with locking

Where it is used

Interview questions

Common mistakes

Related topics

SDLC and Agile · Clean code and code reviews · Test automation and TDD · Software project management and estimation · Software architecture: monoliths and microservices · Operating systems: file systems and hashing

Open in the Knowledge reader →Find jobs →