Master Git Basics: Add, Commit, Checkout, Reset, Merge & Rebase Explained
This guide walks through essential Git commands—including add, commit, reset, checkout, merge, cherry-pick, and rebase—explaining their effects on the working directory, index, and repository, while covering concepts such as HEAD, detached HEAD, and the underlying object storage.
Basic Usage
The four commands below copy files between the working directory, the staging area (also called the index), and the repository. git add <em>files</em> – adds the specified files to the staging area. git commit – creates a snapshot from the staging area and records it as a new commit. git reset -- <em>files</em> – undoes the last git add <em>files</em>; git reset without file arguments clears the entire staging area. git checkout -- <em>files</em> – copies files from the staging area back to the working directory, discarding local changes.
You can also use interactive modes with git reset -p, git checkout -p, or git add -p.
Additional shortcuts include git commit -a (which stages all modified files before committing) and git commit <em>files</em> (which commits the specified files and stages them).
Conventions
Images illustrate commit IDs (green 5‑character strings), branch pointers (orange), and the HEAD marker that indicates the current branch. The diagram shows the latest commit ed489 on main , with another branch stable pointing to an older commit.
Command Details
Diff
Various methods exist to view changes between two commits; examples are shown in the accompanying diagrams.
Commit
When committing, Git creates a new commit object from the staged files, sets the current branch to point to this new commit, and records the previous commit as its parent. If the current branch points to ed489 , after the commit it points to f0cec , with ed489 as the parent.
Amending a commit with git commit --amend creates a new commit that replaces the previous one while keeping the same parent.
Checkout
The git checkout command copies files from a specified commit (or the index) to the working directory and can also switch branches. For example, git checkout HEAD~ foo.c copies
foo.c</em> from the parent of the current commit into both the working directory and the index.</p><p>Checking out a branch moves HEAD to that branch and updates the working tree to match the branch’s commit.</p><p>If no file or branch is specified, Git checks out a tag, remote branch, SHA‑1, or a notation like <em>main~3</em>, resulting in a detached HEAD state.</p><h3>Reset</h3><p><code>git resetmoves the current branch pointer to another commit and optionally updates the working directory and index. --hard updates both the index and working tree, while --soft leaves them unchanged.
When a file name is provided (or -p is used), git reset behaves similarly to git checkout but also updates the index.
Merge
The git merge command combines different branches. If the other branch is an ancestor of the current commit, Git performs a fast‑forward merge; otherwise, it creates a new merge commit that has both branch tips as parents.
Cherry‑Pick
git cherry-pickcopies a specific commit onto the current branch, creating a new commit with identical changes.
Rebase
Rebasing replays commits from one branch onto another, producing a linear history. It is essentially an automated, linearized cherry‑pick. The --onto option limits the range of commits to be rebased.
Interactive rebasing ( git rebase --interactive) allows you to reorder, edit, squash, or drop commits.
Technical Explanation
File contents are stored as blobs in .git/objects and referenced by SHA‑1 hashes; they are not kept directly in the index ( .git/index) or commit objects. The index lists the blobs that make up the next commit. Commits store a tree object that represents the directory hierarchy, with each tree or blob identified by its hash.
When committing from a detached HEAD, the new commit is initially reachable via the reflog for HEAD, but it will eventually become unreachable and be garbage‑collected unless a branch reference is created (e.g., git checkout -b name).
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.
