DEV Community

loading...

Git merges and conflicts

Simran Birla
Learning web development.Trying to Learn to code. Learning to try to code.
・4 min read

Git is a very important tool for developers all around to world . Git can do much more than just creating a repository for storing your code , like :

1.git branches :

a)When you want to add a new feature to a project you create a branch. Once you create a branch the master / main branch of the repo is cloned.

b)Git branch is used for adding or testing a new feature without breaking your main app.

c)Git branch for creating the branch
Eg : git branch branch1

d)Git checkout entering the branch
Eg: git checkout branch1
You are then switched to branch1 from main/master branch in your local machine.

e)When you create a branch on your local machine and then try to commit and push its changes there will be an error saying that the branch has no upstream

$ git push
fatal: The current branch test2 has no upstream branch.
To push the current branch and set the remote as upstream,
Enter fullscreen mode Exit fullscreen mode

f)You resolve this by adding the command
git --set-upstream origin branch1(branch name)

g)This will then take the branch that is in your local machine and add it in your github repo (that is it will create the remote branch).

2.Fork :

This is used when making changes to somebody else’s repo you have no ownership on.
Steps to fork: You open the repo which you wish to fork
You can see a button in the rightmost corner with the word fork on it .Click on it and then that repo will be forked.
Once forked , a cloned repo will be created in your profile.

Fork Button

While working in a forked repo ,my advice is to create a branch and make changes in it and then merge into the master/main branch in your cloned repo.This will help you detect If there are any merge conflicts.You can then remove the merge conflicts and create a Pull requests.

3.Pull requests :

You create Pull request once you have done making changes and now you want to add them into the main branch.

When you have done changes in your forked repo you can then create a pull request from the pull request tab.When created it will give you the option of the base repo(where you want to add the changes) and head repo (where you made those changes.).It also gives you the option to select the branch of both the base and head repo.

Once a pull request has been created , it shows if there are any merge conflicts or not.If there are no merge conflicts then you can easily merge your changes into the project. But when you have merge conflicts you have to remove them and then only the changes can be merged.
You can also add a reviewer to review the changes you made.

While creating a pull request in someone else’s repo they have to approve your Pull request.

4.Conflicts :

While making a pull request you will encounter merge conflicts.If there are merge conflicts then the pull request will be accepted abut the branch cannot be merged into the main branch.

$ git merge test2
Auto-merging Example
CONFLICT (content): Merge conflict in Example
Automatic merge failed; fix conflicts and then commit the result.
Enter fullscreen mode Exit fullscreen mode

Reasons for conflicts:
1.When one file is edited in one PR but deleted in another.
2.When the same line in the file has been edited in 2 different branches

Once you know the reason for conflict you can solve it directly from git or using command line.

Git status : It will tell you that there are the merge conflicts in the branch and also will tell you in which file there are merge conflicts.

$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   Example
Enter fullscreen mode Exit fullscreen mode

Cat : this will give you the contents of file where you have the merge conflicts.
You will see something like

>>>>>HEAD
//* some lines
====
//* some lines
>>>>>branch name
Enter fullscreen mode Exit fullscreen mode

This line ==== is conflict divider.
The statements between HEAD and ==== is already present in your branch where you are making changes
And The statements between ===== and branch name are the ones that you want to add.
So , here is your conflict , You can solve this conflict by either accepting the lines already there or by adding the new changes or adding both

To solve this,
Open code editor if your choice ,
conflicts on VScode

I have used VS code here , you can see it shows the conflict block,It also provides you with the option of accepting current changes, Accepting incoming change , accept both the changes and compare changes.
Once you choose the options , git add . and then git commit -m “merged resolved”.
This can then resolve all the merge conflicts.

You can solve merge conflicts directly from github too. While creating a pull request it will show that there are merge conflicts.Once pull request is created you can resolve conflicts. Github opens an editor you can then manually edit what you want to keep. Once done the marked resolved button will be active and then you can merge the branch.

Github Merge resolve

Discussion (0)