Fundamentals 10 min read

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.

DevOps Coach
DevOps Coach
DevOps Coach
Master Git Basics: From git init to Advanced Branch Management

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 status

Stage files

Stage all changes: git add . Stage a single file:

git add file.py

Unstage 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 log

Undo 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 apply

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

Git workflow illustration
Git workflow illustration
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.

Gitcommand-lineTutorialVersion Controlbranchingcommitstash
DevOps Coach
Written by

DevOps Coach

Master DevOps precisely and progressively.

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.