To merge changes from a feature branch into the main branch, use the following steps:
Switch to the Main Branch:
git checkout main
Merge the Feature Branch:
git merge <feature-branch>
Replace <feature-branch> with the name of your feature branch.
If a merge conflict occurs, Git will mark conflicted files. Manually resolve conflicts in those files, then:
Add the Resolved Files:
git add <conflicted-file1> <conflicted-file2> ...
Complete the Merge:
git merge --continue
Commit the Merge:
git commit -m "Merge branch 'feature-branch' into main"
Rebasing a Feature Branch
Rebasing is an alternative to merging for incorporating changes from one branch into another. To rebase a feature branch onto the main branch:
Switch to the Feature Branch:
git checkout <feature-branch>
Rebase onto Main:
git rebase main
Resolve Conflicts (if any).
Complete the Rebase:
git rebase --continue
Rebasing for a Cleaner Commit History
Rebasing can be used to maintain a linear commit history, avoiding unnecessary merge commits. However, avoid rebasing already-pushed commits to avoid conflicts with collaborators.
This section covers merging branches using git merge, handling merge conflicts, and rebasing in Git. Understanding these concepts is essential for managing changes and maintaining a clean commit history.