Fundamentals 11 min read

Understanding Git: Basic Commands and Workflow

This guide explains Git’s core concepts and basic commands—including add, commit, reset, checkout, merge, rebase, and cherry-pick—illustrated with diagrams that show how files move between the working directory, index, and repository, and how branches, HEAD, and detached states operate.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding Git: Basic Commands and Workflow

Basic Usage

The four commands shown move files among the working directory, staging area (also called the index), and repository.

git add files places current files into the staging area.

git commit creates a snapshot from the staging area and records it.

git reset -- files undoes the last git add ; git reset clears the entire staging area.

git checkout -- files copies files from the staging area back to the working directory, discarding local changes.

You can also use interactive modes such as git reset -p , git checkout -p , or git add -p .

It is possible to skip the staging area entirely, pulling files directly from the repository or committing directly.

Agreement

Images in the following sections use the same visual style.

The green five‑character IDs represent commit hashes pointing to parent nodes. Branches appear in orange, each pointing to a specific commit. The current branch is marked by the attached HEAD. The diagram shows the last five commits, with ed489 as the newest; main points to it, while stable points to an older ancestor.

Command Details

Diff

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

Commit

When committing, Git creates a new commit from the files in the staging area, sets the current branch to this new node, and records the previous commit as its parent.

Even if the current branch points to an ancestor, Git still creates a new commit, after which a merge (or rebase) becomes necessary.

To amend the most recent commit, use git commit --amend , which creates a new commit with the same parent and discards the old one.

Checkout

The checkout command copies files from a historical commit (or the staging area) to the working directory and can also switch branches.

When a filename is provided (or -p is used), Git copies that file from the specified commit to both the staging area and the working directory. If no commit is specified, the content is taken from the staging area, leaving the current branch unchanged.

If a branch name is given, HEAD moves to that branch and the working directory and index are updated to match the commit that HEAD now points to.

Providing neither a filename nor a branch name but a tag, remote branch, SHA‑1, or a notation like main~3 creates a detached HEAD, allowing temporary navigation among historic revisions.

HEAD in Detached State

When HEAD is detached, commits can still be created, but they are not attached to any named branch, effectively updating an anonymous reference.

If you later switch back to a named branch, the anonymous commit may become unreachable and eventually be garbage‑collected unless you create a new branch with git checkout -b name .

Reset

The reset command moves the current branch to another commit and optionally updates the working directory and index.

Without options, only the branch pointer moves. --hard also updates the working directory; --soft leaves both unchanged.

If no commit is specified, HEAD is used by default, resetting the index to the last commit (and the working directory if --hard is given).

When a filename or -p is supplied, the effect mirrors checkout with the index updated.

Merge

The merge command combines different branches. The index must match the current commit before merging. If the other branch is an ancestor, no action occurs; if the current commit is an ancestor of the other, a fast‑forward merge moves the branch pointer.

Otherwise a true three‑way merge occurs, combining the current commit, the other commit, and their common ancestor, then creating a new merge commit.

Cherry‑Pick

cherry-pick copies a specific commit and creates an identical new commit on the current branch.

Rebase

Rebase (also called “衍合”) replays the history of another branch onto the current one, producing a linear commit history; it is essentially an automated series of cherry‑picks.

All commands in the examples operate on a topic branch, which is later replayed onto main . Unreferenced old commits become eligible for garbage collection.

To limit the range of rebasing, use the --onto option. Interactive rebasing ( git rebase --interactive ) allows dropping, reordering, editing, or squashing commits.

Technical Details

File contents are stored as blobs in .git/objects and identified by SHA‑1 hashes; the index ( .git/index ) lists these blobs. Commits are stored as trees, which reference other trees or blobs, mirroring the directory structure. Each commit records the hash of its parent tree.

When committing on a detached HEAD, the new commit is referenced only by the reflog for a limited time before being reclaimed, similar to the effects of git commit --amend or git rebase .

gitmergerebaseVersion Controlcheckoutbranchcommit
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login 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.