Fundamentals 16 min read

Master Git: From Basics to Advanced Branching, Merging, and Rebase

This comprehensive guide explains Git's core concepts—including advantages, file states, commits, HEAD, and remote repositories—covers branching strategies, and provides detailed command references for adding, committing, resetting, checking out, merging, rebasing, cherry‑picking, and synchronizing with remote servers.

Open Source Linux
Open Source Linux
Open Source Linux
Master Git: From Basics to Advanced Branching, Merging, and Rebase

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 Revert‑related

3.5 Remote‑related

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, Git allows local commits and automatically backs them up, enabling faster, more flexible development.

1.2 File States

Modified : Files changed in the working directory.

Staged : Files added to the index with git add.

Committed : Files 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). See the diagram:

1.4 HEAD

HEAD is a reference that points to the current commit (or branch). It determines the state of the working directory.

1.5 Remote Repository

While commits are stored locally, they can be pushed to a remote repository. git clone copies the code and all references (branches, HEAD) to the local machine.

Always ensure the remote repository contains tested code before pushing.

2. Branches

A branch is a lightweight pointer to a commit. Multiple branches can coexist, allowing parallel development. Creating a branch does not duplicate code; it merely creates a new reference.

Branches enable isolated development of features or bug fixes. After merging a feature branch, delete it to keep the repository tidy.

3. Command Details

3.1 Commit‑related

git add <file_path>
git add .
git checkout -- <file_name>
git reset HEAD <file_name>
git commit -m "commit message"

3.2 Branch‑related

git branch <branch_name>
git checkout <branch_name>
git checkout -b <branch_name>
git branch -d <branch_name>

3.3 Merge‑related

git merge <branch_or_hash>

Fast‑forward merges occur when the target branch is ahead of the current branch.

When histories diverge, Git creates a new merge commit.

If the same line is edited in both branches, a conflict must be resolved manually.

3.4 Rebase‑related

git rebase <branch_or_hash>

Rebase rewrites history to produce a linear sequence of commits, making the log cleaner.

Pros: Linear, tidy history. Cons: May require resolving the same conflict multiple times.

3.5 Cherry‑pick

git cherry-pick <commit_hash>

Cherry‑pick copies selected commits onto the current branch.

Remote‑related Commands

git clone <repo_url>
git fetch <remote> [<branch>]
git pull <remote> <branch>
git pull --rebase <remote> <branch>
git push <remote> <branch>

Pull is essentially fetch followed by merge (or rebase when using --rebase).

Conclusion

HEAD and branches are lightweight references that enable Git’s distributed nature.

Merge preserves chronological history; rebase creates a linear history and is preferred when possible.

Moving HEAD lets you inspect any commit.

Clone and fetch bring remote commits and references into the local repository.

Pull = fetch + merge (or rebase).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GitmergerebaseVersion Controlremotebranchingcommit
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.