Exercise: Creating and Connecting a Remote Git-Repository

Objective

After completing this lesson, you will be able to create and connect a GitHub repository

Create and Connect a Remote Git-Repository

Usage Scenario

In this exercise, you will create a public GitHub1 repository for the source code of your application.

You can use git repositories as a collaboration tool for your codebase. Therefore, your developer team can work together efficiently. 

Note

You have the freedom to choose the git provider of your choice. Any kind of publicly available git provider can be used in this exercise. SAP and also the CAP team is heavily relying on GitHub, so we will showcase the steps required to create and connect a GitHub-Repository. By creating a GitHub account or repository, the GitHub Terms and Policies applies.

Exercise Options

You can perform this exercise in two ways:

  1. Live Environment – using the instructions provided below, you can perform the tasks in the SAP BTP Free Tier account
  2. Platform Simulation – follow the step-by-step instructions within the simulation

Note

We strongly recommend first performing the tasks in the live environment.

Prerequisite

To create a GitHub repository, you need a CAP project and a GitHub account. If you do not have a GitHub account yet, sign up for GitHub first.

Steps

  1. Create a public GitHub repository to store the source code of a project.

    1. Open and sign in to https://github.com/.

    2. To create a new repository, on the Repositories tab, select New.

    3. In the Repository name field, enter RiskManagement. Don't select any of the Initialize this repository with checkboxes.

    4. Choose Create repository.

    5. Copy the HTTPS URL of your newly created GitHub repository.

  2. Create a Personal Access Token for GitHub.

    To create a personal access token, which you can use instead of a password, follow the steps described in Creating a personal access token2.

    Note

    You must create a token in order to connect from the SAP Business Application Studio or any git-cli instance to your remote repository. This is due the fact that, starting from August 13, 2021, GitHub removed support for using a password to authenticate Git operations. This means that you will need to use an SSH key or a Personal Access Token (PAT) to authenticate against your remote provider.
  3. Connect your GitHub repository with your CAP project.

    You have created a new, empty GitHub repository. To be able to use it as a repository for your risk management application source code, you need to connect it with your CAP project. Until now, the only place where your project's source code resided was your personal dev space in SAP Business Application Studio.

    1. Return to your SAP Business Application Studio.

    2. Open a new terminal and navigate to your project root folder.

  4. Initialize the local git repository.

    Because you cloned the starter template from GitHub, you already have a GitHub repository. Therefore, you do not have to create a new local one. All you have to do is commit all our recent changes, link the new GitHub repository with your local one, and push the changes to GitHub.

    1. Add all directories and files to the git staging area.

      Perform the command:

      Code Snippet
      Copy code
      Switch to dark mode
      1
      git add .

    2. Create the first commit in your git repository.

      Perform the command:

      Code Snippet
      Copy code
      Switch to dark mode
      1
      git commit -m "Initial Commit"

    3. Add your copied GitHub repository URL from the previous part of the exercise as remote repository (without the angle brackets < and > ):

      Perform the command:

      Code Snippet
      Copy code
      Switch to dark mode
      1
      git remote set-url origin <your-copied-git-repo-url.git>

      This tells your local git repository in the SAP Business Application Studio dev space that it has a remote counterpart on GitHub. The remote counterpart should act as the origin, thus it is the repository that you and your colleagues use as the central repository of your project.

    4. Push the commit with your project content to this GitHub repository.

      Perform the command:

      Code Snippet
      Copy code
      Switch to dark mode
      1
      git push -u origin main

      This tells your local git to push the main branch to the remote repository. The -u option is used because the branch main did not yet exist on the remote repository.

    5. When prompted, enter your GitHub username and the personal access token that you have created previously.

      Note

      The prompt appears in the upper middle of the SAP Business Application Studio, not in the terminal tab that you have used until now.

Result

You have connected your CAP project with your public GitHub repository using git commands4.

Reference Links: Creating and Connecting a GitHub Repository

For your convenience, this section contains the external references in this lesson.

If links are used multiple times within the text, only the first location is mentioned in the reference table.

Ref#SectionContext text fragmentBrief descriptionLink
1Create and Connect a GitHub Repositorythe steps described in Creating a personal access tokenGitHub tokenhttps://github.com/
2Create a Personal Access Token for GitHubyou will create a public GitHub repositoryProject GitHubhttps://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token
3Connect Your GitHub Repository with Your CAP Projectopen your SAP BTP TrialSAP BTP trialhttps://cockpit.hanatrial.ondemand.com/
4Summarypublic GitHub repository using git commandsGit commandshttps://git-scm.com/docs

Log in to track your progress & complete quizzes