Walkthrough on how to handle merge conflicts in Git

Git-Logo-2ColorGit is distributed version control system designed to handle everything from small to very large projects with speed and efficiency.The foundation of DevOps, begins with using source code control. This includes the source control for your Infrastructure as Code.But sometimes, when you check in your code, either you or someone else has been working made a change that creates a conflict between branches.

When the same part of the same file in two branches have been changed, Git won’t be able to figure out which version to use. When such a situation occurs, Git stope your right before the merge commit, where you will need to resolve the conflicts manually.

In this walkthrough, you set up a new repository, make changes to the repository where so changes conflict with those on your local machine, merge the changes, and push the changes to the repository.

This walkthrough take you on the steps for the common scenario how to resolve the merge conflicts, such as shown in the following illustration (provided by Microsoft).

merge-conflict

To set up the the merge conflicts, you will follow these steps:

  1. Create a file on the master branch and push it to the master branch.
  2. Create a new branch locally, pull the branch and make some changes to the file in GitHub and commit the change.
  3. Go into the GitHub editor and make a change in the original file in the master branch, can commit the change.

Now that you have two different versions of the same file — some text on the master branch in GitHub that differs from the code on your local machine — both are committed. To solve the conflict you will:

  1. Use Visual Studio Code to select and save the changes.
  2. Complete the merge and push your merged changes to GitHub repo.

Prerequisites

You will need:

The REPOSITORY_URL will look like:

  • For GitHub: REPOSITORY_URL="https://github.com/$ORGANIZATION_URL/$PROJECT_NAME.git"
  • For Azure DevOps: REPOSITORY_URL="https://dev.azure.com/$ORGANIZATION_URL/$PROJECT_NAME"

Set up a Git repository with a file in master branch

In this step, you will connect the repository in GitHub using, then add a file to the master branch using the same steps in the article, Set up Git with repository on GitHub or Azure DevOps Repos.

First, create a new directory and clone the repo into your new directory.

mkdir myclonedrepo && cd myclonedrepo
git clone $REPOSITORY_URL

view raw
clone-repo.bash
hosted with ❤ by GitHub

Use the following code to connect the new repository in GitHub.

git init
git status #should have nothing outsetanding
git remote #should have no remote repository linked already
git remote add origin $REPOSITORY_URL
# check to see what is connected

Create a file named newpythonfile.py and push to the master branch.

mkdir myclonedrepo && cd myclonedrepo
git clone $REPOSITORY_URL
cd $PROJECT_NAME
git status
# create a new file and add
echo "print('new')" > newpythonfile.py
cat newpythonfile.py # displays the new python file
#add the file and commit the change into the feature27 branch
git add newpythonfile.py
# see what has changed
git status
# commit
git commit -m 'added newpythonfile.py'
git push

view raw
basics-with-git.bash
hosted with ❤ by GitHub

You now have a file named newpythonfile.py on the master branch in your Git repository. Open the repo using in the portal and see the content of the file is print('new').

Now let’s make some changes to both the master branch on the repo and the master branch locally.

Make changes to the repo and locally

Let’s first make a change in the repo. You can edit the file directly in either GitHub or Azure DevOps. In GitHub, naviate to the repo, and click on the file name you just created. Click the edit icon as shown in the following illustration.

editthisfile

Edit the text of the file to print('repo'), then commit the change using Commit changes at the bottom of the page.

Next, make a change locally. The following script makes a change to the newpythonfile.py file so you can see the change is local.

echo "print('local change')" > newpythonfile.py
cat newpythonfile.py
git commit –all -m "change print statement on local machine"

You now have differences in the file in the repo and what is local.

  • In local machine, you have commited print('local change')
  • In the repo, you have commited print('repo')

Next, git pull to get your changes from the repo as shown in the following code.

git pull

view raw
git-pull.bash
hosted with ❤ by GitHub

You will see the notice: CONFLICT (content): Merge conflict in newpythonfile.py as shown in the following illustration.

conflict

This often happens when someone makes a change or when you are merging branches.
Other changes will have been made in the same file. Git tries to merge the files automatically. But in this case, changes have been made that it cannot resolve, because they are on the same line.

Merge with collaboration

So there are now conflicts that you will need to resolve conflicts before you can commit your changes.

Use git status to see the issue.

conflictstatus

Use cat newpythongfile.py to see what Git has done to the local oopy of the file. In the following illustration, you can see Git has added markers that show the changes.

headvslocal

You can edit the text of the file, ave and proceed. But you want want to compare the changes using Visual Studio Code to view the file and make decisions about what to include.

Resolve the conflict in Visual Studio Code

Open the file in Visual Studio Code using code newpythonfile.py. You should see the conflict as shown in the following illustration.

viewchanges

If you do not have the toolbar, click File | Preferences | Settings, then search for codelens. The following illustration shows the setting for codelens.

codelens

To see side by side comparison of the changes, click Compare Changes in the toolbar. The following illustration shows the changes side by side.

viewchanges2

When you have multiple changes, use the arrow button in the upper right to walk through your changes.

Click Accept Both Changes and save the file.

Next, click the Source Control icon in the left toolbar. Right-click the changes and click Accept Changes. You are now ready to add the changes to git and check in.

Complete the checkin

Next, add the files and commit as shown in the following script.

git add .
git commit –all -m "fixed merge with master on newpythonfile.py"
git status
# Your branch is ahead of 'origin/master' by 2 commits.
# check to see what might have changed while you were working
git pull
# should be up to date and ready to push

Push the changes using the following code.

git push origin master

view raw
git-push-sample.bash
hosted with ❤ by GitHub

Now you can view the changes in your repository.

pushedchanges

Git merge summary

In this walkthrough, you set up a new repository, made changes to the repository where so changes conflict with those on your local machine, merged the changes, and pushed the changes to the repository.

References


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s