Creating a Commit
Background
In code development, developers usually clone the cloud repository to the local computer to develop code locally, and the commit the code to the cloud repository after completing the phased development task. This section describes how to use the Git client to commit the modified code.
Prerequisites
- You have installed and configured the Git client. For details, see Git Installation and Configuration.
- You have created a repository in CodeHub. For details, see Overview.
- You have set the SSH keys or HTTPS password. For details, see SSH Keys and HTTPS Passwords.
- You have cloned the cloud 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 the cloud repository, and merge it into the master or develop branch. The preceding operations are simulated as follows:
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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>"
- Check the file status again. If no file to be committed exists, the commit is successful, as shown in 5 in the following figure.
- Push a local branch to the remote repository.
git push --set-upstream origin feature1001
Run the preceding command to create a branch that is the same as your local feature1001 branch in the cloud repository, and associate them and synchronize the branch.
origin indicates the alias of your remote repository. 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 the established SSH key pair. If necessary, regenerate a key and configure it on the CodeHub console. For details, see SSH Keys.
- 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.
- View the branch on the cloud.
Log in to CodeHub and go to your repository. In the Files tab page, you can switch to your branch on the cloud.
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.
- Create a merge request. For details, see Merge Request Approval. Notify the reviewer to review the request and merge the new feature into the master or develop branch.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot