Master Git: Core Concepts, Data Model, and 12 Essential Commands
This guide explains Git’s origin, its snapshot‑based data model, and walks through twelve essential commands covering local operations, remote interactions, and branch management, while also providing visual diagrams and recommended learning resources for mastering version control.
Author and Development Reason
Talk is cheap. Show me the code.
This quote is from Linus Torvalds, the creator of Linux and Git.
Originally Linux used BitKeeper, but after it became non‑free, Linus wrote Git in ten days, making it a free open‑source VCS.
Version Control Systems
Version control is like keeping multiple versions of a resume or thesis.
Local VCS is simple but single‑user; centralized VCS (CVCS) like CVS, Subversion, Perforce uses a single server; distributed VCS (DVCS) like Git, Mercurial, Bazaar mirrors the whole repository, allowing work even if a server fails.
What does “mirroring the whole repository” mean?
CVCS stores only deltas; Git stores snapshots of all files, referencing unchanged files.
Git Data Model
1. What is a snapshot?
Git uses two terms:
blob(single file) and
tree(directory). A snapshot is the top‑level tree that is tracked.
Thus a snapshot represents the tracked “public account” folder.
2. Local repository data model
Each snapshot records its parent, forming a directed acyclic graph.
Every snapshot corresponds to a
commitobject:
<code>class commit {
array<commit> parents
String author
String message
Tree snapshot
}
</code>Objects (blob, tree, commit) are addressed by SHA‑1 hashes.
git cat-file -t : show object type git cat-file -p : show object content
Git also provides references like
HEAD.
Common Commands
Commands are divided into three parts: local operations, remote interactions, and branch collaboration.
Local Operations
Git has three areas: working directory, staging area, and local repository, with states
modified,
staged,
committed.
1. git add
Add changes from working directory to staging area.
2. git commit -m "msg"
Create a commit; often include a Jira link.
To amend the last commit:
<code>$ git commit --amend
</code>3. git log
Show commit history;
git log --onelinefor concise view,
git reflogfor reference log.
4. git reset
Move the repository to a given commit. Options:
--hard(sync all three areas),
--soft(only repository),
--mixed(stage area sync).
Remote Interaction
Push and pull synchronize with remote repositories.
5. git push
Upload local commits to remote.
6. git clone
Clone a remote repository to local.
7. git pull
Fetch and merge remote changes; equivalent to
git fetch+
git merge. Rebase is an alternative merge strategy.
8. git rebase
Reapply commits onto another base, useful for linear history.
Branching and Merging
9. List branches
git branchshows local branches;
git branch -vadds details.
10. Create branch
git branch <name>11. Switch branch
git checkout <name>12. Merge branch
git merge <name>merges another branch; conflicts occur when the same line is edited in both.
Learning Resources
git help
Use
git helpor
git help pullfor detailed usage and examples.
Pro Git
The book covers basic to advanced Git usage and underlying principles. Available in English ( https://git-scm.com/book/en/v2 ) and Chinese.
Play a Game
Practice Git with the interactive game learnGitBranching at https://learngitbranching.js.org/.
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.