Master Git: From Basics to Advanced Workflows in One Guide
This comprehensive guide walks you through Git fundamentals, core concepts, installation, configuration, common commands, branching, merging, rebasing, tagging, remote repository handling, GUI tools, undoing changes, stash, and cherry‑pick techniques, providing clear examples and code snippets for effective version control.
Git Overview
Git is a free, open‑source distributed version‑control system. It records every change to files, enabling rollback to any point and supporting collaborative development across multiple users.
Core Concepts
Workspace – the directory where you edit files.
Staging Area – a temporary area (the .git/index file) that holds changes before they are committed.
Repository – the .git folder that stores all history, branches, tags, and configuration.
Remote – a server‑side repository (e.g., origin) used for sharing changes.
Branch – a lightweight pointer to a commit, allowing parallel development.
HEAD – a pointer to the current branch’s latest commit.
Basic Workflow
Initialize a repository: git init (new) or clone an existing one with git clone <url>.
Modify files in the workspace.
Stage changes: git add . (or specify files).
Commit: git commit -m "message".
Push to a remote: git push.
Pull updates: git pull (or git fetch followed by manual merge).
Installation & Configuration
Download the installer from https://git-scm.com/ and run it. Verify the installation:
git --version
# Example output: git version 2.33.0.windows.2Configure user information (required for commits):
git config --global user.name "Your Name"
git config --global user.email "[email protected]"Set a credential helper to store HTTPS credentials:
git config --global credential.helper store.gitignore
Create a .gitignore file at the repository root to exclude files or directories from tracking. Example rules:
# Ignore all .txt files
*.txt
# Keep lib.txt
!lib.txt
# Ignore the top‑level temp directory
/temp
# Ignore everything under build/
build/Remote Repositories
Common hosting services include GitHub, GitLab, Gitee, and self‑hosted solutions. Two typical authentication methods are:
HTTPS – username/password (or token) in the URL.
SSH – public‑key authentication. Generate a key pair with ssh-keygen -t rsa, add the id_rsa.pub content to the service, and test with ssh -T [email protected].
Key remote commands:
git clone <url>
git remote -v
git remote add origin <url>
git fetch
git pull
git push
git push origin --delete <branch>Branch Management
master/main – stable production code.
dev – ongoing development.
feature/* – short‑lived feature work.
hotfix/* – urgent bug fixes.
# List branches
git branch -a
# Create a new branch
git branch feature1
# Switch (or create and switch)
git switch feature1 # or git checkout -b feature1
# Delete a branch
git branch -d feature1
# Merge another branch into the current one
git merge dev
# Merge without fast‑forward
git merge --no-ff dev
# Rebase current branch onto another
git rebase masterMerging and Conflict Resolution
Fast‑forward merges simply move the branch pointer. A regular merge creates a new commit that records both parent histories. When the same file is edited in both branches, Git inserts conflict markers ( <<<<<<< HEAD, =======, >>>>>>> branch) that must be resolved manually before committing.
Rebasing
Rebase rewrites history by replaying commits onto a new base, producing a linear history. Use git rebase master on a feature branch, then fast‑forward merge the branch back into master for a clean log.
Tagging
Tags mark specific points (usually releases). Create an annotated tag:
# Create an annotated tag
git tag -a v1.0 -m "Release 1.0"
# Push a single tag
git push origin v1.0
# Push all tags
git push origin --tagsUndoing Changes
git checkout .– discard un‑staged changes. git checkout HEAD . – reset both workspace and index to the last commit. git reset [--soft|--mixed|--hard] <commit> – move HEAD (and optionally update the index and working tree). git revert <commit> – create a new commit that undoes the specified one (safe for already‑pushed history).
Stashing
When you need to switch branches but have uncommitted work, stash it:
# Save current work
git stash
# List stashes
git stash list
# Apply and drop the latest stash
git stash pop
# Apply without dropping
git stash applyCherry‑Pick
To copy a specific commit from another branch without merging the whole branch, use:
# Apply a specific commit
git cherry-pick <commit-hash>Git Flow (Typical Workflow)
A common branching model uses long‑living dev and master branches, with short‑lived feature , release , and hotfix branches:
Develop new features on dev.
When a release is ready, merge dev into a release branch, perform final testing, then merge release into master and tag the version.
Hotfixes are branched directly from master, then merged back into both master and dev.
References
Git official site: https://git-scm.com/
Example repository used in the article: https://github.com/kwonganding/KWebNote.git
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.
