Working with GIT

Objective

After completing this lesson, you will be able to use GIT as a version control system for tracking changes in files

GIT Version Control System

In software projects, it Is important to track changes on files. A version control system helps you to track these changes. A version control is a system that records changes to a file or set of files over time so that you can recall specific versions. GIT is such a version control system. The SAP Business Application Studio comes with a built in GIT-Client to interact with the GIT-system. The GIT-system may be installed on premise or a cloud service like http://github.com. Lets take a look at the features, the states and the workflow:

Features

  • GIT works like a set of snapshots, with every commit GIT takes a snapshot of the current state of the underlying files. For unchanged files, only a reference to the file is stored.
  • GIT is a free and open source distributed version control system.
  • GIT is designed to handle small to very large projects, It is designed for speed and efficiency.
  • GIT supports local branching and staging.
  • With GIT, most operations require local files and resources only, almost every operation can be done offline.
  • GIT uses checksums based on SHA-1 hashes.

GIT States

  • Committed: The data is safely stored in the local database.
  • Modified: Files were changed but not yet committed to the local database.
  • Changed: Files were marked as modified in the current version and go into the next commit snapshot.

GIT Workflow

  • Working Tree: Modify files in the working tree.
  • Staging Area: Staging Area: Stage the files/add snapshots of files to the staging areas.
  • GIT Repository: Perform a Commit. Takes the files from the staging area and stores the snapshot permanently in the local GIT repository.

For more information, visit https://git-scm.com

GIT Procedure

The procedures for getting started with GIT repositories are provided in the following figure.

Illustration of the GIT processes: Access project , initialize, stage files and commit changes. Or access project and clone repository.

The figure shows you the two options regarding how to start with GIT repositories:

  1. Import an existing directory into GIT.
  2. Clone an existing GIT repository from a server.

Watch this video to learn about GIT Working Directories.

Work with a Local GIT Repository

Business Example

In this exercise, you will learn how to work with a local Git Repository.

Note

SAP BTP Platform and SAP Business Application Studio are cloud services. Due to the nature of cloud software, the naming of fields and buttons, as well as the content of the steps, may differ from the exercise solution provided here.

