Master Git: From Basics to Advanced Workflow with Real-World Examples
This comprehensive guide walks you through Git fundamentals, differences from SVN, Windows installation, core commands, remote repository setup, branch management, conflict resolution, stash usage, and multi‑person collaboration, all illustrated with step‑by‑step screenshots and code snippets.
1. What Is Git?
Git is the most advanced distributed version control system in the world. Its workflow consists of a Workspace (working directory), an Index/Stage (staging area), a Repository (local .git folder), and a Remote (remote repository).
2. SVN vs. Git
SVN is a centralized system that requires a network connection to commit and update, while Git is distributed—each developer has a full repository locally, allowing offline work and easy peer‑to‑peer sharing.
3. Installing Git on Windows
Download the Windows version (msysgit), run the installer with default options, then open "Git → Git Bash" from the Start menu.
After installation, configure your identity globally:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"4. Basic Operations
Create a repository with git init, add files with git add, and commit with git commit. Use git status to view changes and git diff to see differences.
git init
git add readme.txt
git commit -m "Initial commit"
git status
git diff readme.txtView history with git log or a concise one‑line format:
git log --pretty=oneline5. Reverting Changes
To undo uncommitted changes, use git checkout -- <file>. To reset to a previous commit, use git reset --hard <commit> (e.g., HEAD^ for the previous commit or HEAD~100 for 100 commits back). Find commit hashes with git reflog.
6. Remote Repositories (GitHub)
Generate an SSH key ( ssh-keygen -t rsa -C "[email protected]") and add the public key to GitHub settings. Create a new repository on GitHub, then link it locally:
git remote add origin https://github.com/youruser/testgit.gitPush the master branch: git push -u origin master Clone a remote repository with git clone <url>.
7. Branch Management
Create and switch to a new branch: git checkout -b dev Merge a branch back into master:
git checkout master
git merge devDelete a branch after merging: git branch -d dev Use git merge --no-ff to keep a merge commit for history.
8. Conflict Resolution
When merging, Git marks conflicts with <<<<<<<, =======, and >>>>>>>. Edit the file to resolve, then commit.
9. Bug‑Fix Branches and Stashing
Create a temporary bug‑fix branch, work on it, then merge back. If you need to switch tasks mid‑work, stash changes:
git stash
# later
git stash pop # or git stash apply + git stash drop10. Multi‑Person Collaboration
Push your branch with git push origin <branch>. If the push is rejected, pull the latest changes, resolve any conflicts, then push again. Use git pull after setting the upstream branch (e.g., git branch --set-upstream-to=origin/dev dev).
Push: git push origin master Pull: git pull Resolve conflicts, commit, then push.
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.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.
