Fundamentals 7 min read

Master Git: Essential Commands for Cloning, Branching, Merging, and Reverting

This guide walks you through essential Git operations—including cloning repositories, managing branches, viewing logs, rolling back versions, handling file changes, and configuring user settings—so you can confidently control source code throughout the development lifecycle.

Programmer DD
Programmer DD
Programmer DD
Master Git: Essential Commands for Cloning, Branching, Merging, and Reverting

Project download and branch CRUD

Project download

Clone a remote repository: git clone https://XXXX.git Fetch a remote branch to a local branch:

git fetch origin <originname>:<localname>

Branch add, delete, query, modify

List local branches: git branch List all remote branches: git branch -r List all branches (local + remote): git branch -a Create a branch: git branch <name> Switch to a branch: git checkout <name> Create and switch: git checkout -b <name> Merge a branch into the current one: git merge <name> Push a branch to remote: git push origin <name> Delete a local branch: git branch -d <name> or git branch -D <name> Delete a remote branch: git push origin -d <name> Rename a branch: git branch -m <oldbranch> <newbranch> or

git branch -M <oldbranch> <newbranch>
Note: Do not delete the branch you are currently on.

Viewing commit logs

Show recent changes: git status Show commit history (most recent first): git log Show commit history grouped by author: git shortlog One‑line summary of each commit: git log --oneline Show file changes per commit:

git log --stat

Version rollback

Rollback to previous commit: git reset --hard HEAD^ Rollback two commits back: git reset --hard HEAD^^ or git reset --hard HEAD~2 Rollback to a specific commit: git reset --hard <commit id> Force‑push the rolled‑back state:

git push -f
HEAD points to the current commit; ^ means the previous commit; ~N means N commits before; &lt;commit id&gt; can be abbreviated. Use git log &lt;commit id&gt; to inspect a specific commit. git reflog shows the command history, useful for recovering to a future commit.

File add, commit, pull, push, diff, merge

Add a new file: git add README.md Add all new files: git add . Stash changes: git stash [save "message"] Apply stashed changes: git stash pop Commit changes: git commit -m "message" Pull from remote: git pull origin <name> Push to remote: git push origin <name> Diff between two branches: git diff <name1> <name2> Diff with file list: git diff <name1> <name2> --stat Diff local vs remote branch: git diff <name> origin/<name> Merge a branch: git merge <name> Force‑overwrite local branch:

git fetch --all
git reset --hard origin/<name>
git pull

Common options and other commands

Explanation of some git options

-f

– force -d – delete -D – delete + force -m – move/rename -M – move/rename + force -r – remote -a – all

Other commands

Delete the entire project: git rm -rf . Run git pull every X seconds: for((i=1;i<=10000;i+=1)); do sleep X && git pull; done Rebase a feature branch onto master:

git checkout feature
git rebase master

Configuration

Show current config: git config --list Set user name: git config --global user.name <name> Set user email: git config --global user.email <email> Tip: Windows users can place these commands in a .bat file.

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.

GitTutorialVersion Controlcommandsbranching
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.