Steps

  1. Open your training SAP Business Application Studio.

  2. Create a new SAP Fiori Freestyle project using the following information.

    SAP Fiori Freestyle Project Information

    FieldValue
    TemplateSAP Fiori application
    Template TypeSAP Fiori
    TemplateBasic
    Data sourceNone
    View nameMain
    Module namerepository
    Application titleRepository-integration
    Application namespacestudent.com.sap.training.advancedsapui5
    DescriptionA Fiori Application.
    Project folder path/home/user/projects

    Leave other fields and radio buttons as default values.

    1. Perform this step as shown in a previous exercise.

  3. Open the terminal for the repository folder. Initialize the local GIT repository by executing the following command: git init.

    1. Open the terminal for the repository folder by choosing Open in Integrated Terminal in the contextual menu.

    2. Initialize the local GIT repository by executing the given command.

  4. Stage all changes over the Git view for the first commit. The Git view is located on the left side menu in the form of a branch symbol. If you open it, you will see all changes. You can stage all changes by choosing the three dots symbol and in the dropdown menu, ChangesStage All Changes.

    1. From the left side menu of the SAP Business Application Studio, choose the branch symbol (Source Control) to open the Git view.

    2. Check the commit section of the Git view (located under the Message input field). The files are marked as untracked (U) and located under Changes.

      Screenshot of the Git Pane showing all files with the flag U (Untracked).
    3. Now stage all changes for the first commit. For the staging, select the three dots symbol (Views and More actions…) and choose ChangesStage All Changes.

      Screenshot of the Git Pane menu options.
    4. Check the commit section of the Git view. As you can see, the files are now marked as added (A) and located under Staged Changes.

      Screenshot of the Git Pane showing all files with the flag A (Added).
  5. Commit the files that you have staged in your local Git repository. Use the Git view for the commit and Initial Commit as commit message. You can then go back to the Explorer pane.

    1. Commit the files that you have staged in your local Git repository. For the commit, just type in Initial Commit into the Message input field of the Git view and select the Commit button or choose the checkmark symbol.

      Screenshot of the Git Pane. A comment has been entered and files are ready for commit.
    2. There will be zero changes within the Git view:

      Screenshot of the Git Pane. Changes have been committed. No files are displayed any more.
    3. Go back to the Explorer pane.

  6. Add the listed view of type XML in the view folder and the corresponding controller of type JavaScript in the controller folder of your project. To create the files, use Copy and Paste in the context menu of the Main.view.xml and Main.controller.js files. Rename them to the listed view and controller file names. In the created Detail.view.xml file, you need to change part of the controllerName attribute from Main to Detail and remove the displayBlock attribute. Then change the value of the title attribute of the Page element to the string Title. A string is sufficient for this exercise, you do not need to use i18n. In the created Detail.controller.js file, you need to change part of the controller name in the extend function from Main to Detail.

    View and Controller File Names

    View file nameController file name
    Detail.view.xmlDetail.controller.js
    1. Add the Detail.view.xml file in the view folder and the corresponding Detail.controller.js file in the controller folder of your project. Use Copy then Paste in the context menu of the Main.view.xml and Main.controller.js files to create the files. Now, rename them.

    2. In the created Detail.view.xml file, you need to change part of the controllerName attribute from Main to Detail and remove the displayBlock attribute. Then change the value of the title attribute of the Page element to the string Title. A string is sufficient for this exercise, you do not need to use i18n.

      Code Snippet
      12345678910
      <mvc:View controllerName="student.com.sap.training.advancedsapui5.repository.controller.Detail" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m"> <Page id="page" title="Title"> <content /> </Page> </mvc:View>
    3. In the created Detail.controller.js file, you need to change part of the controller name in the extend function from Main to Detail. The last part of the string should look like the following:

      Code Snippet
      12
      repository.controller.Detail", {
  7. Stage your changes and commit them with the commit message, Detail view and controller added, and come back to the Explorer pane.

    1. Open the Git view and check the commit section. As you can see, the Detail.controller.js and Detail.view.xml files are marked now as untracked (U) and located under Changes.

      Screenshot of the Git Pane showing the two new files with the flag U (Untracked).
    2. Now, stage all changes. For the staging, select the three dots symbol (Views and More actions…) and then on ChangesStage All Changes. Alternatively, you can stage the files individually by selecting the + symbol (Stage Changes) for every file. Now, the files should be marked as added (A) and located under Staged Changes.

      Screenshot of the Git Pane showing the two files with the flag A (Added).
    3. Commit the files that you have staged in your local Git repository. For the commit, just enter Detail view and controller added into the Message input field of the Git view and select the Commit button or choose the checkmark symbol. Now, there should be zero changes.

      Screenshot of the Git Pane. Changes have been commited. No files are displayed any more.
    4. Come back to the Explorer pane.

  8. Open the Detail.view.xml file and assign the value Detail to the title attribute of the Page element. For this exercise, there is no need to use i18n, just assign the value Detail as a string. Then save your changes.

    1. Open the Detail.view.xml file.

    2. Change the value of the title attribute of the Page element to Detail:

      Code Snippet
      123456
      <Page id="page" title="Detail"> <content /> </Page>
    3. Save your changes.

  9. Stage your changes and commit them with the Commit message, Detail view title changed.

    1. Open the Git view. As you can see, the Detail.view.xml file is now marked as modified (M) and located under Changes.

      Screenshot of the Git Pane showing the view file with the flag M (Modified).
    2. Now, stage the changes. For the staging, you can select the + symbol (Stage Changes) for the Detail.view.xml file. When this action is complete, the Detail.view.xml file should still be marked as modified (M) but is now located under Staged Changes.

      Screenshot of the Git Pane showing the file with the flag M (Modified) as staged.
    3. Commit the files that you have staged in your local Git repository. For the commit, enter Detail view title changed in the Message input field of the Git view and select the Commit button or select the checkmark symbol. Now, there should be zero changes.

  10. Select the Git: View history (Git log) icon to see the history of commits. You should see all three commits done in this exercise:

    Screenshot of the Git History Pane showing all three commits.

Work with a Remote Git Repository

Business Example

In this exercise, you will learn how to create and work with a remote Git repository.

As we have seen in previous exercise, SAP Business Application Studio has a local repository for GIT. However, the recommendation is always to have the work saved in remote repositories. Remote repository can be public or private (also knows as Corporate GIT).

For this exercise, we will focus on using the Public GIT in particular GitHub https://github.com/. You can use any other if you already have an account or are familiar with the tool.

Note

