Sub topic 7
Short-lived and long-lived branches
This chapter assumes you can create, commit on, and merge branches, and that you know how a branch is a pointer to a commit. Here you will learn how branch lifetime affects merge pain, and why most workflows in later chapters depend on it.
Two kinds of branch
A short-lived branch exists for hours or a few days. It holds one small change, gets reviewed, merges into the main branch, and is deleted. A long-lived branch exists for weeks, months, or the life of the project. Examples are main, develop, and release/1.4.
Lifetime is a property of how you use a branch, not of Git. Git treats both the same.
| Property | Short-lived | Long-lived |
|---|---|---|
| Typical age | Hours to days | Weeks to permanent |
| Purpose | One change or fix | Shared line of work |
| Divergence from main | Small | Can grow large |
| Merge conflicts | Rare and small | Frequent and large |
| Deleted after merge | Yes | No |
Why age matters
While your branch lives, main keeps moving. Every commit that lands on main after you branched is a change your branch has not seen. The longer you wait, the more files both sides touch, and the more conflicts you resolve at merge time.
Old branches have a second cost. Reviewers must read a large diff, which they do less carefully. Bugs from a big merge are also harder to trace to one commit.
Keeping a branch short
These habits keep branches small:
- Split work into pieces that can each merge on their own.
- Open a pull request early, and merge as soon as checks pass and a reviewer approves.
- Delete the branch after merging.
- Hide unfinished behavior behind a feature flag, covered in a later chapter, so partial work can merge safely.
If a branch must live a few days, sync it with the main branch regularly:
git switch feature/login-form
git fetch origin
git merge origin/mainYou can use git rebase origin/main instead if your team rebases. Either way, you resolve small conflicts daily instead of one large conflict at the end.
When long-lived branches are the right tool
Some branches should stay. A release/1.4 branch keeps receiving fixes after main has moved to 1.5. A develop branch in Gitflow collects finished features before a release. These branches have a clear owner, a clear rule for what may enter them, and usually branch protection.
The failure mode is a long-lived feature branch. It started as a short branch and never merged. Warning signs include:
- It is more than a week behind
main. - Its diff touches dozens of files.
- Nobody remembers what finishing it requires.
Finding stale branches
You can list remote branches by last commit date to spot old ones:
git fetch --prune
git for-each-ref --sort=committerdate refs/remotes/origin \
--format='%(committerdate:short) %(refname:short)'The oldest entries at the top are candidates for a check-in with their owners.
Exercise
Your feature branch is nine days old and main has received 40 commits since. Give two problems this causes and two actions that reduce them.
Show answer
Problems: merging will likely produce many or large conflicts, and reviewers face a large diff that is hard to review carefully. Actions: merge or rebase origin/main into the branch now, then split the remaining work into smaller pull requests, or hide unfinished parts behind a feature flag so they can merge sooner.