Fundamentals 21 min read

Master Git: From Installation to Advanced Branch Management

This comprehensive guide walks you through installing Git on Windows, explains core concepts such as workspace, index, repository and remote, demonstrates common commands for creating, committing, branching, merging, stashing, and collaborating with GitHub, and shows how to resolve conflicts and revert changes.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Master Git: From Installation to Advanced Branch Management

What is Git?

Git is the most advanced distributed version control system in the world. Its basic components are Workspace (working directory), Index/Stage (staging area), Repository (local .git folder), and Remote (remote repository).

Key Differences Between SVN and Git

SVN is a centralized version control system that requires a network connection to a central server for most operations. Git is distributed; each developer has a full copy of the repository locally, allowing offline work and easy peer‑to‑peer sharing of changes.

Installing Git on Windows

Download the Windows version (msysgit) and run the default installer. After installation, launch "Git Bash" from the Start menu. Configure your identity with:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Basic Operations

1. Create a repository

Navigate to a folder (e.g., D:\www\testgit) and run: git init This creates a hidden .git directory that tracks changes.

2. Add files git add readme.txt Use git status to verify that files are staged.

3. Commit changes git commit -m "Initial commit" Check the commit history with git log or a concise one‑line view: git log --pretty=oneline 4. View differences git diff readme.txt Shows line‑by‑line changes between the working file and the last commit.

Reverting Changes

To revert a file to the last committed state: git checkout -- readme.txt To reset the entire repository to a previous commit:

git reset --hard HEAD^   # one commit back
git reset --hard HEAD~100   # 100 commits back

Find the commit hash with git reflog if needed.

Understanding Workspace vs. Staging Area

The workspace contains the files you edit. git add moves changes to the staging area; git commit records the staged snapshot into the repository.

Remote Repositories

Create an SSH key, add it to GitHub, then add a remote:

git remote add origin https://github.com/youruser/testgit.git

Push the local master branch: git push -u origin master Clone a remote repository with:

git clone https://github.com/youruser/testgit2.git

Branching and Merging

Create and switch to a new branch: git checkout -b dev Merge the branch back into master: git merge dev Delete the branch after merging: git branch -d dev Use git merge --no-ff -m "Message" dev to force a non‑fast‑forward merge.

Bug Fix Branches and Stashing

Create a temporary branch for a bug, work on it, then merge back. If you need to pause work, stash changes:

git stash
git stash list
git stash apply   # or git stash pop

Collaboration Workflow

Push your branch with git push origin branch-name. If the push is rejected, pull the latest changes, resolve any conflicts, commit, and push again. Use git pull to fetch and merge remote updates.

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 resolutionVersion Controlbranchingremote repositorystash
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.