Master Git: From Basics to Advanced Branching and Merging
This comprehensive guide explains Git's core concepts, including its distributed advantages, file states, commit nodes, HEAD, remote repositories, branching strategies, and detailed command usage for committing, merging, rebasing, cherry-picking, resetting, fetching, pulling, and pushing, helping developers work confidently with version control.
Basic Concepts
Git Advantages
Git is a distributed version‑control system. Each developer has a full copy of the repository, so commits can be created locally without network access. This contrasts with centralized systems (e.g., SVN) where every commit must travel through a central server.
File States
Modified : Files changed in the working directory.
Staged : Files added to the index with git add.
Committed : Files stored permanently as a commit object.
Commit Nodes
Every git commit creates a node identified by a SHA‑1 hash. Nodes form a chain (or a directed acyclic graph when merges are present). The diagram below shows a linear chain of three commits.
HEAD
HEAD is a symbolic reference that points to the current commit (or to a branch, which in turn points to a commit). Moving HEAD changes the snapshot of files shown in the working directory.
Remote Repository
Collaboration requires a remote repository. git clone copies the entire history, branches, and references. git fetch updates remote references without altering the working tree.
Branching
What Is a Branch?
A branch is a lightweight pointer to a commit. Multiple branches can coexist, each representing an independent line of development (feature, release, bug‑fix, etc.).
Branch Use Cases
Branches let you develop new features while keeping the stable code on another branch. They also enable isolated bug‑fix work without interfering with ongoing development.
Key Points
Creating a branch does not duplicate code; it only creates a new reference to an existing commit.
Command Details
Commit‑Related Commands
git add <file_path> git add . git checkout -- <file_name> git reset HEAD <file_name> git commit -m "commit message"Branch‑Related Commands
git branch <branch_name> git checkout <branch_name> git checkout -b <branch_name> git branch -d <branch_name>Merge‑Related Commands
git merge <branch_or_commit>Fast‑forward merges occur when the target branch is an ancestor of the current branch; the branch pointer is simply moved forward.
Rebase‑Related Commands
git rebase <branch_or_commit>Rebase rewrites history by moving a series of commits onto a new base, producing a linear sequence.
Cherry‑Pick Command
git cherry-pick <commit_hash>Cherry‑pick copies selected commits onto the current branch without merging the entire branch.
Detach HEAD and Amend
git checkout <commit_hash> git checkout --detach git checkout <branch_name>/HEAD^ git checkout <branch_name>~N git commit --amend git reset HEAD~NRemote‑Related Commands
git clone <repo_url> git fetch <remote> <branch> git pull <remote_branch> git pull --rebase <remote_branch> git push <remote_branch>Advanced Topics
Fast‑Forward vs. Non‑Fast‑Forward Merge
If the branch being merged is strictly ahead of the current branch, git merge results in a fast‑forward (no new commit). Otherwise Git creates a new merge commit that records both histories.
Rebase vs. Merge
Rebase produces a cleaner, linear history but rewrites commit hashes, which can cause conflicts to be resolved multiple times. Merge preserves the true chronological order of commits and is safer for shared branches.
Cherry‑Pick Example
Cherry‑pick allows selective integration of individual commits.
Detached HEAD Workflow
Detaching HEAD lets you inspect or modify an arbitrary commit without affecting any branch. After making changes, git commit --amend can replace the original commit.
Reset for Rollback
git reset HEAD~NThis moves the current branch pointer back N commits, discarding later commits from the branch history.
Conclusion
HEAD and branches are lightweight references; together with commit nodes they form Git’s distributed architecture.
Merge preserves the original chronological history; rebase creates a linear history and is preferred for private branches.
Moving HEAD (including detached HEAD) lets you view or edit any commit. git clone and git fetch bring all remote commits and references into a local repository. git pull is essentially git fetch followed by git merge (or git rebase with --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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
