Master Git Basics: From git init to Advanced Branch Management
This step‑by‑step guide walks developers through essential Git commands—from initializing a repository and checking status to staging, committing, branching, merging, stashing, and pushing changes—providing clear examples and code snippets for everyday version‑control workflows.
Introduction
Git is a distributed version‑control system used daily for code changes, pushes and pull‑request reviews. This guide presents essential and some lesser‑known Git commands to improve workflow efficiency.
Initialize a new repository
Create a repository in the current directory: git init Or create it directly in a specific path: git init ~/Desktop/Test The command creates a hidden .git directory that stores all version‑control metadata.
Check repository status
Show the state of files (untracked, modified, staged, committed):
git statusStage files
Stage all changes: git add . Stage a single file:
git add file.pyUnstage files
Move all staged files back to the working tree: git reset . Unstage a specific file:
git reset <filename>Commit changes
Record a snapshot of the staged files with a descriptive message:
git commit -m "<message>"View commit history
Display the commit log:
git logUndo a commit
Revert the most recent commit (hard reset to the previous commit):
git reset --hard HEAD^Branch management
Create a new branch: git branch <new-branch-name> List local branches: git branch List all branches, including remote: git branch -a Delete a branch (use -D to force delete an unmerged branch):
git branch -d <branch-name>Checkout a branch
Switch to an existing branch: git checkout <branch-name> Create and switch to a new branch in one step:
git checkout -b <new-branch>Push changes to a remote repository
Upload local commits to a remote (e.g., origin):
git push <remote> <branch-name>Pull changes from upstream
Fetch and integrate the latest changes from a remote branch:
git pull <remote> <branch-name>Stash work
Temporarily save uncommitted changes: git stash List stashed entries: git stash list Apply the most recent stash and remove it from the stash list: git stash pop Apply a stash without removing it:
git stash applyMerge branches
Integrate changes from another branch into the current HEAD: git merge <branch-to-merge> If merge conflicts occur, resolve them manually, stage the resolved files, and finalize with git commit.
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.
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.
