Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential Git Operations for Linux Environments

Tech 1

Core Git Command Reference

Git Subcommand Purpose
add Stage file changes to the index for upcoming commits
bisect Locate the commit that introduced a bug using binary search
branch Manage repository branches (list, create, delete)
checkout Switch between branches or restore working tree files; use -b to create and switch simultaneously
clone Copy an existing remote repository into a new local directory
commit Save staged index changes to the local repository with a descriptive message
diff Compare changes between working tree, index, local commits, or remote branches
fetch Download latest objects and references from a remote without merging
grep Search repository content for a matching pattern
init Initialize a new empty Git repository in the current directory
log Display commit history; use reflog to track local HEAD movements
merge Combine two or more separate development histories into one
mv Rename or move tracked files, directories, or symlinks
pull Fetch remote changes and merge them into the current local branch
push Upload local commits and update references on a remote repository
rebase Reapply local commits on top of an updated upstream branch
reset Adjust the current HEAD and optionally working tree/index to a specified state; --hard discards all uncommitted and staged changes
rm Remove files from tracking and/or working tree; --cached removes only from index, -f removes from both index and local filesystem
show Display detailed information about Git objects (commits, tags, blobs, trees)
status Show current working tree and index state (untracked, modified, staged files)
tag Create, list, delete, or verify GPG-signed version tags

Staging and Initial Commit Workflow

Start by initializing or navigating to a Git-enabled directory, then stage all modified/new files:

# Stage all files recursively
git add --all

# Verify staging status
git status

Output example after staging:

On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   project-notes.md

Staged files are referenced in .git/index, with content stored as compressed objects in .git/objects/. To inspect the hidden .git directory structure:

cd /path/to/repo
tree -L 2 .git/

Commit staged changes to the local repository with a clear message:

git commit -m "Initialize repository with project documentation"

Successful commit output:

[main (root-commit) 7c2a9f1] Initialize repository with project documentation
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 project-notes.md

Check status again to confirm a clean working tree:

git status

Output:

On branch main
nothing to commit, working tree clean

File Management Operations

Removing Tracked Files

Remove only from the index (keep local file):

git rm --cached temporary-file.txt

Remove from both index and local filesystem:

git rm -f deprecated-script.sh

Renaming Tracked Files

For unstaged files, use standard Linux mv:

mv old-filename.txt new-filename.txt

For already staged files, use Git's native rename to preserve history:

git mv old-staged-name.md updated-staged-name.md

Commmit History Exploration

Basic and advanced history queries:

# Show full commit history
git log

# Show last 3 commits
git log -3

# Show diff of the most recent commit
git log -p -1

# Show file change statistics for last 2 commits
git log --stat -2

# Show condensed one-line history
git log --pretty=oneline

# Show detailed history with author and committer info
git log --pretty=fuller -2

# Custom format output (short hash + author name + commit subject)
git log --pretty=format:"%h | %an | %s"

Custom Log Format Placeholders

Placeholder Description
%s Commit subject line
%cd Commit date (uses local timezone)
%an Author's full name
%cn Committer's full name
%ce Committer's email address
%H Full 40-character SHA-1 commit hash
%h Abbreviated 7-character SHA-1 commit hash
%T Full SHA-1 tree hash
%t Abbreviated SHA-1 tree hash
%P Full SHA-1 parent commit hashes
%p Abbreviated SHA-1 parent commit hashes
%ad Author's revision date

Version Rollback and Recovery

Rollback to Past Commits

Git uses a HEAD pointer to track the current branch tip. Rollback by resetting HEAD:

# First get target commit hash
git log --pretty=oneline

# Hard reset (discard all changes after target)
git reset --hard 7c2a9f1

# Soft reset (keep changes staged)
git reset --soft HEAD~2

# Mixed reset (keep changes unstaged; default behavior)
git reset --mixed HEAD^3

Restore "Lost" Future Commits

Use reflog to track local HEAD movements, even for discarded commits:

# View reflog entries
git reflog

# Restore to a specific reflog state
git reset --hard HEAD@{2}

Git Tag Usage

Tags mark specific commits (e.g., releases). Create lightweight or annotated tags:

# Create lightweight tag (no metadata)
git tag v0.1.0

# Create annotated tag with message and optional GPG sign (-s)
git tag -a v0.2.0 -m "Beta release with UI improvements"

# List all tags
git tag

# Filter tags by pattern
git tag -l "v0.*"

# View tag details
git show v0.2.0

# Delete a local tag
git tag -d v0.1.0

Use tags for rollback directly:

git reset --hard v0.2.0

Change Comparison Tools

Compare file content across differant Git states:

# Compare working tree to staged changes
git diff project-notes.md

# Compare staged changes to last commit
git diff --cached

# Compare two specific commits by hash
git diff 7c2a9f1 a3d4b7c

# List only changed files between two commits
git diff --name-only HEAD HEAD~
Tags: git

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.