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 workflow areas, file‑status transitions, essential and advanced commands, merge and rebase strategies, object model, and reference handling, all illustrated with practical code snippets and visual diagrams.

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, originally to manage Linux kernel development.

Git Workflow and Areas

Workspace (working directory)

Staging/Index (staging area)

Local Repository (modifiable commit history)

/refs/remotes (remote references, read‑only)

Remote (remote repository)

File Status Changes

Files transition between untracked, modified, staged, and committed states as they move through the Git workflow.

Common Git Commands

Simple Commands

git init
gitk
git status
git clean -fd
git fetch remote
git pull remote branch-name
git rev-parse HEAD
git merge branch-name
git format-patch HEAD^
git am patch-file
git blame file-name

Frequently Used Commands

git clone

git clone url
git clone -b branch url

git stash

git stash
git stash apply
git stash save "stash message"
git stash list
git stash drop stash@{1}
git stash clear

git config

git config --global gui.encoding=utf-8
git config --global user.name "name"
git config --global user.email "email"
git config user.name "name"

git remote

git remote -v
git remote add name url
git remote remove name
git remote show origin

git add

git add .
git add --all
git add file
git add file1 file2
git add dir
git add src/main*

git commit

git commit -m "message"
git commit file1 file2 -m "message"
git commit --amend -m "message"
git commit --amend --author="name
" --no-edit

git branch

git branch
git branch -v
git branch -vv
git branch -r
git branch new-branch
git branch -d branch-name
git branch --set-upstream-to origin/master
git branch -m old-branch new-branch

git checkout

git checkout -b local-branch origin/remote-branch
git checkout -b branch-name
git checkout branch-name
git checkout commit-file

git tag

git tag -a v1.4 -m 'my version 1.4'
git tag tag-name
git tag -d tag-name
git tag tag-name commit-id

git push

git push origin :master
git push origin --delete tag tag-name
git push remote branch-name
git push remote branch-name --force
git push remote --all
git push --tags
git push origin tag-name
git push origin :refs/tags/tag-name
git push origin dev:master

git reset

git reset HEAD
git reset --hard
git reset --hard origin/master
git reset --hard HEAD^
git reset --hard HEAD~3
git reset --hard commit-id

git diff

git diff file-name
git diff --cached file-name
git diff branch-name file-name
git diff commit-id commit-id

git show

git show tag-name
git show commit-id

git log

git log --pretty=format:"%h %cn %s %cd" --author="iisheng" --date=short src
git log --pretty=oneline file
git log --graph --pretty=oneline --abbrev-commit
# (additional examples omitted for brevity)

git rebase

git rebase branch-name
git rebase -i commit-id
git rebase -i --root

git restore

git restore --staged file
git restore file

git revert

git revert HEAD
git revert HEAD^
git revert commit-id

Advanced Tips

Enable bash completion on macOS with brew install bash-completion and source it in ~/.bash_profile . Use git stash to temporarily store unfinished work, git cherry-pick to apply specific commits, and git filter-branch to rewrite author information or permanently remove files. Recover an accidental git stash clear with git fsck --lost-found .

Merge Details

Fast‑forward merges simply move the branch pointer when the merged branch is a direct descendant. Three‑way merges create a new merge commit with multiple parents. When conflicts arise, Git stops the merge, marks conflicted files, and requires manual resolution followed by git add and git commit .

Rebase Details

Rebase rewrites history by replaying a series of commits onto a new base commit, producing a linear history that can later be fast‑forward merged. It first finds the common ancestor, extracts changes, moves the branch pointer, and reapplies the changes as new commits.

Git Objects and Snapshots

Git stores data as objects in .git/objects :

Blob objects hold file contents.

Tree objects represent directory listings and reference blobs or sub‑trees.

Commit objects record a tree hash, parent commit(s), author, committer, timestamp, and message.

Tag objects give a friendly name to a commit (or other object) and include tagger information.

References

References (refs) are stored under .git/refs . HEAD is a symbolic ref pointing to the current branch. Branch refs, tag refs, and remote refs (e.g., origin/master ) can be inspected with git show or git log . Use git fetch or git pull to synchronize remote refs.

Reference: Git Official Documentation

workflowgitmergerebaseversion controlcommandsobjects
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

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.