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.
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 --statVersion 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 -fHEAD points to the current commit; ^ means the previous commit; ~N means N commits before; <commit id> can be abbreviated. Use git log <commit id> 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 pullCommon 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 masterConfiguration
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.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
