Master Git: From Basics to Advanced Branching and Merging Techniques
This article explains Git’s core concepts, file states, commit nodes, HEAD, remote repositories, branching strategies, and essential commands—including add, commit, branch, merge, rebase, cherry‑pick, and rollback—while illustrating each topic with clear diagrams and practical examples.
Basic Concepts
Git is a powerful distributed code management tool. Many users only know clone, commit, pull, and push, but deeper concepts such as rebase, merge, and version rollback are essential for efficient workflows.
Advantages of Git
Unlike centralized systems (e.g., SVN) that require network access for every commit, Git allows local commits, automatic local backup, and full history retrieval, making rollbacks fast and cheap.
File States
Files in Git can be in three states: modified (changed in the working directory), staged (added to the index with git add), and committed (recorded permanently in the repository).
Commit Nodes
Each commit creates a node identified by a SHA‑1 hash. Sequential commits form a linear chain of nodes (ignoring merges). The diagram below shows a simple chain.
HEAD
HEAD is a pointer that always references the current commit (the working directory). It can point directly to a commit or indirectly to a branch, which in turn points to a commit.
Remote Repository
Although Git stores history locally, developers push changes to a remote repository. git clone copies the remote code and all references (branches, HEAD) to the local machine. git fetch updates remote references without merging, while git pull performs a fetch followed by a merge (or rebase).
Branches
A branch is a lightweight pointer to a commit. Multiple branches can coexist, enabling parallel development. Creating a branch does not duplicate code; it merely adds another reference, keeping storage overhead low.
Example: after releasing v1.0, a hot‑fix for v1.0 can be done on a separate branch while development of v1.1 continues on master. The diagram illustrates this workflow.
Command Details
Commit‑related
Add a specific file to the index: git add 文件路径 Add all files: git add . Discard changes in the working directory: git checkout -- 文件名 Clear the index for a file: git reset HEAD 文件名 Create a commit with a message:
git commit -m "该节点的描述信息"Branch‑related
Create a new branch (points to the current HEAD): git branch 分支名 Switch to a branch: git checkout 分支名 Create and switch in one step: git checkout -b 分支名 Delete a branch after it has been merged:
git branch -d 分支名Merge‑related
Merge a branch or commit into the current branch: git merge 分支名/节点哈希值 If the target branch is ahead, a fast‑forward merge occurs; otherwise a new merge commit is created.
Rebase
Reapply commits onto another base, producing a linear history: git rebase 分支名/节点哈希值 Rebase keeps history clean but may require resolving conflicts multiple times.
Cherry‑pick
Select specific commits to apply:
git cherry-pick 节点哈希值Rollback‑related
Detach HEAD to point directly at a commit:
git checkout 节点哈希值 git checkout --detachUse relative references to move HEAD back N commits:
git checkout 分支名/HEAD^ git checkout 分支名~NAmend the most recent commit after fixing issues: git commit --amend Reset to a previous commit (discard later commits):
git reset HEAD~NRemote‑related
Clone a repository: git clone 仓库地址 Fetch updates from the remote without merging: git fetch 远程仓库地址/分支名 Pull (fetch + merge) from a remote branch: git pull 远程分支名 Pull with rebase instead of merge: git pull --rebase 远程分支名 Push local commits to the remote:
git push 远程分支名Conclusion
HEAD and branches are merely references; together with commit nodes they form Git’s distributed nature.
Merge preserves explicit history, while rebase yields a cleaner linear timeline.
Moving HEAD lets you inspect any commit’s code.
Clone and fetch store the entire remote history locally.
Pull is essentially fetch + merge (or rebase).
Original source: https://juejin.cn/post/6895246702614806542
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
