Master Git: Essential Commands, Workflows, and Advanced Techniques
This guide explains how Git’s core commands move files among the working directory, staging area, and repository, covering add, commit, reset, checkout, diff, merge, rebase, cherry-pick, and related concepts, with visual diagrams and tips for interactive modes and detached HEAD handling.
Usage
These four commands copy files among the working directory, staging area (index), and repository. git add *files* – adds current files to the staging area. git commit – creates a snapshot from the staging area and commits it. git reset -- *files* – undoes the last git add *files*; git reset undoes all staged files. git checkout -- *files* – copies files from the staging area to the working directory, discarding local changes.
You can enter interactive mode with git reset -p, git checkout -p, or git add -p.
You can also skip the staging area and directly retrieve files from the repository or commit directly.
Conventions
Images in the following sections use the indicated format.
Green 5‑character IDs represent commit hashes, orange bars indicate branches, and the HEAD label marks the current branch. The diagram shows the last five commits, with ed489 as the latest; main points to it, while stable points to its grandparent.
Command Details
Diff
Various ways exist to view changes between two commits; examples are shown.
Commit
git commit -ais equivalent to running git add for all files in the current directory and then git commit. git commit *files* creates a new commit that includes the current snapshot and adds the specified files to the staging area. git checkout HEAD -- *files* rolls back to the last commit for the given files.
Checkout
The checkout command copies files from a historical commit (or the index) to the working directory and can also switch branches.
When a filename is given (or -p is used), Git copies the file from the specified commit to both the working directory and the staging area; without a filename, only the index is affected.
If no filename or branch is specified but a tag, remote branch, SHA‑1, or a range like main~3 is given, Git creates a detached HEAD .
Detached HEAD allows easy switching between historical versions, e.g., git checkout v1.6.6.1 to compile a specific version, then git checkout main to return.
HEAD in detached state
When HEAD is detached, commits are created but no named branch is updated; the commit may become unreachable after switching branches unless a new branch is created with git checkout -b *name*.
Reset
The reset command moves the current branch to another commit and optionally updates the working directory and index. --hard updates both, while --soft leaves them unchanged.
Without a commit argument, HEAD is used; the branch pointer stays, but the index (and optionally the working tree) reverts to the last commit.
Merge
The merge command combines different branches. If the other branch is an ancestor, the merge does nothing; if the current commit is an ancestor, a fast‑forward merge occurs.
Otherwise a true three‑way merge is performed, preserving the current directory and index, then creating a new commit that merges the two histories.
Cherry Pick
The cherry-pick command copies a specific commit onto the current branch as a new commit.
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.
To limit the range, use the --onto option. Interactive rebase ( git rebase --interactive) facilitates dropping, reordering, editing, or squashing commits.
Technical Notes
File contents are stored as blobs in .git/objects and referenced by SHA‑1 hashes; the index ( .git/index) lists these blobs. Commits store a tree object that mirrors the directory structure.
When committing on a detached HEAD, the commit is referenced only by the reflog and may be garbage‑collected unless saved with git commit --amend or by creating a branch.
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.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
