Fundamentals 21 min read

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.

macrozheng
macrozheng
macrozheng
Master Git: From Basics to Advanced Workflow with Real-World Examples

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

Git workflow diagram
Git workflow diagram

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.

Git Bash shortcut
Git Bash shortcut

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 status

to view changes and

git diff

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

or a concise one‑line format:

<code>git log --pretty=oneline</code>

5. Reverting Changes

To undo uncommitted changes, use

git checkout -- &lt;file&gt;

. To reset to a previous commit, use

git reset --hard &lt;commit&gt;

(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:

<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 &lt;url&gt;

.

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-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:

<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 &lt;branch&gt;

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

Collaboration workflow
Collaboration workflow
gitversion controlcollaborationBranchingGit Commands
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

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