Fundamentals 10 min read

Basic Git Commands and Workflow Overview

This article explains the fundamental Git commands for adding, committing, resetting, checking out, and managing branches, along with detailed descriptions of diff, commit, checkout, reset, merge, cherry-pick, and rebase operations, providing visual illustrations and practical usage tips.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Basic Git Commands and Workflow Overview

Basic Usage

The four commands below copy files between the working directory, the staging area (also called the index), and the repository.

git add *files* – Add the specified files to the staging area.

git commit – Create a snapshot from the staged files and commit it.

git reset -- *files* – Undo the last git add *files* ; git reset without arguments clears the entire staging area.

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

You can enter interactive mode with git reset -p , git checkout -p , or git add -p .

It is also possible to skip the staging area and retrieve files directly from the repository or commit them directly.

git commit -a – Equivalent to running git add for all files in the current directory, then git commit .

git commit *files* – Commit the specified files, adding them to the staging area as part of the commit.

git checkout HEAD -- *files* – Revert the specified files to the state of the latest commit.

Conventions

Images in the following sections use the indicated format.

Command Details

Diff

There are many ways to view changes between two commits; examples are shown with accompanying diagrams.

Commit

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

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 file name is provided (or the -p option is used), Git copies the file from the specified commit to both the staging area and the working directory, e.g., git checkout HEAD~ foo.c .

If a branch name is given, the HEAD pointer moves to that branch, the working directory and index are updated to match the commit that HEAD now points to, and files that exist only in the previous commit are removed.

When 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 state.

HEAD Detached Commit Operations

While HEAD is detached, commits can still be created, but they are not attached to any named branch and may become unreachable after switching back to a branch.

To preserve such a state, 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, the branch pointer moves; --hard also updates the working directory, while --soft leaves both unchanged.

Merge

The merge command combines different branches. If the other branch is an ancestor of the current commit, a fast‑forward merge occurs; otherwise a true three‑way merge creates a new commit.

Cherry Pick

cherry-pick copies a specific commit onto the current branch as a new commit.

Rebase

rebase replays commits from one branch onto another, producing a linear history. The --onto option limits the range, and git rebase --interactive allows editing, reordering, 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 represents the directory structure.

When committing in a detached HEAD state, the commit is reachable only via the reflog and may be garbage‑collected later, similar to using git commit --amend or git rebase .

gitmergerebaseVersion Controlbranchcommitreset
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

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.