Introduction
Git: distributed version control system. Created by Linus Torvalds in 2005 for Linux kernel development. Tracks changes, enables collaboration, preserves history. Core to modern software engineering. Lightweight, fast, scalable.
"Git is a tool that every developer should master for efficient source code management." -- Scott Chacon
Version Control Concepts
Definition
Version control: management of changes to documents, programs, or information. Tracks modifications, enables rollback, branches, merges.
Types
Centralized (e.g., SVN): single server repository. Distributed (e.g., Git): every user has full copy.
Benefits
History tracking, collaboration, parallel development, conflict resolution, audit trails.
Git Architecture
Data Model
Snapshot-based: stores file tree snapshots, not diffs. Each commit points to a tree object representing file states.
Objects
Four object types: blob (file content), tree (directory), commit (metadata + tree), tag (annotated reference).
References
Branches and tags are refs pointing to commits. HEAD points to current branch or commit.
Storage
Objects stored in .git/objects as compressed files. Index tracks staged changes.
Repositories
Local Repository
Full copy of project, including history. Created via git init or git clone.
Remote Repository
Shared central repository for collaboration. Hosted on servers (GitHub, GitLab, Bitbucket).
Working Directory
Checked-out files for editing. Changes staged before committing.
Repository Structure
.git folder contains metadata, objects, refs, config.
Commits
Definition
Commit: immutable snapshot of project state with metadata (author, date, message).
Commit Hash
SHA-1 hash uniquely identifies commit. Ensures integrity.
Creating Commits
Stage changes with git add, then commit with git commit.
Commit Messages
Brief, descriptive, imperative mood recommended.
Branching
Concept
Branch: movable pointer to a commit. Enables parallel development.
Creating Branches
git branch <name> creates branch. git checkout <name> switches.
Branching Model
Common models: Git Flow, GitHub Flow, trunk-based development.
Branch Lifecycle
Create, develop, merge, delete.
Merging
Purpose
Combine changes from different branches into one.
Types
Fast-forward: branch linear, no new commit. Recursive: creates merge commit.
Conflict Resolution
Conflicts: overlapping changes. Resolved manually or with tools.
Merge Strategies
Recursive, octopus, ours, theirs.
Working with Remote Repositories
Remote Setup
git remote add <name> <url> links local repo to remote.
Fetching and Pulling
git fetch downloads changes. git pull fetches + merges automatically.
Pushing
git push uploads local commits to remote branches.
Tracking Branches
Local branches can track remote branches for synchronization.
Common Git Commands
Initialization & Cloning
git init, git clone <repo_url>
Staging & Committing
git add <file>, git commit -m "message"
Branching & Merging
git branch, git checkout, git merge
Remote Operations
git remote, git fetch, git pull, git push
Status & Logs
git status, git log, git diff
| Command | Description |
|---|---|
| git init | Create new local repository |
| git clone <url> | Clone remote repository |
| git add <file> | Stage changes for commit |
| git commit -m "msg" | Create commit with message |
| git push | Upload changes to remote |
Workflows
Centralized Workflow
Single shared branch. Simple but limited concurrency.
Feature Branch Workflow
Isolated feature branches merged back to main. Supports parallel work.
Git Flow
Defines strict roles for branches: master, develop, feature, release, hotfix.
Forking Workflow
Fork repos for independent development, submit pull requests.
Best Practices
Commit Often
Small, focused commits improve history clarity.
Write Clear Messages
Use imperative mood, summarize intent, reference issues.
Use Branches
Isolate features or fixes to avoid conflicts.
Regularly Sync with Remote
Fetch and merge frequently to reduce conflicts.
Review Before Merge
Code reviews maintain quality and knowledge sharing.
Troubleshooting
Merge Conflicts
Detected when overlapping changes occur. Resolve by editing conflicted files and committing resolutions.
Detached HEAD
HEAD points to a commit, not a branch. To fix, checkout a branch or create a new one.
Undoing Changes
git checkout to discard local changes; git reset to unstage; git revert to create new commit undoing changes.
Recovering Lost Commits
git reflog tracks HEAD moves; can recover commits using reflog hashes.
git refloggit checkout <commit_hash>git branch recovered-branchReferences
- S. Chacon, B. Straub, Pro Git, Apress, 2014, pp. 1-500.
- J. Loeliger, M. McCullough, Version Control with Git, O'Reilly Media, 2012, pp. 1-400.
- L. Torvalds, Git Source Code Management, Linux Foundation, 2005.
- M. Duvall, A. Matyas, A. Glover, Continuous Integration: Improving Software Quality and Reducing Risk, Addison-Wesley, 2007, pp. 1-350.
- K. Beck, Test Driven Development: By Example, Addison-Wesley, 2003, pp. 1-240.