Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member186879
Active Participant
In this blog post I describe two common Git flows (merge and rebase). I will purposely add conflicts to these flows so I can then show you how to resolve them and continue with the Git operation.

For the purpose of this demo, I created a simple Git repository (on GitHub.com) with a single remote master branch (origin/master).

In SAP Web IDE, I created two local branches based on the origin/master branch:

  • master (created automatically by SAP Web IDE when I cloned the repository)

  • feature1


The Git repository contains a single file named sample.txt with the following content:



Now we will create the conflict as follows:

  1. Switch to the feature1 branch and open the sample.txt file for editing.

  2. Let’s make the following changes to the file (change row #3 from 333 to 555):

  3. Save the file.

  4. In SAP Web IDE, open the Git pane and make sure you see the txt file in the staging table.

  5. Stage the file.

  6. Enter a commit message and commit the file. The staging table should now be empty.

  7. Switch back to the master branch.

  8. Open the sample.txt for editing. You should still see 333 in line #3.

  9. Edit the file (now in the master branch). Change row #3 from 333 to 666. This will create the conflicting change that Git cannot resolve on its own.

  10. Save the file.




The sample.txt file appears in the staging table in the Git pane (now in the master branch).

  1. Stage the file.

  2. Add a commit message and commit the file to Git.


Let’s recap… you modified the same code line in the same file but in two different branches and committed these changes to Git.

Now comes the interesting part…

Let’s assume that we want to incorporate the changes done in the feature1 branch to the master branch. There are 2 ways in which we can do this: rebase and merge.

You can read this excellent tutorial about the differences between merge and rebase.

We’ll start with rebase:

Rebase


Let’s see what happens when we try to rebase.

While still on the master branch, click on Rebase in the Git pane, and select the feature1 local branch as the source for the rebase operation.

The following message appears:



Click OK.

Now the Git pane is in “Rebase Interactive” mode.



You can see that the sample.txt file is in conflict – the little red icon next to the file name indicates the conflict status .

To resolve the conflict, double-click the file in the staging table to open the Compare editor.



On the right, you can see the file from the source branch (feature1) and on the left you can see the file from the target branch (master). Row #3 is conflicting and we need to decide which value we want.

For the sake of this example, let’s assume that the correct value is the one coming from the feature branch (i.e. 555).

Click on the “Copy from right to left” button.

This will copy the 555 from the feature1 branch version to the master branch.

You can also manually edit the file on the left with a different value (yes, the compare editor is also editable).

Save and stage the file.

Click Continue in the Git pane.

Now you should be back to the standard view of the Git pane with your changes merged from feature1 to master.

You can now push the changes to the remote Git repository.

Merge


Now let’s see how this works when merging.

Repeat steps 1-13 in the previous section to recreate the conflict.

While still on the master branch, click on Merge in the Git pane, and select the feature1 local branch as your source for the merge operation.



Click OK.

The following message appears:



Click OK.

Now the Git pane is in “Merge Interactive” mode.



You can see that the sample.txt file is in conflict.

As we saw in the previous section, we can use the compare editor to fix and resolve this conflict.

However, I want to show you how this is done using the standard code editor.

Open the sample.txt file in the code editor by clicking the 3 dots in the staging table and selecting Edit.



This is what the file looks like in the code editor:



The code that was changed in the master branch appears under the <<<<<<< HEAD statement:



The code that was changed in the feature1 branch appears above the >>>>>>> feature1 statement:



All we need to do now is to clean-up the code and leave only the text that we want to keep in our file. Let’s assume we want to keep the 555 form the feature1 branch. This is what the file should look like after our edit:



Save and stage the file.

Add a commit message and commit the change.

This is what the commit history looks like after the change was merged to the master branch:



You can now push the changes to the remote Git repository.

 

So what have we learnt?

We saw how easy it is to resolve conflicts using the SAP Web IDE Git pane.

You can do it using the Compare editor or by manually editing the relevant file in the code editor.

I hope you found this helpful. If there is another Git-related topic on which you would like to learn or get helpful tips, please let me know in the comments section below.