Master Git Workflows: Git Flow, GitHub Flow, GitLab Flow & Advanced Tips
This guide walks you through common enterprise Git workflows, daily best‑practice habits, essential commands, merge vs. rebase decisions, history rewriting, stash handling, hook scripts, shallow cloning, and other advanced techniques to help you manage version control efficiently.
Common Enterprise Workflows
Typical Git workflows used in companies are introduced with visual diagrams.
Git Flow : main branch, stable branch, develop branch, feature branch, patch branch, hotfix branch.
GitHub Flow : create a branch, add commits, open a PR, discuss and review, deploy, merge.
GitLab Flow : production branch, environment branch, release branch.
Daily Best Practices
Prefer the command line over graphical interfaces for speed and efficiency.
Write clear commit messages: separate subject and body, keep the subject under 50 characters, wrap body lines at 72 characters, and omit a trailing period in the subject.
Use a .gitignore template to exclude unnecessary files.
Never develop directly on the main branch; create a new branch for each feature or bug‑fix.
Use release branches and tags for version management (e.g., release/1.32, tags A‑big‑feature, B‑small‑feature, C‑bugfix).
Common Commands Summary
Only six commands are needed for everyday work.
# 工作区 -> 暂存区
git add <file/dir>
# 暂存区 -> 本地仓库
git commit -m "some info"
# 本地仓库 -> 远程仓库
git push origin master # push master to origin # 工作区 <- 暂存区
git checkout -- <file>
# 暂存区 <- 本地仓库
git reset HEAD <file>
# 本地仓库 <- 远程仓库
git clone <git_url>
git fetch upstream master
git pull upstream master
git pull --rebase upstream master # 工作区 <- 本地仓库
git reset <commit>
git reset --mixed <commit>
git reset --soft <commit>
git reset --hard <commit>Merge vs. Rebase
When to use merge : preserve the complete history as a factual record; avoid rewriting history that has already been shared.
When to use rebase : keep a clean linear history on local branches before they are pushed; avoid duplicate commits caused by merges.
# Example merge commit graph
* 62a322d (HEAD->master) Merge branch 'hotfix3' into master
|\
| * 6fa8f4a (hotfix3) 3rd commit in hotfix3
* | 548d681 3rd commit in master
|/
* 6ba4a08 2nd commit
* 22afcc1 1st commit # Example rebase interactive commands
git rebase -i HEAD~5
# edit, reword, squash, fixup, drop etc.Rewriting History
Amend the latest commit: git commit --amend or git commit --amend -m "new message".
Drop or edit specific commits with git rebase -i (pick, reword, edit, squash, fixup, exec, drop).
Use git revert to create a new commit that undoes a previous change.
Cherry‑pick individual commits: git cherry-pick -x <commit>.
Rewrite author information globally with git filter-branch.
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' HEADHooks and Automation
Git provides client‑side and server‑side hook scripts located in .git/hooks. Sample hooks have a .sample suffix and must be renamed to become active. Hooks can be written in any language as long as they exit with the appropriate status code.
Popular hook frameworks: pre-commit – installs and runs checks before commits or pushes. pre-commit-hooks – a collection of ready‑made Python hooks (e.g., trailing‑whitespace, flake8).
# Install pre‑commit
pip install pre-commit
# Enable a hook for push
pre-commit install -f --hook-type pre-push
# Example .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.9.2
hooks:
- id: trailing-whitespace
- id: flake8Efficient Cloning
For large repositories, use shallow clones to fetch only the latest snapshot:
git clone http://example.com/repo.git --depth=1To clone a specific tag without full history:
git init myproject
git remote add origin http://example.com/repo.git
git -c protocol.version=2 fetch origin 15.0.1 --depth=1
git checkout FETCH_HEADSkip downloading LFS objects during clone when they are not needed:
GIT_LFS_SKIP_SMUDGE=1 git clone http://example.com/repo.gitHandling Work Interruptions
Use git stash to save uncommitted changes on a stack and restore them later:
# Save current changes
git stash
# Include untracked files
git stash -u
# List stashes
git stash list
# Apply a stash
git stash apply stash@{0}
# Pop (apply and drop) the latest stash
git stash pop
# Drop a specific stash
git stash drop stash@{0}
# Clear all stashes
git stash clearAlternatively, push your work to a remote branch for backup before switching contexts.
Additional Useful Commands
Reset to a previous commit: git reset --hard <commit>.
Force‑push after a rewrite: git push origin master -f.
Delete a local branch: git branch -D <branch_name>.
View reflog to recover lost commits: git reflog then git cherry-pick the desired commit.
Original article link: https://www.escapelife.site/posts/f6ffe82b.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
