Comprehensive Guide to Git: Core Concepts, Branches, Commands, and Best Practices
This article provides an in‑depth tutorial on Git, covering its advantages over centralized systems, file states, commit nodes, HEAD, remote repositories, branching strategies, detailed command usage for committing, branching, merging, rebasing, cherry‑picking, and rollback, as well as practical tips for effective version control.
Introduction
Git is the most powerful distributed code management tool, yet many developers only use basic commands like clone, commit, pull, and push, avoiding rebase and merge. This guide explains Git from the ground up, helping readers understand its underlying principles and use advanced commands confidently.
Table of Contents
1. Basic Concepts 1.1 Advantages of Git 1.2 File States 1.3 Commit Nodes 1.4 HEAD 1.5 Remote Repository
2. Branches
3. Command Details 3.1 Commit‑related 3.2 Branch‑related 3.3 Merge‑related 3.4 Rollback‑related 3.5 Remote‑related
1. Basic Concepts
1.1 Advantages of Git
Git is a distributed version control system, unlike centralized systems (e.g., SVN) that require network access for every commit. With Git, commits are local, each developer can clone the entire repository, and history is stored locally, enabling fast, offline operations.
1.2 File States
Files in Git can be in three states:
Modified : changes detected in the working directory.
Staged : added to the index via git add .
Committed : permanently recorded in the repository.
1.3 Commit Nodes
Each commit creates a node identified by a SHA‑1 hash. Nodes form a linear chain (ignoring merges), as illustrated in the accompanying diagram.
1.4 HEAD
HEAD is a pointer that references the current commit (or branch). It determines the state of the working directory. HEAD can point directly to a commit (detached HEAD) or to a branch, which in turn points to a commit.
1.5 Remote Repository
Although Git stores history locally, collaboration requires a remote repository. git clone copies the code and all references (branches, HEAD) to the local machine. Remote references are not updated automatically; git fetch must be used to synchronize them.
Note
Always ensure code quality before pushing to the remote repository to avoid introducing untested changes.
2. Branches
2.1 What Is a Branch?
A branch is a lightweight pointer to a commit. Multiple branches can exist simultaneously, allowing parallel development streams. Branches enable isolated feature development, bug fixes, and version management.
Example: After releasing v1.0, a new v1.1 is started, but a critical bug in v1.0 requires a hot‑fix. By creating a branch from the v1.0 commit, developers can fix the bug without interfering with ongoing v1.1 work.
Note
Creating a branch does not duplicate code; it merely creates another reference, keeping storage overhead minimal.
3. Command Details
3.1 Commit‑related Commands
Add a specific file to the staging area: git add file_path
Add all files: git add .
Discard changes in the working directory: git checkout -- filename
Clear a file from the staging area: git reset HEAD filename
Create a commit with a message: git commit -m "commit description"
3.2 Branch‑related Commands
Create a branch: git branch branch_name
Switch to a branch: git checkout branch_name
Create and switch in one step: git checkout -b branch_name
Delete a branch: git branch -d branch_name
3.3 Merge‑related Commands
Merge a branch or commit: git merge branch_name_or_commit_hash
Rebase onto another branch: git rebase branch_name_or_commit_hash
Cherry‑pick specific commits: git cherry-pick commit_hash
Merge creates a new commit (a merge node) when histories diverge, while rebase rewrites history to produce a linear sequence. Cherry‑pick copies selected commits without merging the entire branch.
Note
Rebase results in a cleaner, linear history but may require resolving conflicts multiple times if many commits are rebased.
3.4 Rollback‑related Commands
Detach HEAD to a specific commit: git checkout commit_hash git checkout --detach
Move HEAD relative to a branch: git checkout branch_name/HEAD^ // previous commit git checkout branch_name~N // N commits back
Amend the most recent commit (useful after fixing a mistake while HEAD is detached): git commit --amend
Reset the current branch to an earlier state: git reset HEAD~N // go back N commits
3.5 Remote‑related Commands
Clone a repository: git clone repository_url
Fetch updates from the remote without merging: git fetch remote_url/branch_name
Pull (fetch + merge) from a remote branch: git pull remote_branch_name
Pull with rebase instead of merge: git pull --rebase remote_branch_name
Push local commits to the remote: git push remote_branch_name
Push may fail if the remote has diverged; a preceding git pull (or git pull --rebase ) resolves conflicts before pushing.
Conclusion
Both HEAD and branches are merely references; together with commit nodes they form Git's distributed architecture.
Merge preserves explicit history order, while rebase yields a linear, cleaner log and is preferred when possible.
Moving HEAD lets you inspect any commit's code state.
Cloning or fetching stores the entire remote history locally.
git pull is essentially fetch followed by merge , optionally using --rebase for a linear integration.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.