Master Git: From Basics to Advanced Branching and Merging Techniques
This comprehensive guide explains Git fundamentals—including its distributed nature, file states, commit nodes, HEAD, remote repositories, branching concepts, and detailed command usage such as add, commit, reset, checkout, merge, rebase, cherry-pick, fetch, pull, and push—helping developers confidently manage version control workflows.
Preface
Git is the most powerful code management tool, yet many users only perform clone, commit, pull, and push, avoiding rebase and advanced features.
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 stored locally, enabling offline work and easy cloning of the entire history.
Centralized: all code resides on a central server; each commit requires network access and may cause frequent merges. Distributed: commits are local, each developer can clone the full repository with its history.
Git’s low‑cost commits make rollback simple and elegant.
1.2 File Status
Files in Git can be in three states: modified , staged , and committed .
Modified : Git detects changes in the working directory.
Staged : git add moves changes to the staging area.
Committed : git commit records the staged changes permanently.
1.3 Commit Nodes
Each commit creates a node identified by a SHA‑1 hash, forming a linear chain of nodes (ignoring merges).
The hash appears above each node.
1.4 HEAD
HEAD is a pointer that references the current commit (the working directory). It can also point to a branch, indirectly referencing the branch’s tip.
1.5 Remote Repository
While commits are stored locally, they are eventually pushed to a remote repository. git clone copies the code and its history, but remote references are not updated automatically; they must be fetched manually.
Important Note
Always ensure code quality before pushing to the remote repository.
Branching
2.1 What Is a Branch?
A branch is a pointer to a commit, allowing multiple lines of development. Creating a branch does not duplicate code; it merely adds another reference.
Example: after releasing v1.0, a hot‑fix for v1.0 can be done on a separate branch while v1.1 development continues on master.
Important Note
Branches are lightweight references; they do not copy files.
Command Details
3.1 Commit‑Related Commands
Add files to the staging area: git add <file_path> Add all files: git add . Undo changes in the working directory: git checkout -- <file_name> Clear the staging area: git reset HEAD <file_name> Commit staged changes:
git commit -m "commit message"3.2 Branch‑Related Commands
Create a branch (points to the current HEAD): 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 after its work is merged:
git branch -d <branch_name>3.3 Merge‑Related Commands
Merge a branch or commit into the current branch: git merge <branch_name_or_hash> Fast‑forward merge occurs when the target branch is ahead of the current one.
If both branches modify the same line, a manual conflict resolution is required.
3.4 Rebase
Rebase applies commits onto another base, producing a linear history: git rebase <branch_name_or_hash> Rebase creates new commits (different hashes) but results in a cleaner history.
Pros: linear, tidy history. Cons: may require resolving conflicts multiple times.
3.5 Cherry‑Pick
Select specific commits to apply:
git cherry-pick <commit_hash>3.6 HEAD Detachment and Reset
Detach HEAD to point directly at a commit:
git checkout <commit_hash>
# or
git checkout --detachUse relative references to move HEAD:
git checkout <branch>/HEAD^
# or
git checkout <branch>~NAmend the most recent commit after detaching: git commit --amend Reset to a previous commit (moves both branch and HEAD):
git reset HEAD~N3.7 Remote‑Related Commands
Clone a repository: git clone <repo_url> Fetch updates from the remote without merging: git fetch <remote> <branch> Pull (fetch + merge) updates the current branch: git pull <remote_branch> Pull with rebase: git pull --rebase <remote_branch> Push local commits to the remote:
git push <remote_branch>Conclusion
HEAD and branches are lightweight references; together with commit nodes they form Git’s distributed model.
Merge preserves explicit history, while rebase yields a cleaner linear history and is preferred when possible.
Moving HEAD lets you inspect any commit’s code.
Clone and fetch store the full remote history locally.
Pull is essentially fetch + merge, optionally using rebase.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
