Master Git: Essential Commands, Branching, and Conflict Resolution Explained
This comprehensive guide walks you through the fundamentals of Git—including its role as a distributed version‑control system, the four work areas, common workflows, essential commands for cloning, committing, branching, merging, handling conflicts, undoing changes, tagging, and several advanced utilities—complete with code snippets and visual diagrams.
What is Git?
Git is a free, open‑source distributed version‑control system. It stores the full history of a project in a local .git directory and can synchronize with remote repositories such as GitHub or Gitee.
Git Theory Basics
Four Work Areas
Workspace : the files and directories you see on your computer.
Index/Stage : the staging area (usually .git/index) where git add places changes.
Repository : the local .git directory that stores commits; HEAD points to the latest commit.
Remote : a repository hosted on a service such as GitHub.
Typical Workflow
Pull code from a remote repository.
Modify files in the workspace.
Stage the changes with git add.
Commit the staged changes with git commit.
Push the new commits to the remote repository.
File States
Each file can be Untracked , Unmodified , Modified , or Staged . The state changes as you add, modify, or commit the file.
Common Git Commands for Daily Development
git clone <url>– clone a remote repository. git checkout -b dev – create and switch to a new branch named dev. git add . – stage all changes in the current directory. git commit -m "msg" – commit staged changes with a message. git status / git status -s – show workspace and staging status. git log / git log --oneline – view commit history. git diff – compare differences between worktree, index, or commits. git pull / git fetch – update local branches from the remote. git push – send local commits to the remote.
Command Examples
git clone https://github.com/example/repo.git git checkout -b dev git add . git commit -m "Add feature X" git status git log --oneline git diff git pull origin master git push origin devAdvanced: Branch Management
git branch– list, create, or delete branches. git checkout – switch branches. git merge – merge one branch into another.
Creating Branches
git checkout -b dev2 # create and switch to dev2 git branch dev2 # create dev2 without switchingViewing Branches
git branch # local branches git branch -r # remote branches git branch -a # all branchesDeleting Branches
git branch -D <branchname> # delete a local branchSwitching Branches
git checkout masterMerging Branches
After development on dev, merge it into master:
git merge devAdvanced: Conflict Resolution
When the same line of a file is edited in two branches, a merge conflict occurs.
Example Conflict
public class HelloWorld {</code><code> public static void main(String[] args) {</code><code> System.out.println("Hello, dev version!");</code><code> }</code><code>}and on master:
public class HelloWorld {</code><code> public static void main(String[] args) {</code><code> System.out.println("Hello, master version!");</code><code> }</code><code>}Resolution Steps
Open the conflicted file and locate conflict markers <<<<<<<, =======, >>>>>>>.
Choose which version(s) to keep, edit the file, and remove the markers.
Stage the resolved file ( git add <file>) and commit.
Advanced: Undo and Revert
Discarding Uncommitted Changes
git checkout -- file.txt # discard changes in a single file</code><code>git checkout . # discard all local modificationsgit reset
Moves HEAD to a previous commit and optionally updates the index and working tree.
git reset HEAD -- file # unstage a file</code><code>git reset --soft <commit> # keep changes staged</code><code>git reset --mixed <commit> # keep changes in working tree</code><code>git reset --hard <commit> # discard all changesgit revert
Creates a new commit that undoes a previous one while preserving history.
git revert -n <commit_id> # revert without committing automaticallyAdvanced: Tagging
Tags mark specific points in history, often used for releases.
git tag # list tags</code><code>git tag v1.0 # create a lightweight tag on HEAD</code><code>git tag v1.0 <commit> # create a tag on a specific commit</code><code>git tag -d v1.0 # delete a local tag</code><code>git push origin v1.0 # push a tag to the remote</code><code>git checkout -b new-branch v1.0 # create a branch from a tagOther Classic Commands
git rebase
Reapplies commits on top of another base, producing a linear history.
# before rebase</code><code>A---B---C---F--- (master)</code><code> \</code><code> D---E (test)</code><code># after rebase</code><code>A---B---D---E---C'---F'--- (test, master)git stash
Temporarily saves uncommitted changes.
git stash # hide current work</code><code>git stash list # list saved stashes</code><code>git stash pop @{0} # restore the most recent stash</code><code>git stash drop @{0} # delete a stash</code><code>git stash clear # delete all stashesgit reflog
Shows recent updates to the tip of branches, useful for recovering lost commits.
git refloggit blame
Displays line‑by‑line attribution for a file.
git blame <file>git remote
git remote # list remote names</code><code>git remote add <name> <url> # add a remote</code><code>git remote show <name> # show details of a remoteSigned-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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
