Master Git: From Basics to Advanced Branch, Conflict, and Revert Techniques
This comprehensive guide walks you through Git fundamentals, explains the four work areas and file states, demonstrates essential commands for daily development, and dives into advanced topics such as branch management, conflict resolution, undo/revert strategies, tagging, and other powerful Git utilities.
What Is Git?
Git is a free, open‑source distributed version control system (DVCS). It stores a complete copy of the repository on each developer’s machine, allowing offline work and fast operations on projects of any size.
Four Working Areas
Workspace : the files on the local filesystem that are under Git’s control.
Index/Stage : the staging area (usually .git/index) where changes are prepared for a commit.
Repository : the local commit history; HEAD points to the latest commit.
Remote : a repository hosted on a service such as GitHub, GitLab, or Gitee.
Typical Workflow
Pull changes from a remote repository.
Modify files in the workspace.
Add the changes to the staging area.
Commit the staged changes to the local repository.
Push the new commits to the remote repository.
File States
Untracked : not yet added to Git.
Unmodified : tracked and unchanged.
Modified : tracked but changed.
Staged : changes added to the index, ready to commit.
Common Git Commands for Daily Development
Clone a repository: git clone REPO_URL Create and switch to a new branch: git checkout -b dev Add changes: git add . or git add FILE Commit changes: git commit -m "message" Show status: git status or git status -s View log: git log or git log --oneline Show differences: git diff (with various options)
Pull or fetch updates: git pull / git fetch Push commits:
git pushBranch Management
git branch : create, list, or delete branches.
git branch dev2 # create branch dev2
git branch -D dev2 # delete local branch dev2
git branch -a # list local and remote branchesgit checkout : switch branches. git checkout master # switch to master git merge : integrate changes from another branch.
git merge dev # merge dev into current branch
git merge --no-ff origin/dev
git merge --abort # abort a conflicted mergeHandling Merge Conflicts
Identify conflicted files (e.g., git status).
Edit the file, keep the desired sections, and remove conflict markers ( <<<<<HEAD, =======, >>>>>dev).
Stage the resolved file and commit.
git add FILE
git commit -m "Resolve conflict"Undo and Revert Operations
Discard Unstaged Changes
git checkout FILE # discard changes to a single file
git checkout . # discard all workspace changesgit reset
Move HEAD and optionally modify the index and workspace. git reset HEAD -- FILE – unstage a file. git reset --soft COMMIT – keep changes staged. git reset --mixed COMMIT – keep changes in the workspace. git reset --hard COMMIT – discard all changes.
git revert
Create a new commit that undoes a previous commit without rewriting history.
git revert -n COMMIT # revert but keep changes unstaged for further editingTagging Releases
git tag # list tags
git tag v1.0 # create lightweight tag on HEAD
git tag -a v1.0 -m "release" # annotated tag
git tag -d v1.0 # delete local tag
git push origin v1.0 # push tag to remote
git checkout -b new-branch v1.0 # create branch from a tagOther Classic Commands
git rebase : reapply commits on top of another base. git rebase master git stash : temporarily save work.
git stash
git stash list
git stash popgit reflog : view the reference log of HEAD. git reflog git blame FILE : show line‑by‑line author information.
git remote : manage remote repositories.
git remote -v
git remote add origin REPO_URL
git remote show originReferences
"一个小时学会Git" – https://www.cnblogs.com/best/p/7474442.html
"Git 工作区、暂存区、版本库、远程仓库" – https://www.cnblogs.com/qdhxhz/p/9757390.html
"Git Reset 三种模式" – https://www.jianshu.com/p/c2ec5f06cf1a
"Git 恢复之前版本的两种方法 reset、revert(图文详解)" – https://blog.csdn.net/yxlshk/article/details/79944535
"Git 撤销&回滚操作 (git reset 和 git revert)" – https://blog.csdn.net/asoar/article/details/84111841
"为什么要使用 git pull --rebase?" – https://www.jianshu.com/p/dc367c8dca8e
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
