Understanding How Git Works: Basics, Branching, Merging, and Collaboration
This article provides a comprehensive introduction to Git, covering its distributed architecture, snapshot‑based model, core commands, branching strategies, merging types, rebasing, cherry‑picking, remote collaboration workflows, and advanced features such as detached HEAD and graphical tools.
Introduction
Git is a widely used distributed version‑control system created by Linus Torvalds to manage the Linux kernel source code. Services like GitHub, IBM DevOps Services, and IBM Rational Team Concert are built on top of Git, making it essential for Linux development and modern DevOps workflows.
Fundamental Concepts
Unlike classic centralized repositories (CVS, SVN), Git treats the repository as a collection of snapshots. Each commit creates a new snapshot linked to its parent, forming a directed graph of versions (branches). The working directory holds the current files, and changes are staged before being committed.
How Git Works
Git records the entire project state as snapshots and tracks changes between snapshots (change sets). Understanding snapshots, commits, and the HEAD pointer is key to using Git effectively.
Basic Commands
git init – initialize a new repository.
git checkout <branch> – switch to a branch.
git add <file> – stage changes.
git commit – create a new snapshot.
git status – show staged and unstaged changes.
git log – view commit history.
git diff – compare changes.
git diff --cached – compare staged changes.
Branching
Branches are pointers to specific commits, allowing parallel development. Common commands include:
git branch <branchname> – create a new branch.
git checkout -b <branchname> – create and switch to a new branch.
git merge <branchname> – merge another branch into the current one.
git merge --abort – abort a conflicted merge.
Merging Types
Git supports fast‑forward merges, non‑conflict merges, and conflict merges. Fast‑forward simply moves the branch pointer; non‑conflict merges create a new merge commit; conflict merges require manual resolution.
Rebasing and Cherry‑Picking
Rebasing rewrites history by moving a series of commits onto a new base:
git rebase <otherbranch>
git rebase -i <otherbranch> – interactive rebasing.
Cherry‑picking applies a specific commit to the current branch:
git cherry-pick <commit>
git cherry-pick --abort – abort a conflicted cherry‑pick.
Reverting
git revert creates a new commit that undoes the changes introduced by a previous commit.
Collaboration
Git’s distributed nature means each developer has a full copy of the repository. Remote workflows use commands such as:
git remote add <origin> <url> – add a remote repository.
git fetch <origin> <branch> – fetch remote changes.
git pull <origin> <branch> – fetch and merge.
git push <origin> <branch> – push local commits.
Pushes are rejected if they would not result in a fast‑forward merge, forcing the user to pull and resolve conflicts first.
Advanced Git
Advanced features include detached HEAD mode, interactive rebasing, and graphical tools like gitk --all for visualizing repository structure. Regular garbage collection ( git gc ) cleans up unreachable commits.
Conclusion
Git’s simple snapshot model provides powerful flexibility for managing code history, branching, merging, and collaboration. Mastering the core commands and understanding remote workflows enables efficient development across teams.
Art of Distributed System Architecture Design
Introductions to large-scale distributed system architectures; insights and knowledge sharing on large-scale internet system architecture; front-end web architecture overviews; practical tips and experiences with PHP, JavaScript, Erlang, C/C++ and other languages in large-scale internet system development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.