Git allows developers to work on separate branches to develop features independently. When a branch is stable, it can be merged into the master branch. To merge a branch:
1. Create and checkout a new branch, make changes, and push the branch to the remote repository.
2. Pull the latest changes from the master branch into the local repository.
3. Checkout the feature branch and merge it into the master branch. Conflicts may occur if the same files were changed in both branches and must be resolved manually before merging.
2. Git Merge
• When you work on a big project, it is better to push your code in a
separate branch.
• Store all your work in that branch by git push command.
• When your branch is stable, then update the master branch with your
branch.
3. Create a Branch
• When creating a new branch, make sure you are on the master
branch
• “git branch” –> will print the current branch you are in
• git checkout -b “YOUR_BRANCH_NAME” –> to create a new branch
• git add –A –> to track all the changes that you made
• git commit –m “Feature X implemented” –> commit your update
• git push origin “YOUR_BRANCH_NAME” –> push all the updates to
your branch
4. Prepare your branch
• “git fetch” –> to up to date your local repository with the latest
changes from the remote server
• “git checkout master” –> checkout to the master branch
• “git pull origin master” –> to up to date the local master branch with
the remote branch
5. Merge with master branch
• “git checkout YOUR_BRANCH” –> to checkout to your branch
• “git merge master” –> merge your branch with the master branch
Now your branch will contain all the updated code from the master
branch. If there is no conflict then there will be no error in your code
and you can execute the program easily.
6. Conflicts
If more than one people work on the same file, then there is a high
chance of facing conflicts
• <<<<<<< –> Conflict marker, the conflict starts after this line
• ======= –> Divides your changes from the changes in the master
branch
• >>>>>>> –> End of the conflicted lines
7. Resolving Conflicts
• You need to resolve the conflict manually by going to the file where it
happened
• Decide if you want to keep your changes or master branch changes by
deleting the conflict markers.
1. <<<<<<< HEAD(master)
2. conflicted code from HEAD(master)
3. =======
4. conflicted code from YOUR_BRANCH
5. >>>>>>> YOUR_BRANCH
To keep master branch changes, you need to delete 1, 3-5
To keep YOUR_BRANCH changes, you need to delete 1-3, 5
8. Merge Request
• After resolving the conflict, you can push the code to your branch by
“git push origin YOUR_BRANCH”
• Before making a merge/pull request, make sure your branch is
stable/working and it has no conflict with the master branch
• You can submit the merge/pull request by clicking the pull request
button and then submit the request describing with the changes that
you made