Merging
Branching makes it super easy to work within selfcontained contexts, but often we want to incorporate changes from one branch into another. We can do this using the git merge command
Remember these two merging concepts:
- We merge branches, not specific commits.
- We always merge to the current HEAD branch.
To merge, follow these basic steps:
- Switch to or checkout the branch we want to merge the changes into (the receiving branch).
- Use the git merge command to merge changesfrom a specific branch into the current branch.
git switch master => Switch to the master branch
git merge branchName => merge branchName to master branch
What happen if we add a commit on the master branch and then try to merge
Imagine one of our teammates merged in a new feature or change to master while we were working on a branch. If we try to merge branch to master branch then git will prompt a merge conflict issue and our merge will not be done. which we need to manually resolve.
When we encounter a merge conflict, Git warns us in the console that it could not automatically merge and It also changes the contents of our files to indicatethe conflicts that it wants us to resolve.
<<<<<<< HEAD
Some lines
=======
Some lines
>>>>>>> bug-fix
<<<<<<< HEAD
Some lines The content from your current HEAD (the branch we are trying to merge content into (master) is displayed here.
Some lines
>>>>>>> bug-fix The content from your current HEAD (the branch we are trying to merge content into (master) is displayed here.
Resolving Conflicts
Whenever we encounter merge conflicts, follow these steps to resolve them:
- Open up the file(s) with merge conflicts.
- Edit the file(s) to remove the conflicts. Decide which branch's content you want to keep in each conflict. Or keep the content from both.
- Remove the conflict "markers" in the document
- Add your changes and then make a commit!