Fundamentals 19 min read

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.

ITPUB
ITPUB
ITPUB
Master Git: Essential Commands, Branching, and Conflict Resolution Explained

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 dev

Advanced: 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 switching

Viewing Branches

git branch          # local branches
git branch -r       # remote branches
git branch -a       # all branches

Deleting Branches

git branch -D <branchname>   # delete a local branch

Switching Branches

git checkout master

Merging Branches

After development on dev, merge it into master:

git merge dev

Advanced: 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 modifications

git 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 changes

git revert

Creates a new commit that undoes a previous one while preserving history.

git revert -n <commit_id>   # revert without committing automatically

Advanced: 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 tag

Other 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 stashes

git reflog

Shows recent updates to the tip of branches, useful for recovering lost commits.

git reflog

git 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 remote
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.

GitVersion ControlTaggingbranchingresetMerge Conflicts
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.