I’ve always trying to summarize an article of how to use git, but there has been a lot of resistance of doing so, one reason is that there are sufficient material that have enough info of how to use git. Although they provide a very detailed info regarding how to use git, I or beginners may find it bafffling to incorporate that into my daily workflow since there are a lot of options and arguments to be memorized.
What I’ve found is that the best way to learn it is to use it. This claim sounds really vague and would not actually provide anything meaningful. What I meant is that it would be better that a reader needs to run a repo code on local machine and remote server, collaborate with other people, efficiently develop the code with various git techniques. Git would makes more sense when one is trying to deal with a large code base and need to manage the development workflow back and forth. Those info provided online are good but it seems that they could not be able to make an impact on readers who want to get started.
I have to admit that with new generations of editors including VS Code, users don’t need to remember all the intricate commands, plugins are also able to provide commit messages that are actually pretty good. You could definitely use ChatGPT or Claude to generate the commands based on the prompt.
Thus based on the arguments I provided above, in this post I just want to provide a high level summary for git, so that readers know how to get started, to use git without memorizing all the commands. In one episodes of Friends, Ross Geller is trying to list all 50 states with lots of difficulties in Thanksgiving. My goal is definitely not asking you to remember all the commands, but to let you know where California or Washington is, and how to plan a road trip to NYC and how to handle the basic issues during the trip.
Note that in the beginning, just try to work on or become comfortable with the fundamental ones, which does not require other concepts. Once you are comfortable with the fundamental blocks, you could work all the way up.
https://education.github.com/git-cheat-sheet-education.pdf
%%{init: {'theme': 'default', 'themeVariables': { 'fontSize': '40px' }}}%%
sequenceDiagram
autonumber
participant WD as Working<br/>Directory
participant SA as Staging<br/>Area
participant LR as Local<br/>Repository
participant RR as Remote<br/>Repository
rect rgb(240, 248, 255)
Note over WD,RR: 📝 Basic Workflow
WD->>+SA: git add . (Stage all)
WD->>SA: git add -p (Interactive staging)
WD->>SA: git add -N (Intent to add)
WD->>SA: git add --chmod=+x file
SA->>+LR: git commit -m "feat: new feature"
SA->>LR: git commit --amend
SA->>LR: git commit --fixup=HEAD
SA->>LR: git commit --date="2 days ago"
LR->>+RR: git push origin main
LR->>RR: git push --force-with-lease
LR->>RR: git push --follow-tags
LR->>RR: git push --all
RR-->>-LR: Confirm push success
LR-->>-SA: Update HEAD pointer
SA-->>-WD: Clear staged changes
end
rect rgb(255, 248, 240)
Note over WD,RR: 🔄 Synchronization & Remote
RR->>+LR: git fetch --all --prune --tags
RR->>LR: git fetch origin branch:branch
RR->>LR: git remote add upstream URL
RR->>LR: git remote set-url origin URL
RR->>+WD: git pull --rebase origin main
RR->>WD: git pull --ff-only
RR->>WD: git pull --autostash
RR->>WD: git pull --allow-unrelated-histories
WD-->>-RR: Confirm sync complete
LR-->>-RR: Update tracking refs
end
rect rgb(245, 245, 245)
Note over WD,RR: 🌿 Branch Management
LR->>LR: git branch feature/new
LR->>LR: git branch -D old-branch
LR->>LR: git branch --merged
LR->>LR: git branch --no-merged
LR->>LR: git checkout feature/new
LR->>LR: git checkout -b feature/new upstream/feature
LR->>LR: git switch -c new-branch
LR->>LR: git switch - (Previous branch)
LR->>LR: git merge --squash feature
LR->>LR: git merge --strategy-option theirs
LR->>LR: git merge --no-ff
LR->>LR: git rebase -i HEAD~3
LR->>LR: git rebase --onto main feature
LR->>LR: git rebase --autostash
Note over LR: git cherry-pick -x commit-hash
Note over LR: git cherry-pick --no-commit commit-hash
end
rect rgb(240, 255, 240)
Note over WD,RR: ⚡ Temporary Storage
SA->>SA: git stash save "WIP: feature"
SA->>SA: git stash list
SA->>SA: git stash show -p stash@{0}
SA->>SA: git stash branch new-branch
LR->>WD: git checkout -- file.txt
SA->>WD: git stash pop stash@{0}
SA->>WD: git stash apply stash@{0}
SA->>WD: git stash drop stash@{0}
SA->>WD: git stash clear
WD->>WD: git clean -fd
WD->>WD: git clean -fX (Remove ignored)
end
rect rgb(255, 240, 240)
Note over WD,RR: 🔍 Inspection & Comparison
WD-->>WD: git status -s -b
WD-->>WD: git status --ignored
LR-->>LR: git log --graph --oneline --all
LR-->>LR: git log --follow file.txt
LR-->>LR: git log -S"string" --patch
LR-->>LR: git log --author="name"
LR-->>LR: git diff --cached
LR-->>LR: git diff branch1...branch2
LR-->>LR: git diff --word-diff
LR-->>LR: git blame -w -C file.txt
LR-->>LR: git reflog expire --expire=30.days
LR-->>LR: git shortlog -sn --no-merges
LR-->>LR: git rev-parse HEAD
LR-->>LR: git rev-list --count HEAD
end
rect rgb(240, 240, 255)
Note over WD,RR: ↩️ History Modification
SA->>WD: git reset HEAD file.txt
SA->>WD: git restore --staged file.txt
SA->>WD: git restore --source=HEAD~1 file.txt
LR->>WD: git reset --hard HEAD~1
LR->>WD: git reset --soft HEAD~1
LR->>WD: git reset --merge ORIG_HEAD
LR->>LR: git revert HEAD
LR->>LR: git revert --no-commit HEAD~3..
LR->>LR: git revert -m 1 HEAD
LR->>LR: git filter-branch --tree-filter
LR->>LR: git filter-repo --path file.txt
end
rect rgb(248, 240, 255)
Note over WD,RR: 🏷️ Tags & Releases
LR->>LR: git tag -a v1.0.0 -m "Release"
LR->>LR: git tag -s v1.0.0 -m "Signed"
LR->>LR: git tag --contains commit-hash
LR->>RR: git push --tags
LR->>RR: git push --follow-tags
LR->>LR: git tag -d old-tag
LR->>LR: git tag -l "v1.8.5*"
RR->>LR: git fetch --tags
end
rect rgb(240, 255, 255)
Note over WD,RR: 🔧 Config & Maintenance
WD->>WD: git config --global user.name
WD->>WD: git config --global alias.co checkout
WD->>WD: git config --global core.editor vim
WD->>WD: git config --global merge.tool kdiff3
LR->>LR: git gc --aggressive
LR->>LR: git prune --expire=2.weeks.ago
LR->>LR: git fsck --full
LR->>LR: git count-objects -v
LR->>LR: git verify-pack -v
end
rect rgb(255, 255, 240)
Note over WD,RR: 📦 Submodules & Worktrees
LR->>LR: git submodule add URL path
LR->>LR: git submodule update --init --recursive
LR->>LR: git submodule foreach git pull
LR->>LR: git worktree add path branch
LR->>LR: git worktree list
LR->>LR: git worktree remove path
end
rect rgb(240, 245, 255)
Note over WD,RR: 🎣 Hooks & Automation
LR->>LR: prepare-commit-msg hook
LR->>LR: pre-commit hook
LR->>LR: post-commit hook
LR->>LR: pre-push hook
LR->>LR: post-merge hook
end
rect rgb(255, 245, 240)
Note over WD,RR: 🔀 Advanced Operations
LR->>LR: git bisect start
LR->>LR: git bisect good/bad
LR->>LR: git grep -n "string"
LR->>LR: git archive --format=zip HEAD
LR->>LR: git notes add -m "note"
LR->>LR: git worktree add -b emergency-fix ../temp HEAD
LR->>LR: git maintenance start
end
Note over WD,RR: 💡 Pro Tips:
Note over WD,RR: 1️⃣ Efficient Workflow:<br/>git commit -am "message" (Add & commit)<br/>git push -u origin branch (Set upstream)<br/>git pull --rebase (Keep history clean)
Note over WD,RR: 2️⃣ Advanced Usage:<br/>git log --pretty=format:"%h %an %s"<br/>git rev-list --objects --all | git cat-file --batch-check<br/>git for-each-ref --format='%(refname:short)'
Note over WD,RR: 3️⃣ Maintenance:<br/>git gc --auto<br/>git repack -d<br/>git maintenance start
Note over WD,RR: 4️⃣ Debugging:<br/>git bisect run npm test<br/>git blame -w -C -C -C<br/>GIT_TRACE=1 git commandsequenceDiagram
%%{init: {'theme': 'default', 'themeVariables': { 'fontSize': '36px', 'messageFontSize': '36px', 'noteFontSize': '36px'}}}%%
autonumber
participant DEV as Developer
participant WD as Working<br/>Directory
participant SA as Staging<br/>Area
participant LR as Local<br/>Repository
participant RR as Remote<br/>Repository
rect rgb(240, 248, 255)
Note over DEV,RR: 🚀 Efficient Daily Workflow
DEV->>WD: git config --global alias.cm "commit -am"
Note right of WD: Quick commit with message
DEV->>WD: git config --global alias.st "status -sb"
Note right of WD: Concise status view
WD->>SA: git add -p
Note right of SA: Interactive staging for precise commits
SA->>LR: git cm "feat: new feature"
Note right of LR: Using the alias we just created
LR->>RR: git push -u origin HEAD
Note right of RR: Push & set upstream to current branch
end
rect rgb(255, 245, 240)
Note over DEV,RR: 📊 History Management
DEV->>LR: git rebase -i HEAD~3
Note right of LR: Interactive rebase for clean history
LR->>LR: git commit --fixup=HEAD~1
LR->>LR: git rebase -i --autosquash HEAD~3
Note right of LR: Automatic squashing of fixup commits
RR->>WD: git pull --rebase --autostash
Note right of WD: Rebase & stash in one command
end
rect rgb(240, 255, 240)
Note over DEV,RR: 🔍 Advanced Inspection
DEV->>LR: git log --pretty=format:"%C(yellow)%h %C(blue)%ad %C(red)%an %C(green)%s"
Note right of LR: Colored, formatted log output
LR->>LR: git shortlog -sn --all --no-merges
Note right of LR: Contributor statistics
DEV->>LR: git log -G"pattern" --patch
Note right of LR: Search history for code changes
DEV->>LR: git blame -w -C -C file.txt
Note right of LR: Detect code movement & ignore whitespace
end
rect rgb(245, 245, 255)
Note over DEV,RR: 🔧 Maintenance & Cleanup
DEV->>LR: git maintenance start
Note right of LR: Enable background maintenance
DEV->>LR: git gc --aggressive --prune=now
Note right of LR: Deep repository optimization
DEV->>WD: git clean -fdx
Note right of WD: Remove all untracked & ignored files
DEV->>LR: git reflog expire --expire=30.days
Note right of LR: Clean old reflog entries
end
rect rgb(255, 245, 255)
Note over DEV,RR: 🐛 Debugging Tools
DEV->>LR: git bisect start
DEV->>LR: git bisect bad HEAD
DEV->>LR: git bisect good HEAD~10
DEV->>LR: git bisect run npm test
Note right of LR: Automatic bug finding
DEV->>LR: GIT_TRACE=1 git status
Note right of LR: Debug Git operations
end
rect rgb(245, 255, 245)
Note over DEV,RR: 💼 Working with Branches
DEV->>LR: git checkout -
Note right of LR: Quick switch to previous branch
DEV->>LR: git branch --merged main
Note right of LR: List merged branches
DEV->>LR: git branch --no-merged
Note right of LR: Find work in progress
DEV->>LR: git for-each-ref --sort=-committerdate refs/heads/
Note right of LR: List branches by last commit
end
rect rgb(255, 250, 240)
Note over DEV,RR: 🔒 Safety & Recovery
DEV->>LR: git config --global merge.ff only
Note right of LR: Prevent accidental merges
DEV->>RR: git push --force-with-lease
Note right of RR: Safe force push
DEV->>WD: git stash save "WIP: description"
Note right of WD: Descriptive stash entries
DEV->>LR: git reflog show --all
Note right of LR: Track all ref changes
end
rect rgb(240, 250, 255)
Note over DEV,RR: 🚄 Performance Tips
DEV->>LR: git clone --depth 1
Note right of LR: Shallow clone for large repos
DEV->>LR: git clone --filter=blob:none
Note right of LR: Clone without file content
DEV->>RR: git fetch --unshallow
Note right of RR: Get full history when needed
DEV->>LR: git commit --no-verify
Note right of LR: Skip hooks for quick commits
end
rect rgb(250, 245, 255)
Note over DEV,RR: 🔗 Integration Workflow
DEV->>RR: git fetch origin pull/123/head:pr-123
Note right of RR: Fetch PR for local testing
DEV->>LR: git rebase --onto main feature bug-fix
Note right of LR: Re-target branch base
DEV->>LR: git merge --squash feature
Note right of LR: Combine feature as one commit
DEV->>RR: git cherry-pick -x commit-hash
Note right of RR: Track cherry-picked commits
end
%Note over DEV,RR: 💡 Additional Tips:<br/>
%Note over DEV,RR: • Use .gitignore templates for different project types<br/>
%Note over DEV,RR: • Set up global .gitconfig with useful aliases<br/>
%Note over DEV,RR: • Use commit message templates for consistency<br/>
Note over DEV,RR: • Configure SSH keys with multiple identitiessequenceDiagram
autonumber
participant DEV as Developer
participant WD as Working<br/>Directory
participant SA as Staging<br/>Area
participant LR as Local<br/>Repository
participant RR as Remote<br/>Repository
rect rgb(240, 248, 255)
Note over DEV,RR: 🚀 Efficient Daily Workflow
DEV->>WD: git config --global alias.cm "commit -am"
Note over DEV: Set up alias for quick commits with message
DEV->>WD: git config --global alias.st "status -sb"
Note over DEV: Create alias for concise status view
WD->>SA: git add -p
Note over WD: Stage changes interactively for precise commits
SA->>LR: git cm "feat: new feature"
Note over SA: Use alias to stage and commit in one command
LR->>RR: git push -u origin HEAD
Note over LR: Push and set upstream to current branch name
end
rect rgb(255, 245, 240)
Note over DEV,RR: 📊 History Management
DEV->>LR: git rebase -i HEAD~3
Note over DEV: Clean up last 3 commits interactively
LR->>LR: git commit --fixup=HEAD~1
Note over LR: Mark commit to be squashed later
LR->>LR: git rebase -i --autosquash HEAD~3
Note over LR: Automatically arrange and squash fixup commits
RR->>WD: git pull --rebase --autostash
Note over RR: Stash, rebase and pop changes automatically
end
rect rgb(240, 255, 240)
Note over DEV,RR: 🔍 Advanced Inspection
DEV->>LR: git log --pretty=format:"%C(yellow)%h %C(blue)%ad %C(red)%an %C(green)%s"
Note over DEV: Custom colored log output with hash, date, author, message
LR->>LR: git shortlog -sn --all --no-merges
Note over LR: List all contributors sorted by commit count
DEV->>LR: git log -G"pattern" --patch
Note over DEV: Search commit history for code pattern changes
DEV->>LR: git blame -w -C -C file.txt
Note over DEV: Track line changes while ignoring whitespace and detecting moves
end
rect rgb(245, 245, 255)
Note over DEV,RR: 🔧 Maintenance & Cleanup
DEV->>LR: git maintenance start
Note over DEV: Schedule regular background maintenance tasks
DEV->>LR: git gc --aggressive --prune=now
Note over DEV: Aggressively optimize repository size and performance
DEV->>WD: git clean -fdx
Note over DEV: Remove all untracked and ignored files recursively
DEV->>LR: git reflog expire --expire=30.days
Note over DEV: Remove old reflog entries older than 30 days
end
rect rgb(255, 245, 255)
Note over DEV,RR: 🐛 Debugging Tools
DEV->>LR: git bisect start
Note over DEV: Initialize binary search for bug
DEV->>LR: git bisect bad HEAD
Note over DEV: Mark current version as broken
DEV->>LR: git bisect good HEAD~10
Note over DEV: Mark older version as working
DEV->>LR: git bisect run npm test
Note over DEV: Automate bug finding with test command
DEV->>LR: GIT_TRACE=1 git status
Note over DEV: Enable detailed Git operation logging
end
rect rgb(245, 255, 245)
Note over DEV,RR: 💼 Branch Management
DEV->>LR: git checkout -
Note over DEV: Switch to previously checked out branch
DEV->>LR: git branch --merged main
Note over DEV: Show branches merged into main
DEV->>LR: git branch --no-merged
Note over DEV: List branches with unmerged changes
DEV->>LR: git for-each-ref --sort=-committerdate refs/heads/
Note over DEV: List branches sorted by last commit date
end
rect rgb(255, 250, 240)
Note over DEV,RR: 🔒 Safety & Recovery
DEV->>LR: git config --global merge.ff only
Note over DEV: Prevent accidental merge commits
DEV->>RR: git push --force-with-lease
Note over DEV: Force push only if remote hasn't changed
DEV->>WD: git stash save "WIP: description"
Note over DEV: Save work-in-progress with clear description
DEV->>LR: git reflog show --all
Note over DEV: View history of all ref updates
end
rect rgb(240, 250, 255)
Note over DEV,RR: 🚄 Performance Tips
DEV->>LR: git clone --depth 1
Note over DEV: Clone only latest commit for faster download
DEV->>LR: git clone --filter=blob:none
Note over DEV: Clone without file contents initially
DEV->>RR: git fetch --unshallow
Note over DEV: Download full history when needed
DEV->>LR: git commit --no-verify
Note over DEV: Skip pre-commit hooks for faster commits
end
rect rgb(250, 245, 255)
Note over DEV,RR: 🔗 Integration Workflow
DEV->>RR: git fetch origin pull/123/head:pr-123
Note over DEV: Fetch PR locally for testing
DEV->>LR: git rebase --onto main feature bug-fix
Note over DEV: Move bug-fix branch onto main branch
DEV->>LR: git merge --squash feature
Note over DEV: Combine all feature commits into one
DEV->>RR: git cherry-pick -x commit-hash
Note over DEV: Copy commit with reference to original
end
%Note over DEV,RR: 💡 Additional Tips:<br/>
%Note over DEV,RR: • Create project-specific .gitignore using 'git config --global alias.ignore "!gi() { curl -L -s https://www.gitignore.io/api/$@ ;};gi"'<br/>
%Note over DEV,RR: • Set up SSH config with multiple identities using 'Host github-work' and 'Host github-personal'<br/>
%Note over DEV,RR: • Use commit templates: git config --global commit.template ~/.gitmessage<br/>
Note over DEV,RR: • Enable auto-correction: git config --global help.autocorrect 1%%{init: { 'sequence': {'mirrorActors': false, 'fontSize': 24, 'messageAlign': 'center', 'actorFontSize': 24, 'noteFontSize': 24, 'messageFontSize': 24, 'messageFont': 'arial', 'actorFont': 'arial', 'noteFont': 'arial'} } }%%
sequenceDiagram
autonumber
participant DEV as Developer
participant WD as Working<br/>Directory
participant SA as Staging<br/>Area
participant LR as Local<br/>Repository
participant RR as Remote<br/>Repository
rect rgb(240, 248, 255)
Note over DEV,RR: 🚀 Efficient Daily Workflow
DEV->>WD: git config --global alias.cm "commit -am"
Note over DEV: Set up alias for quick commits with message
endgit config --global credential.helper cacheBash
Leave a Reply