Fundamentals 9 min read

Master Git: Essential Commands, Workflows, and Advanced Techniques

This guide explains Git's core commands—including add, commit, reset, checkout, diff, merge, cherry-pick, and rebase—covers staging, branching, detached HEAD handling, and the underlying storage model, providing a comprehensive overview for developers seeking to master version control.

Open Source Linux
Open Source Linux
Open Source Linux
Master Git: Essential Commands, Workflows, and Advanced Techniques

Basic Usage

The four commands copy files between the working directory, staging area (index), and repository. git add <em>files</em> – add files to the staging area. git commit – create a snapshot from the staging area. git reset -- <em>files</em> – undo the last git add <em>files</em>; git reset removes all staged files. git checkout -- <em>files</em> – discard local changes by copying from the index to the working directory.

You can also use interactive modes: git reset -p, git checkout -p, git add -p. git commit -a stages all modified files and commits them. git commit <em>files</em> commits staged files and the current working directory snapshot. git checkout HEAD -- <em>files</em> rolls back to the last commit.

Conventions

Images illustrate commit IDs, branches, and HEAD.

Command Details

Diff

Various ways to view changes between commits; examples shown.

Commit

Git creates a new commit from the staged files, sets the parent, and moves the current branch pointer.

Amending a commit with git commit --amend creates a new commit with the same parent, discarding the old one.

Checkout

Copies files from a commit or the index to the working directory and can switch branches.

Examples: git checkout HEAD~ foo.c, git checkout v1.6.6.1, git checkout main.

Detached HEAD occurs when checking out a commit, tag, or SHA directly.

HEAD in detached state

Commits made in detached HEAD are not referenced by any named branch and may be lost unless a new branch is created with git checkout -b <em>name</em>.

Reset

git reset

moves the current branch pointer and optionally updates the working directory and index with --hard or --soft.

Merge

Combines branches; fast‑forward occurs when the current branch is an ancestor of the other.

Cherry Pick

Copies a specific commit onto the current branch.

Rebase

Reapplies commits from one branch onto another, creating a linear history; similar to an automated cherry‑pick.

Use --onto to limit the range, and git rebase --interactive for complex operations.

Technical Details

File contents are stored as blobs in .git/objects identified by SHA‑1 hashes; the index lists these blobs. Commits store a tree object representing the directory structure. Detached HEAD commits are referenced only by the reflog and may be garbage‑collected.

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.

GitrebaseVersion Controlcheckoutbranchingcommit
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.