Comprehensive Guide to Git Workflows, Commands, and Best Practices
This article provides an extensive overview of Git workflows, including Git Flow, GitHub Flow, and GitLab Flow, along with practical command references, best practices for commits, branching, rebasing, stash usage, repository cloning, and hook configuration, aimed at improving version control efficiency.
The author, a senior architect, introduces the importance of mastering Git for efficient development and presents several widely used workflows.
Common Git Workflows
Git Flow : main, stable, develop, feature, hotfix branches.
GitHub Flow : create branch → commit → pull‑request → review → merge.
GitLab Flow : production, environment, release branches.
Daily Best Practices
Prefer command‑line over GUI for speed and control.
Write clear commit messages: separate subject (≤50 chars) and body (≤72 chars per line).
Use .gitignore templates to exclude unnecessary files.
Develop on feature branches, not directly on main.
Use release branches and tags for versioning.
Essential Commands
# stage changes
$ git add <file/dir>
# commit with message
$ git commit -m "some info"
# push to remote
$ git push origin master
# discard local changes
$ git checkout -- <file>
# reset to a previous commit
$ git reset --hard <commit>Merge vs Rebase
Merge preserves the complete history, useful for auditability, while rebase creates a linear history, ideal for clean main branches. The rule of thumb: rebase only unpublished local commits; never rebase commits that have been pushed.
Updating Repository History
Use interactive rebase ( git rebase -i HEAD~5 ) to squash, reword, edit, or drop commits. Example of a rebase todo list:
pick c2aeb6e 3rd commit
reword 25a3122 4th commit
squash bd5d32f 6th commit
drop 581e96d 7th commitStash for Interruptions
Save uncommitted work with git stash , list with git stash list , apply with git stash apply , or pop with git stash pop . Stash can include untracked files using -u .
Efficient Cloning
Shallow clone the latest snapshot: git clone --depth=1 <repo_url> . To clone a specific tag without full history:
$ git init project
$ git remote add origin <repo_url>
$ git -c protocol.version=2 fetch origin 15.0.1 --depth=1
$ git checkout FETCH_HEADSkip large LFS files during clone with GIT_LFS_SKIP_SMUDGE=1 git clone <repo_url> .
Hooks and Automation
Git provides client‑side and server‑side hooks in .git/hooks . Enable a hook by removing the .sample suffix. Example of a pre‑push hook to block WIP commits:
# pre-push sample (remove .sample to activate)
#!/bin/sh
if git log -1 --pretty=%B | grep -i "WIP"; then
echo "Push blocked: commit contains WIP"
exit 1
fiUse the pre-commit framework to run linters (e.g., flake8 ) before commits or pushes.
Advanced History Rewrites
For global changes (e.g., fixing author email), use git filter-branch with a custom commit filter:
# Change author email globally
$ git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" == "[email protected]" ]; then
GIT_AUTHOR_NAME="escape";
GIT_AUTHOR_EMAIL="[email protected]";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEADAfter rewriting, push with git push -f or use reflog to recover lost commits.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.