I am working on a project where we have 2 different branches branched out from Master branch. One is Branch A, where the Backend Team works, and Branch B where Frontend Team(Me) works. Now the Branch A has some new additions that needed to be included in Branch B, how do I do get the new changes from Branch A to be reflected in Branch B without having conflicts?
Master Branch --> Branch A (Branch A has new changes which Branch B needs to have)
|--> Branch B (How to pull the changes from Branch A to B remotely in GitHub?
and what are the other ways, I can use to achieve the same objective?
CodePudding user response:
Pull Request
As @Shamim mentioned in the comments, you coukd create a pull request from branch A to B. The pull request would then allow you to fix potential conflicts and then merge it.
CLI merge
Another possibility would be to merge it via the command line. For doing so, you first need to fetch your remote so you have the latest state of branch A and pull branch B so you have the latest changes there as well. Then, you can merge branch A to branch B. This creates a commit to branch B containing all previous changes to branch A. Finally, you need to push the changes.
git checkout B # make sure you are in branch B
git fetch # download all changes/commits
git pull # update branch B
git merge A # merge A into B
git push # push B
avoiding and fixing conflicts
(Merge) Conflicts occur when the same part of the same file is changed in multiple branches and those are then merged. You shouldn't have any conflicts if the backed and fronted files are clearly seperated and different branches are used only for changes to either the frontend or backend.
If there are conflicts in a CLI merge (the git merge
command
), git tells you files that conflict, lets you to fix the conflicts manually, run git add
for staging the resolved conflicts and git commit
for committing the merge.
The GitHub UI also allows you to fix simple merge conflicts but in some cases, you would still need to merge using the CLI even though you have created a Pull Request.
However, creating Pull Requests is (often) a best practice as it provides documentation on the history of the repository and allows you to review changes/etc.
CodePudding user response:
You can do this via pull requests or the command line. It's up to you, really. From the CLI, you can do the following:
git checkout B
git pull origin A # A pull is a fetch and a merge in one.
git push # Assuming no conflicts
If there are conflicts, resolve them manually and then add them via git add
. You can also check to make sure you didn't miss any conflict markers with git diff --check
(add --cached
if you already added the files to the index). When everything resolved you can do git merge --continue
to complete the merge and push afterwards.