Operations 10 min read

Master Git: Essential Commands and Workflows Explained

This guide walks through Git's core commands—including add, commit, reset, checkout, diff, merge, cherry‑pick, and rebase—explaining how they move files among the working directory, index, and repository, how branches and HEAD behave, and what happens under the hood in the object database.

Big Data and Microservices
Big Data and Microservices
Big Data and Microservices
Master Git: Essential Commands and Workflows Explained

Basic Usage

git add files

– stage the specified files in the index. git commit – create a snapshot of the index and record it as a new commit. git reset -- <files> – unstage the last git add for the given files; git reset without a path removes all staged files. git checkout -- <files> – discard local changes by copying the version from the index back to the working directory.

You can also enter interactive mode with git reset -p, git checkout -p or git add -p to select hunks.

Conventions

Images in the article use a five‑character green hash to represent commit IDs, orange blocks for branches, and the HEAD label for the current branch tip.

Diff Commands

Several ways exist to view changes between two commits; the article shows examples with screenshots.

Commit Commands

git commit -a

– equivalent to running git add for all tracked files before committing. git commit <files> – creates a commit that includes the specified files and stages them. git checkout HEAD -- <files> – restores the given files to the state of the latest commit.

Amending Commits

Use git commit --amend to replace the most recent commit with a new one that has the same parent; the old commit becomes unreachable.

Checkout Command

git checkout

copies files from a commit (or the index) to the working directory and can also switch branches.

With a file name, e.g., git checkout HEAD~ foo.c, the file is taken from the parent of HEAD and staged.

With a branch name, HEAD moves to that branch and the working tree is updated to match the branch tip.

With a tag, remote branch, SHA‑1, or a notation like master~3, Git creates a detached HEAD , allowing temporary checkout of any revision.

Detached HEAD

When HEAD is detached, commits are created but no named branch points to them; they can be lost unless a new branch is created with git checkout -b <name>.

Reset Command

git reset

moves the current branch pointer and optionally updates the index and working tree.

No option: move branch pointer only. --hard: update index and working directory to match the target commit. --soft: leave index and working directory untouched.

With -p or a file name, the effect mirrors git checkout but updates the index.

Merge Command

git merge

combines two branches. If the other branch is an ancestor, the merge is a fast‑forward; otherwise a three‑way merge is performed.

Cherry‑Pick Command

git cherry-pick

copies a single commit from another branch onto the current branch, creating a new commit with the same changes.

Rebase Command

git rebase

rewrites history by replaying commits from one branch onto another, producing a linear history. The --onto option limits the range of commits to be replayed. Interactive rebase ( git rebase --interactive) lets you reorder, edit, squash, or drop commits.

Technical Details

File contents are stored as blobs in .git/objects and referenced by SHA‑1 hashes. The index ( .git/index) records which blobs belong to the next commit. Commits store a tree object that represents the directory hierarchy; each tree points to sub‑trees or blobs. When using a detached HEAD, the reflog records the temporary commit, but it will eventually be pruned unless a branch is created or the commit is referenced by another ref.

workflowDevOpsGitCommand LineVersion Controlsource control
Big Data and Microservices
Written by

Big Data and Microservices

Focused on big data architecture, AI applications, and cloud‑native microservice practices, we dissect the business logic and implementation paths behind cutting‑edge technologies. No obscure theory—only battle‑tested methodologies: from data platform construction to AI engineering deployment, and from distributed system design to enterprise digital transformation.

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.