Fundamentals 20 min read

Master Git: Essential Commands, Branching, Merging, and Conflict Resolution

This comprehensive guide walks you through the fundamentals of Git, covering version‑control concepts, the four work areas, daily workflow, core commands, branch management, conflict resolution, undo/revert techniques, tagging, and advanced utilities such as rebase, stash, reflog, and blame, all illustrated with clear examples and diagrams.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Git: Essential Commands, Branching, Merging, and Conflict Resolution

Preface

Mastering Git commands is a core skill for every developer. The author switched from a GUI tool to the command line and created this tutorial to review essential Git operations.

What Is Git?

Version Control

Version control manages changes to source code, configuration files, and documentation throughout software development.

Centralized VCS

A centralized system stores all history on a single server; developers sync changes by uploading to or downloading from that server.

Distributed VCS

In a distributed system each user has a full copy of the repository, can commit locally, and push changes to a remote when online.

Git

Git is a free, open‑source distributed version‑control system that efficiently handles projects of any size.

Git Theory Basics

Four Work Areas

Workspace : Files you see on your local machine.

Index/Stage : The staging area (usually .git/index) where git add places changes.

Repository : Local repository; git commit writes changes here. HEAD points to the latest commit.

Remote : Remote repositories such as GitHub or GitLab.

Git Workflow

Pull files from a remote repository.

Modify files in the workspace.

Add changes to the staging area.

Commit staged changes to the local repository.

Push commits to the remote repository.

File States

Untracked : Not yet under version control; git add moves it to staged.

Unmodified : Tracked and unchanged.

Modified : Tracked but changed; can be staged.

Staged : Changes are ready to be committed.

Basic Daily Commands

git clone

git checkout -b dev

git add

git commit

git log

git diff

git status

git pull / git fetch

git push

The diagram below shows the typical forward workflow:

git clone

git clone <em>url</em>   # Clone a remote repository

git checkout -b dev

git checkout -b dev   # Create and switch to a new branch named dev

git add

git add .                # Add all files in the current directory</code>
<code>git add <em>dir</em>           # Add a specific directory (recursively)</code>
<code>git add <em>file</em>          # Add a single file

git commit

git commit -m "message"                # Commit staged changes</code>
<code>git commit <em>file</em> -m "message"      # Commit a specific file</code>
<code>git commit --amend -m "message"      # Replace the previous commit

git status

git status               # Show working‑tree and staging area status</code>
<code>git status -s            # Short summary</code>
<code>git status --show-stash  # Show if any stash exists

git log

git log                     # Show commit history</code>
<code>git log --oneline          # Compact one‑line view</code>
<code>git log -p <em>file</em>        # Show changes for a specific file</code>
<code>git blame <em>file</em>          # Show line‑by‑line author info

git diff

git diff                     # Diff between workspace and index</code>
<code>git diff <em>filepath</em>      # Diff a specific file</code>
<code>git diff HEAD <em>filepath</em>  # Diff against HEAD</code>
<code>git diff <em>branch</em> <em>filepath</em>  # Diff between branches</code>
<code>git diff <em>commitId</em> <em>filepath</em> # Diff against a commit

git pull / git fetch

git pull                     # Fetch and merge remote changes</code>
<code>git pull origin master       # Merge remote master into current branch</code>
<code>git fetch --all             # Fetch all remote branches</code>
<code>git fetch origin master      # Fetch only master from origin

git push

git push origin master       # Push local master to remote</code>
<code>git push origin -d <em>branch</em>   # Delete a remote branch</code>
<code>git push --tags            # Push all tags

Advanced: Branch Handling

git branch

Creating, listing, and deleting branches:

git checkout -b dev2          # Create and switch to dev2</code>
<code>git branch dev2               # Create dev2, stay on current branch</code>
<code>git branch                    # List local branches</code>
<code>git branch -r                 # List remote branches</code>
<code>git branch -a                 # List all branches</code>
<code>git branch -D <em>branch</em>    # Delete a local branch

git checkout

git checkout master          # Switch to master branch

git merge

git merge master              # Merge master into current branch</code>
<code>git merge --no-ff origin/dev # Merge remote dev without fast‑forward</code>
<code>git merge --abort           # Abort an ongoing merge

Advanced: Conflict Resolution

When the same line is edited in different branches, a merge conflict occurs. Example files from dev and master branches illustrate the conflict.

Resolution steps:

Inspect the conflicted file (Git marks sections with <<<<<<< HEAD, =======, >>>>>>> dev).

Decide which changes to keep, edit the file to remove conflict markers.

Stage the resolved file and commit.

Undo and Revert

git checkout (discard changes)

git checkout <em>file</em>   # Discard changes to a file</code>
<code>git checkout .          # Discard all local changes

git reset

git reset moves the HEAD pointer to a previous commit, optionally affecting the index and working tree.
git reset HEAD --file          # Unstage a file</code>
<code>git reset --soft <em>commit</em>   # Move HEAD, keep changes staged</code>
<code>git reset --mixed <em>commit</em> # Move HEAD, keep changes in working tree</code>
<code>git reset --hard <em>commit</em>  # Discard all changes

git revert

git revert creates a new commit that undoes the changes introduced by a specified commit.
git revert -n <em>commit_id</em>   # Revert without committing automatically

Tagging

git tag                     # List all tags</code>
<code>git tag <em>tag</em>            # Create a tag on HEAD</code>
<code>git tag <em>tag</em> <em>commit</em>   # Tag a specific commit</code>
<code>git tag -d <em>tag</em>          # Delete a local tag</code>
<code>git push origin <em>tag</em>    # Push a tag to remote</code>
<code>git show <em>tag</em>           # Show tag details</code>
<code>git checkout -b <em>branch</em> <em>tag</em> # Create branch from a tag

Other Classic Commands

git rebase

Rebase rewrites commits onto another base, producing a linear history.

git rebase <em>test</em>   # Reapply current branch commits onto test

git stash

git stash               # Save current work-in-progress</code>
<code>git stash list          # List saved stashes</code>
<code>git stash pop <em>stash@{n}</em> # Restore a stash</code>
<code>git stash drop <em>stash@{n}</em> # Delete a stash</code>
<code>git stash clear         # Remove all stashes

git reflog

Shows recent updates to the HEAD reference, useful for recovering lost commits.

git reflog

git blame

Shows line‑by‑line attribution for a file.

git blame <em>filepath</em>

git remote

git remote               # List remote names</code>
<code>git remote add <em>url</em>   # Add a new remote</code>
<code>git remote show <em>remote</em> # 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.

conflict resolutionTaggingUNDObranchingMerging
Liangxu Linux
Written by

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.)

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.