Overview
Branches are fundamental to Git workflows, allowing you to develop features, fix bugs, and experiment without affecting the main codebase. GitHub Desktop makes branch management intuitive and visual.Create Branches
Create new branches from any commit or branch
Switch Branches
Quickly switch between branches with automatic stashing
Merge Branches
Merge branches with conflict detection and resolution
Track Remote
Automatically track branches with upstream remotes
Understanding Branches
Branch Basics
A branch is a pointer to a specific commit in your repository’s history. Branches allow you to:- Develop features in isolation
- Fix bugs without affecting production code
- Experiment with new ideas safely
- Collaborate with others on specific changes
Default Branch
Every repository has a default branch (usuallymain or master):
- New commits are made to this branch by default
- Pull requests typically merge into this branch
- The default branch represents the production or stable code
Creating Branches
Create from Current Branch
Create from Specific Commit
Branch Creation Implementation
When creating a branch from a remote branch, GitHub Desktop automatically adds
--no-track to prevent accidentally pushing to the fork’s upstream.Switching Branches
Switch to Existing Branch
Automatic Stashing
When switching branches with uncommitted changes:- GitHub Desktop automatically creates a stash
- Switches to the target branch
- Attempts to apply the stash
- If conflicts occur, leaves the stash for manual application
Branch List Organization
The branch dropdown organizes branches into sections:Default Branch
- Shows the repository’s default branch at the top
- Usually
mainormaster
Recent Branches
- Branches you’ve worked on recently
- Sorted by last checkout time
Other Branches
- All other local branches alphabetically
Pull Request Branches
- Branches associated with open pull requests
- Grouped by PR status
Remote Branches
- Branches on the remote that don’t exist locally
- Can be checked out with one click
Renaming Branches
Rename the current branch or any other branch:Rename Implementation
Deleting Branches
Delete Local Branch
Delete Remote Branch
Delete Implementation
Merging Branches
Merge into Current Branch
Merge Strategies
GitHub Desktop uses standard Git merge strategies:- Fast-forward: When possible, simply moves the branch pointer forward
- Merge commit: Creates a new merge commit when branches have diverged
- Conflict resolution: Stops and asks for manual resolution when conflicts occur
Comparing Branches
View differences between branches:Branch Status Indicators
Branches in the list show visual indicators:- Ahead/Behind: Shows how many commits ahead or behind the upstream
- Current Branch: Bold text and checkmark icon
- Default Branch: Star icon
- Published: Cloud icon indicates the branch exists on remote
- PR Indicator: Icon shows if there’s an associated pull request
Finding Merged Branches
Identify branches that have been fully merged:Upstream Tracking
Publishing Branches
Publish a local branch to the remote:Setting Upstream
When publishing a branch, GitHub Desktop automatically:- Pushes the branch to
origin - Sets up upstream tracking with
--set-upstream - Enables pull/push for the branch
Branch Naming Conventions
Common branch naming patterns:Protected Branches
When working with protected branches on GitHub:- Force push blocked: Cannot force push to protected branches
- Review required: May require pull request reviews before merging
- Status checks: CI/CD checks must pass before merging
- Admin bypass: Repository admins may have special permissions
Best Practices
- Use Descriptive Names: Branch names should clearly indicate their purpose
- Keep Branches Short-Lived: Merge or delete branches after completing work
- Update from Main Regularly: Keep feature branches up to date with the default branch
- Delete Merged Branches: Clean up branches after merging to avoid clutter
- Don’t Commit to Main Directly: Use feature branches for all changes
- Use Prefixes: Organize branches with prefixes like
feature/,fix/,docs/
Keyboard Shortcuts
| Action | Windows/Linux | macOS |
|---|---|---|
| Switch branch | Ctrl+B | Cmd+B |
| New branch | Ctrl+Shift+N | Cmd+Shift+N |
| Find branch | Ctrl+P | Cmd+P |
Troubleshooting
Cannot Switch Branches
Cannot Switch Branches
If you can’t switch branches:
- Uncommitted changes conflict: Commit, stash, or discard changes
- Unmerged files: Resolve merge conflicts before switching
- Detached HEAD: Create a branch from current commit first
Branch Deleted by Mistake
Branch Deleted by Mistake
If you accidentally deleted a branch:
- Find the commit SHA in the History tab
- Create a new branch from that commit
- Or use
git reflogin terminal to find the deleted branch’s tip
Merge Conflicts
Merge Conflicts
When merging causes conflicts:
- GitHub Desktop highlights conflicted files
- Resolve conflicts in your editor or using the conflict resolver
- Stage resolved files and complete the merge
- See Merge Conflicts for detailed help
Cannot Delete Branch
Cannot Delete Branch
Branches cannot be deleted if:
- It’s the currently checked out branch (switch first)
- It has unmerged commits (use
-Dflag to force) - It’s the default branch for the repository