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.
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 backFind 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.gitPush the local master branch: git push -u origin master Clone a remote repository with:
git clone https://github.com/youruser/testgit2.gitBranching 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 popCollaboration 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.
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.
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!
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.
