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:
<code>git config --global user.name "Your Name"
git config --global user.email "[email protected]"</code>4. Basic Operations
Create a repository with
git init, add files with
git add, and commit with
git commit. Use
git statusto view changes and
git diffto see differences.
<code>git init
git add readme.txt
git commit -m "Initial commit"
git status
git diff readme.txt</code>View history with
git logor a concise one‑line format:
<code>git log --pretty=oneline</code>5. 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~100for 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:
<code>git remote add origin https://github.com/youruser/testgit.git</code>Push the master branch:
<code>git push -u origin master</code>Clone a remote repository with
git clone <url>.
7. Branch Management
Create and switch to a new branch:
<code>git checkout -b dev</code>Merge a branch back into master:
<code>git checkout master
git merge dev</code>Delete a branch after merging:
<code>git branch -d dev</code>Use
git merge --no-ffto 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:
<code>git stash
# later
git stash pop # or git stash apply + git stash drop</code>10. 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 pullafter setting the upstream branch (e.g.,
git branch --set-upstream-to=origin/dev dev).
Push:
git push origin masterPull:
git pullResolve conflicts, commit, then push.
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.