Git 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).
To set up the the merge conflicts, you will follow these steps:
- Create a file on the master branch and push it to the master branch.
- Create a new branch locally, pull the branch and make some changes to the file in GitHub and commit the change.
- 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:
- Use Visual Studio Code to select and save the changes.
- Complete the merge and push your merged changes to GitHub repo.
You will need:
- Install Git on your development computer. For more information, see Getting Started – Installing Git in the Git documentation.
- Visual Studio Code on Windows, Ubuntu, or Mac.
- A Git repository with the value stored in an environment variable
REPOSITORY_URL. See Set up Git with repository on GitHub or Azure DevOps Repos for how to set up a Git repository.
You will want an understanding of Git basics. See our previous post, Cheatsheet and best practices for Git.
REPOSITORY_URL will look like:
- For GitHub:
- For Azure DevOps:
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.
Use the following code to connect the new repository in GitHub.
Create a file named
newpythonfile.py and push to the master branch.
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
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.
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.
You now have differences in the file in the repo and what is local.
- In local machine, you have commited
- In the repo, you have commited
git pull to get your changes from the repo as shown in the following code.
You will see the notice:
CONFLICT (content): Merge conflict in newpythonfile.py as shown in the following illustration.
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.
git status to see the issue.
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.
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.
If you do not have the toolbar, click File | Preferences | Settings, then search for
codelens. The following illustration shows the setting for 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.
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.
Push the changes using the following code.
Now you can view the changes in your repository.
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.
- Visual Studio Code how to resolve merge conflicts with git?
- Been, Henry. Implementing Azure DevOps Solutions: Learn about Azure DevOps Services to successfully apply DevOps strategies. Packt Publishing.
- To use Visual Studio 2019 or later, see Resolve merge conflicts