Master Git: From Basics to Advanced Branching and Merging Techniques
This comprehensive guide demystifies Git by explaining its core concepts—file states, commits, HEAD, remote repositories, and branching—and provides detailed command-line instructions for adding, committing, resetting, merging, rebasing, cherry-picking, fetching, pulling, and pushing, enabling developers to master version control workflows.
Preface
Git is the most powerful code management tool today, yet many people only use clone, commit, pull, and push, avoiding rebase and relying on merge. This article shares a deep understanding of Git, explaining its fundamentals and helping readers use Git commands confidently.
Basic Concepts
1. Advantages of Git
Git is a distributed version control system, unlike centralized systems such as SVN that require network access for every commit. In Git, commits are local, cheap, and can be rolled back at any time.
Centralized: all code lives on a central server; each commit must access the network and merges are frequent. Distributed: commits are local, automatically backed up, and each developer can clone the entire repository with its history.
2. File States
Modified: Git detects files changed in the working directory and marks them as modified.
Staged: git add moves modified files to the staging area, awaiting commit.
Committed: git commit records staged files permanently in the repository.
3. Commit Nodes
Each commit creates a node identified by a SHA‑1 hash. A linear chain of commits forms the project history (ignoring merges).
The node’s hash is calculated from its contents; later nodes contain earlier nodes’ changes.
4. HEAD
HEAD is a pointer that references the current commit (the working directory). It can also point to a branch, which in turn points to a commit.
5. Remote Repository
Although commits are stored locally, they are eventually pushed to a remote repository. git clone copies the remote code and its history; git fetch updates remote references; git pull fetches and merges changes.
Branches
1. What Is a Branch?
A branch is a lightweight pointer to a commit. Multiple branches can exist simultaneously, allowing parallel development.
2. Using Branches
Branches enable scenarios such as fixing bugs on a released version while continuing development on the next version. By creating a separate branch for the bug fix, developers can merge the fix back without disturbing ongoing work.
Command Details
1. Commit‑Related Commands
git add <path>adds a file to the staging area; git add . adds all files.
git add file_path git add .To discard changes: git checkout -- filename To unstage a file: git reset HEAD filename To commit:
git commit -m "commit message"2. Branch‑Related Commands
Create a branch (HEAD and the new branch point to the same commit): 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 it has been merged:
git branch -d branch_name3. Merge‑Related Commands
Merge integrates changes from another branch or commit: git merge branch_name_or_hash Fast‑forward merges occur when the target branch is ahead of the current branch.
When histories diverge, a new merge commit is created.
4. Rebase
Rebase reapplies commits onto another base, producing a linear history:
git rebase branch_name_or_hash5. Cherry‑Pick
Select specific commits to apply:
git cherry-pick commit_hash6. HEAD Detachment and Reset
Detach HEAD to point directly at a commit:
git checkout commit_hash
# or
git checkout --detachMove HEAD relative to its current position:
git checkout branch_name/HEAD^
# or
git checkout branch_name~NAmend the most recent commit after fixing issues: git commit --amend Reset to a previous commit (moves both branch and HEAD):
git reset HEAD~N7. Remote‑Related Commands
Clone a repository: git clone repository_url Fetch updates from the remote without merging: git fetch remote_name/branch_name Pull (fetch + merge) from the remote: git pull remote_branch_name Pull with rebase: git pull --rebase remote_branch_name Push local commits to the remote:
git push remote_name/branch_nameConclusion
HEAD and branches are lightweight references; together with commits they form Git’s distributed model.
Merge preserves explicit history; rebase creates a cleaner, linear history and is preferred when possible.
Moving HEAD lets you inspect any commit.
Clone and fetch bring the full remote history to the local machine.
Pull is essentially fetch followed by merge (or 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.
