Branching and Merging in Git
Branching is one of Git's most powerful features. It allows you to diverge from the main line of development and continue to do work without messing with that main line. This is incredibly useful for developing new features, fixing bugs, or experimenting with ideas in isolation.
What is a Branch?
In Git, a branch is essentially a lightweight movable pointer to one of your commits. The default branch name in Git is typically main
(or historically master
). When you make commits, you are advancing this pointer. When you create a new branch, you are creating a new pointer to the current commit. As you commit on the new branch, it moves forward independently of other branches.
This ability to create separate lines of development is fundamental to many Modern DevOps Practices, allowing for parallel work streams and safer integration.
Basic Branching Commands
-
Create a new branch:
This creates a new branch but does not switch you to it.git branch <branch-name>
-
List all branches:
The current branch will be marked with an asterisk (*).git branch
-
Switch to a branch:
Or, with newer Git versions (2.23+):git checkout <branch-name>
git switch <branch-name>
-
Create and switch to a new branch:
Or, with newer Git versions:git checkout -b <new-branch-name>
git switch -c <new-branch-name>
-
Delete a branch:
You cannot delete a branch you are currently on.git branch -d <branch-name> # Safely delete (only if merged) git branch -D <branch-name> # Force delete
Merging
Once you've completed work on a branch (e.g., a new feature is finished and tested), you'll often want to integrate those changes back into your main development line (e.g., the main
branch). This process is called merging.
To merge, you first switch to the branch you want to merge *into* (the receiving branch), and then run the git merge
command with the name of the branch you want to merge *from* (the source branch).
# Switch to the main branch
git checkout main
# Merge the feature-branch into main
git merge feature-branch
Merge Conflicts
Sometimes, Git cannot automatically merge changes. This happens when changes on the two branches conflict – for example, if the same lines of a file were modified differently on both branches. This is a merge conflict.
When a merge conflict occurs, Git will stop the merge process and tell you which files have conflicts. You need to manually resolve these conflicts:
- Open the conflicted file(s) in your editor. Git marks the conflicted areas like this:
<<<<<<< HEAD This is the content from the current branch (e.g., main). ======= This is the content from the branch being merged (e.g., feature-branch). >>>>>>> feature-branch
- Edit the file to fix the conflict, choosing which parts to keep, or combining them as needed. Remove the conflict markers (
<<<<<<<
,=======
,>>>>>>>
). - Once resolved, stage the modified file:
git add <filename>
. - Commit the merge:
git commit
(Git usually provides a default merge commit message).
Effectively handling merge conflicts is a key skill in collaborative projects, much like understanding how different components interact is crucial in understanding microservices architecture.
git status
frequently during a merge to see the state of conflicted files.
Common Branching Workflows
While you can use branches however you like, some common workflows have emerged:
- Feature Branching: Create a new branch for each new feature you work on. This keeps your
main
branch clean and deployable. Once the feature is complete, merge it back. - Gitflow Workflow: A more structured workflow involving different types of branches (feature, release, hotfix) for larger projects.
- GitHub Flow / GitLab Flow: Simpler workflows often centered around feature branches and pull/merge requests.
Understanding branching and merging is fundamental to leveraging Git's full potential for both solo and team projects.
Next: Working with Remote Repositories ➡️