Git Commands and Workflow: Add, Commit, Reset, Checkout, Merge, Rebase and More
This article provides a comprehensive guide to Git's core commands—including add, commit, reset, checkout, merge, rebase, and cherry‑pick—explaining their effects on the working directory, index, and repository, as well as visualizing branch relationships and HEAD states.
Usage
The four basic commands copy files between the working directory, the staging area (index), and the repository.
git add *files* – stage files.
git commit – snapshot the staged files and create a commit.
git reset -- *files* – undo the last git add *files* ; git reset without arguments clears the entire staging area.
git checkout -- *files* – discard local changes by copying the file from the index back to the working directory.
Interactive modes are available via git reset -p , git checkout -p , or git add -p . You can also bypass the index entirely to retrieve files directly from the repository or commit them in one step.
git commit -a – automatically stages all modified files before committing.
git commit *files* – commits the specified files, adding them to the index first.
git checkout HEAD -- *files* – restores files from the latest commit.
Conventions
Images in the original article illustrate commit IDs, branch pointers, and the HEAD reference.
Command Details
Diff
Various methods exist to view changes between two commits; the article shows example 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. Amendments ( git commit --amend ) replace the last commit 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. Using a file name (or -p ) copies that file to both the index and working tree. Supplying a branch name moves HEAD to that branch, updating the working tree accordingly. Providing a tag, remote branch, SHA‑1, or a notation like main~3 creates a detached HEAD.
Detached HEAD allows you to explore history without affecting any named branch; commits made in this state are reachable only via the reflog and may be garbage‑collected unless a new branch is created ( git checkout -b name ).
Reset
git reset moves the current branch pointer to another commit and optionally updates the index and working directory. --soft moves only the branch pointer, --hard also resets the working tree, while the default (no option) resets the index to the target commit.
Merge
git merge combines two branches. If the other branch is an ancestor, Git performs a fast‑forward; otherwise it creates a new merge commit after a three‑way merge of the two branch tips and their common ancestor.
Cherry‑Pick
git cherry-pick copies a specific commit onto the current branch, creating a new commit with the same changes.
Rebase
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 rebased. Interactive rebasing ( git rebase --interactive ) enables reordering, editing, squashing, or dropping commits.
Technical Notes
Git stores file contents as blobs in .git/objects and tracks them via SHA‑1 hashes. The index ( .git/index ) lists the blobs that form the next commit. Detached HEAD commits are referenced only by the reflog and may be lost unless a branch is created or the commit is amended/rebased.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.