Fundamentals 18 min read

Common Enterprise Git Workflows and Best Practices

This article provides a comprehensive guide to common enterprise Git workflows, daily best‑practice tips, essential commands, configuration options, merge vs. rebase decisions, history rewriting techniques, reflog usage, batch history editing, hook scripts, fast cloning of large repositories, and handling work interruptions, all illustrated with clear examples and code snippets.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Common Enterprise Git Workflows and Best Practices

Common Enterprise Workflows

This section introduces the most frequently used Git workflows in enterprises, including Git Flow, GitHub Flow, and GitLab Flow, each illustrated with diagrams.

Git Flow

Main branch

Stable branch

Develop branch

Hotfix branch

Feature branch

GitHub Flow

Create a branch

Add commits

Open a pull‑request

Discuss and review code

Deploy for testing

Merge the code

GitLab Flow

Production branch

Environment branch

Release branch

Daily Best‑Practice Guidelines

Key recommendations for everyday Git usage:

Prefer the command line over graphical tools for efficiency.

Write clear commit messages: separate subject and body with a blank line, keep the subject under 50 characters, wrap body lines at 72 characters, and avoid a trailing period on the subject.

Use a .gitignore file to exclude unnecessary files, possibly based on a template.

Adopt a branch‑or‑fork development model; never commit directly on the main branch.

Employ release branches and tags for version management (e.g., release/1.32 , tags like A‑feature , B‑feature , C‑bugfix ).

Common Command Summary

Remember these six core commands for daily work:

# work‑tree -> index
$ git add
# index -> local repository
$ git commit -m "some info"

# local repository -> remote repository
$ git push origin master
# work‑tree <- index
$ git checkout --
# index <- local repository
$ git reset HEAD
# local repository <- remote repository
$ git clone
$ git fetch upstream master
$ git pull upstream master
$ git pull --rebase upstream master

Configuring Useful Options

Global configuration examples:

# user information
$ git config --global user.name "your_name"
$ git config --global user.email "your_email"

# editor
$ git config --global core.editor "nvim"

# pager
$ git config --global core.pager "more"

# alias
$ git config --global alias.gs "git status"

# autocorrect
$ git config --global help.autocorrect 1

Personal configuration

# without --global, settings apply to the current repository
$ git config --list
$ git config user.name "your_name"
# view the repository‑specific config file
$ cat .git/config
[user]
    name = "your_name"

Choosing Merge vs. Rebase

Merge preserves the complete history, which is valuable for auditability, while rebase creates a linear, cleaner history by eliminating merge commits. The general rule is to rebase only local, unshared commits and never rewrite history that has already been pushed.

Updating Repository History

Techniques for cleaning up commits before a pull request:

# edit the last five commits
$ git rebase -i HEAD~5
# example actions: reword, squash, pick, fixup, drop

Using Reflog to Recover Lost Work

Reflog records all reference updates, allowing you to locate and restore lost commits.

# view reflog
$ git reflog
# recover a lost commit
$ git cherry-pick

Batch Modifying History

Use git filter-branch to rewrite author information, remove sensitive data, or delete large files across the entire history. Always test on a separate branch first.

# change author email globally
$ git filter-branch --commit-filter '
    if [ "$GIT_AUTHOR_EMAIL" == "[email protected]" ]; then
        GIT_AUTHOR_NAME="new_name";
        GIT_AUTHOR_EMAIL="[email protected]";
        git commit-tree "$@";
    else
        git commit-tree "$@";
    fi' HEAD

Flexible Hook Scripts

Git hooks located in .git/hooks can enforce policies. Sample client‑side hooks include pre‑commit , commit‑msg , etc. Enable a hook by removing the .sample suffix and writing it in any language that returns an appropriate exit code.

# list available hook samples
$ ls .git/hooks
applypatch-msg.sample
commit-msg.sample
pre-commit.sample
pre-push.sample   # prevents pushing commits with "WIP"

Integrate the pre‑commit framework to run checks such as trailing‑whitespace removal and flake8 linting before pushes:

# install pre‑commit
$ pip install pre-commit
# install hooks for pre‑push
$ pre-commit install -f --hook-type pre-push
# configure checks in .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
  rev: v2.9.2
  hooks:
    - id: trailing-whitespace
    - id: flake8
# run checks on push
$ git push origin master

Fast Cloning Large Projects

Use shallow clones to fetch only the latest snapshot, optionally targeting a specific tag, and skip large LFS files when necessary.

# shallow clone (no history)
$ git clone http://example.com/repo.git --depth=1

# shallow clone a specific tag
$ git init repo-tag
$ git remote add origin http://example.com/repo.git
$ git -c protocol.version=2 fetch origin v1.2.3 --depth=1
$ git checkout FETCH_HEAD

# skip LFS files
$ GIT_LFS_SKIP_SMUDGE=1 git clone http://example.com/repo.git

Handling Work Interruptions

When you need to switch context, stash your changes to keep the working directory clean, then later apply or pop the stash.

# stash current changes
$ git stash
# include untracked files
$ git stash -u
# list stashes
$ git stash list
# apply a specific stash
$ git stash apply stash@{n}
# pop the latest stash (apply and delete)
$ git stash pop
# drop a stash
$ git stash drop stash@{n}
# clear all stashes
$ git stash clear

Alternatively, commit your work (or amend the latest commit) and push to a remote branch for backup before switching tasks.

# amend the last commit
$ git commit --amend -m "some info"
# reset to a previous commit (mixed, soft, hard)
$ git reset
$ git reset --mixed
$ git reset --soft
$ git reset --hard
workflowgitbest practicesCommand-lineversion controlRepository Management
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.