Git Branch Management Strategies and Operations
Understanding Git Branches
Git branches enable parallel development by creating independent lines of work. Each branch maintains its own commit history while sharing a common base. The master branch typically serves as the primary development line, while feature branches facilitate isolated work on specific tasks.
Creating Branches
To create a new branch:
git branch feature-development
This command generates a new branch named "feature-development" based on the current commit. View all existing branches with:
git branch
The asterisk (*) indicates the currently active branch.
Switching Between Branches
Change your working context to a different branch using:
git switch feature-development
This command updates the HEAD reference to point to the specified branch, making it the active working branch.
Branch Merging
After completing work on a feature branch, integrate changes back to the main branch:
git switch main
git merge feature-development
Fast-forward merging occurs when the target branch hasn't diverged, simply advancing the pointer to the latest commit.
Removing Branches
Delete merged branches to maintain repository cleanliness:
git branch -d feature-development
This command removes the specified branch after verifying it has been succesfully merged.
Handling Merge Conflicts
Conflicts arise when competing changes modify the same file sections. Git marks conflicting regions:
<<<<<<< HEAD
Current branch content
=======
Incoming branch content
>>>>>>> feature-branch
Resolution involves manually editing the file to select desired changes, then committing the resolved version:
git add resolved-file.txt
git commit -m "Resolve merge conflict"
Temporary Work Preservation
When interrupted by urgent tasks, save current work without committing:
git stash push -m "Work in progress"
Restore stashed changes later:
git stash pop
Integration Strategies
Before merging feature branches into main, update them with latest main changes:
git switch feature-branch
git merge main
This approach resolves potential conflicts in the feature branch rather than the main development line.
Forceful Branch Removal
Delete unmerged branches when their changes are no longer required:
git branch -D abandoned-feature
This command bypasses safety checks and permanently removes the specified branch.