SAP BTP Platform and SAP Business Application Studio are cloud services. Due to the nature of cloud software, the naming of fields and buttons, as well as the content of the steps, may differ from the exercise solution provided here.

Steps

  1. If you do not have a GitHub account, visit https://github.com/ and create one. Make sure that you are using a valid email address. Furthermore, make sure that you are able to access the email account.

    1. Go to https://github.com/ and choose Sign up.

    2. Follow the account creation process step by step.

  2. Go to https://github.com/ and log into your account. Then create a new repository with the following information:

    Repository Information

    FieldValue
    Repository nametrainingrepository
    DescriptionRepository for training
    Repository visibilityPrivate
    1. In the upper-right corner of any page, use the plus(+) dropdown menu, and select New repository.

    2. Enter trainingrepository as the Repository name.

    3. Enter Repository for training as the Description.

    4. Choose Private as repository visibility.

    5. Choose Create repository.

  3. Create a personal access token with the listed information. After the creation of the personal access token, ensure you copy and save it because you will not be able to see it again and you will need to insert it into a file of the SAP Business Application Studio. You can create the personal access token in the Developer settings of your GitHub account.

    Personal Access Token Information

    FieldValue
    Notetraining
    Select scoperepo, delete_repo and write:discussion
    1. Open the personal settings of your account by clicking on your account in the upper right corner, and choose Settings from the dropdown menu.

    2. Choose Developer settings.

    3. Choose Personal access tokens.

    4. Choose Tokens (classic).

    5. Choose Generate new tokenGenerate new token (classic).

    6. Enter training at Note and select repo, delete_repo and write:discussion at Select scopes. Choose Generate token.

    7. Make sure to copy and save your new personal access token now because you will not be able to see it again; you will need to insert it into a file of the SAP Business Application Studio.

  4. Open the SAP Business Application Studio.

  5. Open the terminal for the repository folder. Set your email address and username for your Git account by executing the following commands:

    • git config --global user.email "<your github email address >"
    • git config --global user.name "<your github username>"
    1. Open the terminal for the repository folder. Set your email address and username by executing the listed commands.

  6. In order to save the personal access token within the SAP Business Application Studio, create a .netrc file within the /home/user folder with the listed information. Only one space is required between the key and the value.

    Content of the .netrc File

    KeyValue
    machinegithub.com
    login<Your GitHub email address>
    password<Your personal access token from step 2>
    1. Choose FileOpen Folder….

    2. Enter /home/user/ and choose OK.

    3. Choose the New File icon.

    4. Enter .netrc and press the Enter key (on your keyboard).

    5. Enter the listed key-value pairs. There is no need to use : or = for the key-value pairs. Only one space is required between the key and the value.

    6. It will look like what we see in the following figure:

      Screenshot of the .netrc file.
    7. Save your changes.

  7. Open the repository workspace. Open the terminal for the repository folder and assign the new remote Git repository to your project. To assign the new remote Git repository to your project, you can use the following command with the URL of your trainingrepository remote Git repository, which can be found on GitHub: git remote add origin https://<Git-Host-URL>/<full-path-to-repository>

    1. Choose FileOpen Recent … to go back to your projects.

    2. From the context menu of the repository folder, choose Open in Integrated Terminal.

    3. Within the terminal, execute the following command with the URL of your trainingrepository remote Git repository, which can be found on GitHub: git remote add origin https://<Git-Host-URL>/<full-path-to-repository>

  8. Push the changes from your local Git repository to the remote Git repository. You can use the Push to… action from the Git view.

    Note

    If a window appears on the bottom right of your screen asking for periodical git Fetch, answer No.
    1. Open the Git view.

    2. Choose the three dots symbol (Views and More Actions…) and choose Pull, PushPush to…

      Screenshot of the Git Pane menu options. Choose Pull,Push → Push to...
    3. In the input field, choose origin.

      Screenshot of the command line. Choose origin.
    4. If the following window appears, answer/select No.

      Screenshot of the message box. Choose No.
  9. Check the content of your remote Git repository on GitHub.

    1. Go to https://github.com/ and log into your account.

    2. Open your remote Git repository, trainingrepository.

    3. Observe the content of your repository.

      Screenshot of the github web page showing the actual state of the app.
    4. To see the three commits, choose the clock symbol.

      Screenshot of the Github web page showing the history of changes.

Log in to track your progress & complete quizzes