Fundamentals 22 min read

Comprehensive Introduction to Git: Workflow, Commands, and Advanced Operations

This article provides a thorough overview of Git, covering its origins, core concepts such as workspace, staging area, and repositories, detailed explanations of common and advanced commands, workflow diagrams, troubleshooting tips, and best practices for collaborative development and version control.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Comprehensive Introduction to Git: Workflow, Commands, and Advanced Operations

Git Overview

Git is a distributed version control system created by Linus Torvalds in 2005 under the GPL, originally designed to manage Linux kernel development.

Git Workflow and Areas

The main areas are Workspace (working directory), Staging/Index (temporary area), Local Repository (modifiable), Remote References (/refs/remotes, read‑only), and Remote Repository.

File Status Changes

Git tracks files through three states: untracked, modified, and staged, allowing transitions via commands such as git add, git commit, and git reset.

Common Git Commands

Simple Commands

# Initialize a repository in the current directory
git init
# Open the graphical history viewer
gitk
# Show all changes
git status
# Remove all untracked files
git clean -fd
# Fetch updates from remote
git fetch remote
# Fetch and merge updates
git pull remote branch-name
# Show the latest commit ID
git rev-parse HEAD
# Merge a branch into the current one
git merge branch-name
# Create a patch from the last commit
git format-patch HEAD^
# Apply a patch file
git am patch-file
# Show blame for a file
git blame file-name

Frequently Used Commands

git clone

# Clone a remote repository
git clone url
# Clone a specific branch
git clone -b branch url

git stash

# Save uncommitted changes
git stash
# Apply the most recent stash
git stash apply
# Save with a message
git stash save "stash test"
# List all stashes
git stash list
# Drop a specific stash
git stash drop stash@{1}
# Clear all stashes
git stash clear

git config

# Set GUI encoding
git config --global gui.encoding=utf-8
# Set user name
git config --global user.name "Your Name"
# Set user email
git config --global user.email "[email protected]"
# Set name for the current repository
git config user.name "Your Name"

git remote

# Show all remotes
git remote -v
# Add a new remote
git remote add name url
# Remove a remote
git remote remove name
# Show details of a remote
git remote show origin

git add / commit

# Add all changes
git add .
git add --all
# Add a specific file
git add file
# Commit staged changes
git commit -m "message"
# Amend the previous commit
git commit --amend -m "new message"
# Change author of the last commit
git commit --amend --author="Name <email>" --no-edit

Branch Management

# List branches
git branch
# Create a new branch
git branch new-branch
# Delete a branch
git branch -d old-branch
# Set upstream for a branch
git branch --set-upstream-to origin/master
# Rename a branch
git branch -m old new

Checkout and Reset

# Create and switch to a new branch
git checkout -b new-branch
# Switch to an existing branch
git checkout branch-name
# Discard changes in a file
git checkout -- file
# Reset staging area
git reset HEAD
# Hard reset to a specific commit
git reset --hard commit-id

Tagging

# Create an annotated tag
git tag -a v1.4 -m "my version 1.4"
# List tags
git tag
# Delete a tag
git tag -d tag-name

Push and Pull

# Delete a remote branch
git push origin :master
# Delete a remote tag
git push origin --delete tag tag-name
# Push a branch
git push remote branch-name
# Force push
git push remote branch-name --force
# Push all branches
git push remote --all
# Push all tags
git push --tags

Diff and Log

# Show changes between working tree and index
git diff file-name
# Show staged changes
git diff --cached file-name
# Show commit history with custom format
git log --pretty=format:"%h %cn %s %cd" --author="iisheng" --date=short src

Rebase

# Rebase current branch onto another
git rebase branch-name
# Interactive rebase
git rebase -i commit-id
# Rebase from the root
git rebase -i --root

Restore and Revert

# Unstage a file
git restore --staged file
# Discard changes in a file
git restore file
# Revert a commit
git revert HEAD

Advanced Tips and Troubleshooting

Enable Bash auto‑completion on macOS:

brew install bash-completion
if [ -f $(brew --prefix)/etc/bash_completion ]; then
  . $(brew --prefix)/etc/bash_completion
fi

When switching branches with uncommitted work, stash the changes ( git stash) and later apply them ( git stash apply). To discard all local modifications, use git reset --hard or reset to the remote state with git reset --hard origin/master and force‑push if necessary.

Fix incorrect author information in history using git filter-branch with an --env-filter script that rewrites OLD_EMAIL to CORRECT_NAME and CORRECT_EMAIL, then force‑push the rewritten history.

Recover accidentally deleted stashes with git fsck --lost-found, locate the lost commit IDs, and re‑apply them.

Configure multiple SSH identities for GitHub and GitLab by editing ~/.ssh/config with separate Host entries pointing to different identity files.

Use git rebase instead of merge to keep a linear history, avoiding merge commits and branch crossing.

Git objects (blob, tree, commit, tag) store data as SHA‑1 addressed files under .git/objects. Blobs hold file contents, trees represent directory snapshots, commits reference a tree plus metadata, and tags provide human‑readable references.

References (HEAD, branch names, tags, remote refs) are stored under .git/refs. HEAD points to the current branch; remote refs like origin/master must be updated with git fetch or git pull to stay in sync.

Reference: Git Official Documentation

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

workflowGithooksmergerebaseVersion Controlcommands
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

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.