Updated on 2023-07-25 GMT+08:00

Creating a Commit

Background

In code development, developers usually clone code from CodeArts Repo to the local PC to develop code locally, and the commit the code to CodeArts Repo after completing the phased development task. This section describes how to use the Git client to commit the modified code.

Prerequisites

  1. Git Installation and Configuration.
  2. You have created a repository in CodeArts Repo. For details, see Overview.
  3. You have set the SSH keys or HTTPS password. For details, see Setting SSH Key or HTTPS Password for CodeArts Repo Repository
  4. You have Cloned the CodeArts Repo Repository to the Local Host. For details, see Overview.

Procedure

Generally, developers do not directly develop code in the master branch. Instead, they create a feature branch based on the master or develop branch, and develop code in it. Then they commit the feature branch to CodeArts Repo, and merge it into the master or develop branch. The preceding operations are simulated as follows:

  1. Go to the local repository directory and open the Git client. Take Git Bash as an example. The principles and commands of other Git management tools are the same.
  2. Create a feature1001 branch based on the master branch, switch to the created branch, and run the following command in the master branch:

    git checkout -b feature1001 #Shown in 1 in the following figure.

    This command creates a branch and then switches to the branch.

    If the command is successfully executed, 2 in the following figure is shown. You can run the ls command to view the files of the branch (as shown in 3 in the following figure), which are the same as those of the master branch currently.

  3. Modify code in the feature branch (code development).

    Git supports Linux commands. In this case, the touch command is used to create a file named newFeature1001.html, indicating that the developer has developed new features locally and a new file is added into the local code repository.

    touch newFeature1001.html

    Run the ls command again to view the created file.

  4. Run the add and commit commands to add the file from the working directory to the staging area, and then commit the file to the local repository. (For details, see Overview.)

    You can also run the status command to check the file status.

    1. Run the status command. The command output shows that a file in the working directory is not included in version management, as shown in 1 in the following figure.
    2. Run the add command to add the file to the staging area, as shown in 2 in the following figure.
      git add . # Period (.) means all files, including hidden files. You can also specify a file.
    3. Run the status command. The command output shows that the file has been added to the staging area and is waiting to be committed, as shown in 3 in the following figure.
    4. Run the commit command to commit the file to the local repository, as shown in 4 in the following figure.
      git commit -m "<your_commit_message>"
    5. Check the file status again. If no file to be committed exists, the commit is successful, as shown in 5 in the following figure.

  5. Push a local branch to CodeArts Repo.

    git push --set-upstream origin feature1001 

    Run the preceding command to create a branch that is the same as your local feature1001 branch in CodeArts Repo, and associate them and synchronize the branch.

    origin indicates the alias of your CodeArts Repo. The default alias of a directly controllable repository is origin. You can also use the repository address.

    If the push fails, check the connectivity.

    • Check whether your network can access CodeArts Repo.
      Run the following command on the Git client to test the network connectivity:
      1
      ssh -vT git@********.com
      

      If the command output contains connect to host ********.com port 22: Connection timed out, your network is restricted from accessing CodeArts Repo. Contact your network administrator.

    • Check the SSH key. If necessary, regenerate a key and configure it on the CodeArts Repo console. For details, see SSH Keys. Alternatively, check whether the HTTPS password is correctly configured.
    • Check the IP address whitelist. If no whitelist is configured, all IP addresses are allowed to access the repository. If a whitelist is configured, only IP addresses in the whitelist are allowed to access the repository.

  6. View the CodeArts Repo repository branch.

    Log in to CodeArts Repo and go to your repository. In the Files tab page, you can switch to your branch in CodeArts Repo.

    If the branch you just committed is not displayed, your origin may be bound to another repository. Use the repository address to commit the branch again.

  7. Create a merge request. For details, see Managing MRs. Notify the approver to review the request and merge the new feature into the master or develop branch.