Operations 11 min read

Mastering Git: Essential Commands and Best Practices Explained

This guide provides a comprehensive, step‑by‑step walkthrough of Git’s core commands—including add, commit, reset, checkout, diff, merge, cherry‑pick, and rebase—illustrated with diagrams and practical examples to help developers understand version‑control workflows and avoid common pitfalls.

IT Architects Alliance
IT Architects Alliance
IT Architects Alliance
Mastering Git: Essential Commands and Best Practices Explained

01. Basic Usage

The four fundamental commands copy files between the working directory, the staging area (also called the index), and the repository. git add *files* – stage the specified files. git commit – create a snapshot of the staged files and commit it. git reset -- *files* – undo the last git add *files*; git reset without arguments discards all staged changes. git checkout -- *files* – copy files from the staging area back to the working directory, discarding local modifications.

Interactive modes are available via git reset -p, git checkout -p, or git add -p.

You can also bypass the staging area entirely, pulling files directly from the repository or committing in one step.

02. Conventions

Images in the guide use a five‑character green hash to represent commit IDs, orange arrows for branches, and the HEAD label to indicate the current branch. The example shows the latest five commits, with ed489 as the most recent and main pointing to it, while stable points to an older ancestor.

03. Command Details

04. Diff

Various methods exist to view changes between two commits; several examples are illustrated.

05. Commit

When committing, Git creates a new commit from the staged files, sets the current node as its parent, and moves the current branch pointer to the new commit. The diagram shows main moving from ed489 to f0cec . Even if the current branch points to an ancestor, Git still creates a new commit, potentially requiring a later merge.

To amend the most recent commit, use git commit --amend, which replaces the previous commit while keeping the same parent.

06. Checkout

The git checkout command copies files from a historical commit (or the index) to the working directory and can also switch branches. Specifying a file name (or using -p) copies that file from the given commit to both the index and the working tree. Without a file name, providing a branch name moves HEAD to that branch, updating the index and working directory to match the new commit.

If neither a file nor a branch is specified, Git checks out a tag, remote branch, SHA‑1, or a notation like main~3, resulting in a detached HEAD . This allows temporary exploration of older versions, e.g., git checkout v1.6.6.1 to build a specific release.

07. Detached HEAD Commits

When HEAD is detached, commits are created but no named branch is updated; the new commit exists on an anonymous line and may be lost if not referenced later. To preserve it, create a new branch with git checkout -b *name*.

08. Reset

git reset

moves the current branch pointer and optionally updates the working directory and index. Options: --hard – update both index and working tree. --soft – leave index and working tree untouched.

Without a commit argument, git reset defaults to HEAD , rolling back the index to the last commit; adding --hard also rolls back the working tree. Supplying a file name (or -p) behaves like git checkout for that file, but updates the index.

09. Merge

git merge

combines different branches. If the other branch is an ancestor of the current commit, the merge is a fast‑forward; otherwise, a three‑way merge occurs, creating a new commit that records the combined changes.

10. Cherry‑Pick

git cherry-pick

copies a specific commit onto the current branch, creating a new commit with identical changes.

11. Rebase

Rebase rewrites history by replaying commits from one branch onto another, producing a linear history. It is essentially an automated series of cherry‑picks. Use the --onto option to limit the range, e.g., rebasing commits after 169a6 onto 2c33a. Interactive rebasing ( git rebase --interactive) allows dropping, reordering, editing, or squashing commits.

12. Technical Details

File contents are stored as blobs in .git/objects and referenced by SHA‑1 hashes. The index ( .git/index) lists the blobs that make up the staged snapshot. Commits store a tree object that represents the directory hierarchy; each commit records the hash of its parent tree, enabling reconstruction of the project state at any point.

When committing on a detached HEAD , the new commit is referenced only by the reflog and may be garbage‑collected unless a branch is created or the commit is amended ( git commit --amend) or rebased.

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.

DevOpssoftware developmentGitcommand-lineVersion Control
IT Architects Alliance
Written by

IT Architects Alliance

Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.

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.