0% found this document useful (0 votes)
28 views

03 Git Fundamentals and Practices

Uploaded by

fersd2018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

03 Git Fundamentals and Practices

Uploaded by

fersd2018
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

10/19/2024

Git Fundamentals and Practices

Page 0 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Foreword
• Since software is expanding in scale and becoming increasingly complicated, developers
have higher requirements on version control during software development.

• There are various version control tools in the industry, such as ClearCase, Visual SourceSafe
(VSS), Subversion (SVN), and Git. This course describes the basic concepts and operations of
the popular open-source tool Git, and introduces the code hosting practice on HUAWEI
CLOUD.

Page 1 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

1
10/19/2024

Objectives
 Upon completion of this course, you will be able to:
▫ Describe the differences between centralized and distributed version control systems.

▫ Describe Git basic concepts.

▫ Perform basic Git operations.

▫ Use the GUI-based Git client TortoiseGit.

▫ Perform code hosting on HUAWEI CLOUD.

Page 2 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Contents
1. Version Control Overview

2. Git Basic Concepts

3. Basic Git Operations: Clone, Pull, Push, and Merge

4. HUAWEI CLOUD-based Code Hosting

Page 3 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

2
10/19/2024

Begin with Your Thesis


• Multiple copies with different modifications:

1 2

XX thesis_0401.docx XX thesis_0402.docx

3 4

XX thesis_0403.docx XX thesis_0404.docx

Question: How to solve this problem?


Pain point:
• Multiple copies with different modifications are available. To revert certain modifications, you need to open
these files one by one to search for the desired modifications.
Page 4 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Version Control: Add Change Descriptions


• Adding a description for each copy facilitates you to recognize what changes are made in a copy.

• Since multiple copies may be generated in a day, to arrange all these copies in a timeline, specify a version number
for each copy.

1 2

v1_xx Thesis_Title v2_xx Thesis_Abstract


Modified_0401.docx Modified_0402.docx

3 4

v3_xx Thesis_Preface v4_xx Thesis_Reference


Modified_0403.docx Modified_0404.docx

Page 5 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

3
10/19/2024

New Problem: What If Multiple Users Modify a


Single File
• When a thesis is reviewed and modified by several mentors, a new problem arises.

1 2 5

v1_xx Thesis_Title v2_xx Thesis_Abstract v5_xx Thesis_Comment Mentor 1's comments


Modified_0401.docx Modified_0402.docx 1_0405.docx

3 4 6

v3_xx Thesis_Preface v4_xx Thesis_Reference v6_xx Thesis_Comment Mentor 2's comments


Modified_0403.docx Modified_0404.docx 2_0405.docx

Pain point:
• The file name does not present the modifier information. In this Add the modifier information
case, it is difficult to determine the modifier directly through the
v5_xx Thesis_Mentor Zhang_Comment 1_0405.docx
file name. v6_xx Thesis_Mentor Li_Comment 2_0405.docx

Page 6 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Version Control Overview


• Version control is the management of changes to program code, configuration files, documents,
and other files during software development. It records changes of a file or a set of files over time so
that you can recall specific versions later.
• Benefits of version control systems:
▫ Record changes made by different developers each time, including the author and their edits.
▫ Switch between historical versions in projects
▫ Compare differences between versions
▫ Allow developers to revert certain changes
▫ Allow developers to create branches for their own workflows and merge their work together
▫ Enable developers to collaborate on projects

Page 7 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

4
10/19/2024

Types of Version Control Systems (1/2)


Version control systems are Local version control system Centralized version control system
classified into the following
systems based on their
Local PC PC A
operation mode:
Server
• Local version control File
system Version
Check out Version database
database
• Centralized version Version 3
control system File Version 3
Version 2
• Distributed version
Version 2
control system PC B Version 1

Version 1 File

Page 8 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Types of Version Control Systems (2/2)


Distributed version control system • Local version control system:
Drawback: All files are stored on one local PC. Multiple users cannot
Server
collaborate to work on the files.
Version
database • Centralized version control system:
Version 3 Drawback: This system requires network connectivity; therefore,
Version 2 developers are unable to work offline. If the central server fails,

Version 1 developers cannot collaborate and even data lost may occur;
therefore, security cannot be assured.

PC A PC B • Distributed version control system:


This system comes into picture to overcome the preceding
File File
drawbacks. Developers clone the version database to their own PCs.
In this case, each developer owns all versioned files.
Version Version
database database

Page 9 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

5
10/19/2024

DISTR VCS: Basic Concepts


• Basic concepts:
▫ Remote repository: a code repository created on the server. It can be described as the heart of the system,
where all developers collaborate and store their code.

▫ Local repository: a code repository on developers’ local PCs. Developers write code in their local repositories
and then push their changes to the remote repository. In addition, they can retrieve code from the remote
repository to obtain changes made by other developers.

▫ Branch: a copy of code in a repository. Developers can write code in a branch and merge their changes to the
target branch. This eliminates the impact on code in branches other than the one that has been checked out
during feature development.

▫ Clone: This operation copies the remote repository to the local PC.

▫ Push: This operation integrates changes in the local repository to the remote repository.

Page 10 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

DISTR VCS: Fundamentals


Roll back to a
Compare versions to previous version.
V1 identify differences. V2 V3
Remoteorigin
repository

Master

Clone Push
Local repository of developer A

Master

Local repository of developer B Clone

Master

Create a branch. Merge branches.

Feature

Develop features.

Page 11 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

6
10/19/2024

Contents
1. Version Control Overview

2. Git Basic Concepts

3. Basic Git Operations: Clone, Pull, Push, and Merge

4. HUAWEI CLOUD-based Code Hosting

Page 12 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Git Overview
• Git is an open-source distributed software version control system for tracking code changes during
software development.

• It was developed by the open source community led by Linus Torvalds in 2002.

• It was originally developed to support the massive open-source code of the Linux kernel.

• It was designed based on the following principles:


▫ Fast and easy to design

▫ Strong support for non-linear development and thousands of parallel branches

▫ Fully distributed

▫ Able to efficiently handle large projects like the Linux kernel

Page 13 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

7
10/19/2024

Git Application Scenarios


• Collaborative software development

Remote repository

HTTPS/SSH Clone Fetch Pull Push

PC 1 PC 2 PC 3 PC 4
Local Local Local Local
repository repository repository repository

Page 14 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Git Basic Concepts (1/2)


• Before learning Git, you need to understand the following basic concepts:
▫ Remote repository: a repository for storing versioned files on the remote server.

▫ Local repository: a repository for storing versioned files on a local server.

▫ Working directory: developer's working copy. Changes in the working directory can be committed to the local
repository.

▫ Staging area: area between a working directory and a local repository. Before committing changes to a local
repository, add the changes to the staging area first.

▫ Branch: a copy of code in a repository.

Page 15 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

8
10/19/2024

Git Basic Concepts (2/2)


• Checkout: checks out code of a
certain version from the local
repository to the working
directory.

• Fetch: downloads code changes


Local PC
from the remote repository to Pull
Clone
your local repository but does
not check out the changes to the Check out Fetch
working directory. Local Remote
Working
repository repository
directory
• Pull: downloads code changes Staging
from the remote repository to area
Push the changes to the
your local repository and checks remote repository.
out the changes to the working Add modified code to the Commit the changes to
staging area. the local repository.
directory.

Page 16 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Git Local Working Areas


Three areas exist on the Local PC installed with Git:
1. Working directory: a working copy of files checked out
Working Staging Local
from the local repository.
directory area repository
2. Staging area: isolates the working directory from the
git checkout local repository and stores files with changes to be

git init committed in the .git/index file, in most cases.


git add 3. Git repository: local repository on the local PC, which is a
hidden directory named .git.
git commit
Related commands:
Local PC • git init: creates a local repository.
• git add: adds files with changes from the working
directory to the staging area.
• git commit: commits the changes that have been
staged to the local repository.

Page 17 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

9
10/19/2024

Remote Repository
• The remote repository is a Git server (also known as origin).
Technically, a remote repository does not differ from a local
one.
Working Staging Local Remote
directory area repository repository • It saves code permanently and allows developers to
collaborate on projects.
• Differences between remote and local repositories:
git fetch ▫ Connection to the remote repository with the URL
▫ SSH access to the remote repository
git pull/clone • Git-based repository managers:
▫ Outside China: GitHub and GitLab
git push ▫ In China: CodeHub (HUAWEI CLOUD), Gitee, etc.
• Related commands:

Local PC
Remote ▫ git clone: clones the remote repository.
repository
▫ git push: pushes files from a local branch to the remote
repository.
▫ git fetch: copies the information that is in the remote
repository but is not in the local repository.
▫ git pull: copies the information that is in the remote
repository but is not in the local repository, and merge it to
a local branch.
Page 18 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Branching: Creating the Master Branch


• A branch in Git is simply a pointer. The master branch has a master pointer that points to the last
commit you made. Other branches also have their own pointers. The HEAD points to the branch that
you are currently on.

HEAD

Master
Origin
Master

Page 19 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

10
10/19/2024

Branching: Creating a Hotfix Branch


HEAD

Master • A branch in Git is simply a pointer. The master branch has a master
Origin pointer that points to the last commit you made. Other branches also
Master have their own pointers. The HEAD points to the branch that you are
currently on.
git checkout
–b Hotfix • Branch creation: Create branches that are separated from the master
branch (which is the mainstream branch), enabling you to start an
Hotfix
independent development line. Branches are not physically copied.
The files with changes in branches are saved only when they are
Hotfix
committed.
HEAD
• Branch switching: You can switch to different branches in the same
Related commands: repository under the same working directory by modifying the HEAD.
• git checkout -b <name>: creates a branch and switch to it. When you switch branches, Git replaces the content of your working
• git branch <name>: creates a branch. directory with the last committed snapshot of the destination branch.
• git switch <name>: switches to a branch.

Page 20 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Writing Code in the Hotfix Branch


• A branch in Git is simply a pointer. The master branch has a master
Master pointer that points to the last commit you made. Other branches also
origin have their own pointers. The HEAD points to the branch that you are
Master currently on.

• Branch creation: Create branches that are separated from the master
git checkout
–b Hotfix branch (which is the mainstream branch), enabling you to start an
independent development line. Branches are not physically copied.
Hotfix The files with changes in branches are saved only when they are
committed.
Hotfix Hotfix
HEAD HEAD
• Branch switching: You can switch to different branches in the same
repository under the same working directory by modifying the HEAD.
When you switch branches, Git replaces the content of your working
Related commands: directory with the last committed snapshot of the destination branch.
• git checkout -b <name>: creates a branch and switch to it.
• git branch <name>: creates a branch.
• git switch <name>: switches to a branch.

Page 21 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

11
10/19/2024

Merging Branches
HEAD
• A branch in Git is simply a pointer. The master branch has a master
Master git switch Master pointer that points to the last commit you made. Other branches also
origin git merge Hotfix
have their own pointers. The HEAD points to the branch that you are
Master currently on.

git checkout • Branch creation: Create branches that are separated from the master
–b Hotfix branch (which is the mainstream branch), enabling you to start an
independent development line. Branches are not physically copied.
Hotfix
The files with changes in branches are saved only when they are
committed.
Hotfix
HEAD • Branch switching: You can switch to different branches in the same
repository under the same working directory by modifying the HEAD.
Related commands: When you switch branches, Git replaces the content of your working
• git checkout -b <name>: creates a branch and switch to it. directory with the last committed snapshot of the destination branch.
• git branch <name>: creates a branch.
• git switch <name>: switches to a branch. • Branch merging: Merge the updates in one branch to another.
• git merge <name>: merges a branch into the branch you have
checked out.

Page 22 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Deleting the Hotfix Branch


HEAD • A branch in Git is simply a pointer. The master branch has a master
Master git switch Master pointer that points to the last commit you made. Other branches also
Origin git merge Hotfix have their own pointers. The HEAD points to the branch that you are
git branch -d Hotfix
Master currently on.

• Branch creation: Create branches that are separated from the master
git checkout
–b Hotfix branch (which is the mainstream branch), enabling you to start an
independent development line. Branches are not physically copied.
Hotfix The files with changes in branches are saved only when they are
committed.
Hotfix
• Branch switching: You can switch to different branches in the same
Related commands: repository under the same working directory by modifying the HEAD.
• git checkout -b <name>: creates a branch and switch to it. When you switch branches, Git replaces the content of your working
• git branch <name>: creates a branch. directory with the last committed snapshot of the destination branch.
• git switch <name>: switches to a branch.
• git merge <name>: merges a branch into the branch you have • Branch merging: Merge the updates in one branch to another.
checked out.
• git branch -d <name>: deletes a branch. • Branch deletion: Delete the pointer of a branch.

Page 23 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

12
10/19/2024

Contents
1. Version Control Overview

2. Git Basic Concepts

3. Basic Git Operations: Clone, Pull, Push, and Merge

4. HUAWEI CLOUD-based Code Hosting

Page 24 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Introduction to Git Clients


• Git comes with clients based on the command line interface (CLI) and graphical user interface (GUI). The Git software provides a
CLI by default.
• Multiple GUI-based Git clients are available, such as TortoiseGit, SourceTree, SmartGit, and GitEye. TortoiseGit is used here as
an example.

Git installation: Start

Download and install the Git software.

TortoiseGit SourceTree

Download and install TortoiseGit.

(Optional) Download and install the


language package of TortoiseGit.

SmartGit GitEye
End

Page 25 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

13
10/19/2024

Clone Pull Push Merge

Clone (CLI-based Client)


• Cloning means that you copy the remote repository to your local PC,
without the need of creating a local repository in advance (the .git $ git clone [email protected]
4.huaweicloud.com:DevCloud_firstuse00001/Git_firstuse.git
folder on your local PC). huaweicloudGit # Clone the specified remote repository.
• Every version of every file in the remote repository is copied when
you run the git clone command. This operation requires the URL of $ cd huaweicloudGit/ # Enter the folder where the working directory
is located.
the remote repository.
$ll # Display files in the working directory.
• After this command is executed, a folder with the same name as the total 4
remote repository is created in the current directory, a .git folder is -rw-r--r-- 1 xxx 1049089 12 May 7 14:57 firstpush.txt
-rw-r--r-- 1 xxx 1049089 17 May 7 14:57 merge1
generated in the folder to store all downloaded versioned files, and
-rw-r--r-- 1 xxx 1049089 12 May 7 14:57 README.md
the files of the latest version are checked out to the working -rw-r--r-- 1 xxx 1049089 19 May 7 14:57 Text
directory.

Files in the remote repository Cloned files


in theName
local repository update time

Page 26 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Clone Pull Push Merge

Clone (TortoiseGit)

• URL: Remote repository URL

• Directory: Folder where the working directory resides

Page 27 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

14
10/19/2024

Clone Pull Push Merge

Pull (CLI-based Client)


$ git pull # Copy the information that is in the remote repository but is not in
• The information that is in the remote repository but is not in your local repository
your local repository, and merge it to the local branch that you have checked
out.
need to be saved in the local repository first.
$ll # Display the files in the working directory.
total 6
• The git pull command is a combination of the git fetch and git merge -rw-r--r-- 1 xxx 1049089 19 Jun 19 09:30 file1.txt
-rw-r--r-- 1 xxx 1049089 23 Jun 19 09:31 firstpush.txt
commands. -rw-r--r-- 1 xxx 1049089 8 May 15 13:59 merge1
-rw-r--r-- 1 xxx 1049089 8 May 15 13:59 README.md
-rw-r--r-- 1 xxx 1049089 29 Jun 19 09:17 readme_dev.txt
-rw-r--r-- 1 xxx 1049089 19 May 15 17:46 Text

Working Staging Local Remote $git fetch # Fetch down all the information that is in the remote repository that
directory area repository repository is not in your local repository.
$ll # Display the files in the folder where the working directory resides.
total 5
-rw-r--r-- 1 xxx 1049089 23 Jun 19 09:31 firstpush.txt
git merge git fetch -rw-r--r-- 1 xxx 1049089 8 May 15 13:59 merge1
-rw-r--r-- 1 xxx 1049089 8 May 15 13:59 README.md
-rw-r--r-- 1 xxx 1049089 29 Jun 19 09:17 readme_dev.txt
-rw-r--r-- 1 xxx 1049089 19 May 15 17:46 Text
git pull $git merge # Merge the differences to the local branch.
$ll # Display the files in the working directory.
total 6
-rw-r--r-- 1 xxx 1049089 20 Jun 19 09:34 file1.txt
-rw-r--r-- 1 xxx 1049089 23 Jun 19 09:31 firstpush.txt
Remote repository -rw-r--r-- 1 xxx 1049089 8 May 15 13:59 merge1
Local PC -rw-r--r-- 1 xxx 1049089 8 May 15 13:59 README.md
-rw-r--r-- 1 xxx 1049089 29 Jun 19 09:17 readme_dev.txt
-rw-r--r-- 1 xxx 1049089 19 May 15 17:46 Text

Page 28 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Clone Pull Push Merge

Pull (TortoiseGit)
1

git pull git fetch + git merge

Page 29 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

15
10/19/2024

Clone Pull Push Merge

Push (CLI-based Client)


$ll # Display the files in the working directory.
• Before pushing changes to the remote repository, run the git commit –m total 6
-rw-r--r-- 1 xxx 1049089 20 Jun 19 09:34 file1.txt
<message> command to commit the changes to the local repository. -rw-r--r-- 1 xxx 1049089 23 Jun 19 09:31 firstpush.txt
-rw-r--r-- 1 xxx 1049089 8 May 15 13:59 merge1
• Then, run the git push origin master command to push the changes from a -rw-r--r-- 1 xxx 1049089 8 May 15 13:59 README.md
-rw-r--r-- 1 xxx 1049089 29 Jun 19 09:17 readme_dev.txt
local branch to the remote repository. -rw-r--r-- 1 xxx 1049089 19 May 15 17:46 Text
$ git rm file1.txt firstpush.txt # Delete the specified file.
$git commit -m "delete file1.txt and firstpush.txt" # Commit the changes to
Staging Local the local repository.
Working Remote
area repository $git push # Push the changes in the local branch to the remote repository.
directory repository

git add/mv/rm Files in the remote repository


Before git After git
git commit push: push:

git push

Remote
Local PC Deleted
repository

Page 30 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Clone Pull Push Merge

Push (TortoiseGit)

add readme.txt file.

1 Git commit
2 Git push

Page 31 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

16
10/19/2024

Clone Pull Push Merge

Merge (CLI-based Client)


• The git merge command merges a specified branch into the branch you have $ git branch hotfix # Create a hotfix branch.
$ git switch hotfix # Switch to the hotfix branch.
checked out.
$ll # Display the files in the working directory.
• The merge is likely to run into conflicts. When Git encounters a conflict, it will total 2
ask for manual intervention to resolve it and the changes need to be committed -rw-r--r-- 1 xxx 1049089 27 7 May 16:12 forgotten_file
-rw-r--r-- 1 xxx 1049089 78 7 May 16:06 method_rename.txt
again through the git commit command.
$ touch file1.txt # Create a file.
$ git add file1.txt # Commit the file to the staging area.
$ git commit -m "add file1.txt in branch hotfix" # Commit the changes to the local
repository.
Working Staging Local $ git switch master # Switch to the master branch.
directory area repository $ git merge hotfix # Merge the changes in the hotfix branch to the master branch.

$ git log --pretty=oneline --graph # Display the commit history.


git checkout * 34cf9d1a2ab5a5ce7343db8d021387aea8b38f51 (HEAD -> master, hotfix) add file1.txt in
branch hotfix
• ee9d29c2f167671ea1d2cae8e169dda613625db9 method_rename.txt modified
git add/mv/rm
$ll # Display the files in the remote repository.
git commit total 3
branchname -rw-r--r-- 1 xxx 1049089 7 8 May 10:25 file1.txt
git merge -rw-r--r-- 1 xxx 1049089 27 7 May 16:12 forgotten_file
<branchname> -rw-r--r-- 1 xxx 1049089 78 7 May 16:06 method_rename.txt
HEAD

Local PC

Page 32 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Clone Pull Push Merge

Merge (TortoiseGit)
• Create a hotfix branch.

2
3
1

Page 33 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

17
10/19/2024

Clone Pull Push Merge

Merge (TortoiseGit)
• Switch to the hotfix branch and commit the changes in the hotfix • Switch to the master branch and merge the changes in the hotfix
branch to the local repository. branch to the master branch.

Page 34 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Contents
1. Version Control Overview

2. Git Basic Concepts

3. Basic Git Operations: Clone, Pull, Push, and Merge

4. HUAWEI CLOUD-based Code Hosting

Page 35 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

18
10/19/2024

Code Hosting on HUAWEI CLOUD


CodeHub is a Git-based online code hosting service for software developers. It is a
CodeHub •
cloud code repository that provides such functions as security management,
member and permission management, branch protection and merging, online
editing, and statistics. The service aims to address issues such as cross-distance
collaboration, multi-branch concurrent development, code version management,
and security.

Send merge Review and • Major functions:


requests CodeHub
merge changes ▫ Online code operations: You can read, modify, and commit code online
anytime, anywhere.
▫ Permission management: You can add, delete, and modify project members,
and assign permissions to them.
▫ Online branch management: You can create, switch, and merge branches.
▫ Online code hosting: You can clone, update, push, compare codes.
▫ Statistics services: You can view repository operation logs and member
contribution statistics
▫ IP address whitelists: Unauthorized code downloads are blocked to ensure
data security.
Employee A Employee B Manager
City A City B

Page 36 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Code Hosting Procedure


• Code hosting procedure on HUAWEI CLOUD
Register a HUAWEI
CLOUD account.

Set the HTTPS


Perform real-name password.
authentication.

Add the
Install the Git Generate an Create a Create a
Start SSH public
client. SSH key pair. project. repository.
key.

Operations on the local PC Manage code Clone code to


End
on the cloud. the local PC.
Operations on HUAWEI CLOUD

Page 37 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

19
10/19/2024

Generating an SSH Key Pair on the Local PC


• Git can use the SSH protocol to manage versioned code files. $ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
An SSH key pair is used to establish a secure connection Enter file in which to save the key (/c/Users/xxx/.ssh/id_rsa):
between a PC and the code hosting service. Different users Enter passphrase (empty for no passphrase):
Enter same passphrase again:
may use different computers. Before using SSH to connect to Your identification has been saved in /c/Users/xxx/.ssh/id_rsa
a code repository, you need to configure an SSH key pair on Your public key has been saved in /c/Users/xxx/.ssh/id_rsa.pub
The key fingerprint is:
your local PC. SHA256:kZUYzfaGAIOzEWDf7pirF+W6uT3WtqZH3nYUmSuVJuU
[email protected]
The key's randomart image is:
• Command: ssh-keygen –t rsa –C "<key comment>" +---[RSA 3072]----+
| o..oo..=.. |
• If you press Enter without entering the password, the | . .+. .oo= . |
generated private key file id_rsa is stored in plain text on | .+. oo = + |
| ... .o E |
the local PC. | o. S = o |
| .+. . . o |
• If passphrase is set, the generated private key file is saved | oo.+ . o |
| o+o * o . |
in cipher text on the local PC.
| .o=+o*.o . |
+----[SHA256]-----+

Page 38 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Creating a Project on HUAWEI CLOUD


• Since CodeHub depends on ProjectMan, you need to subscribe to ProjectMan before using CodeHub.
Service provisioning needs to be performed by region. Therefore, select a proper region before service
provisioning.

• After a project is created, you can create code repositories under this project.

Page 39 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

20
10/19/2024

Adding the SSH Public Key


• Copy the generated SSH public key, log in to the CodeHub home page, click Add SSH Key, and add the SSH
public key as prompted.
$ cat ~/.ssh/id_rsa.pub
ssh-rsa
AAAAB3NzaC1yc2EAAAADAQABAAABgQC4NjZfIQB468a/W/pqYGFc25IkQvg9i7m6ytMJ3YkbzBEfPtUw+VzXW9gnHi3WC5JEw9GLjFvDK+HhNjJjCj3GCbe6WwcF
qSZA6CnQtcNChpMKqZAiY1UrsGVVxRWrZzvRItHaltax7B/B8fEmsBn1BaMC3n1aRN7YNRkrzQIGAEt+ss+kAYnwOb3+WBEX4/0uoN9S3aVjVrkcIA4wdToOCL2EH
vKHax17yj3319KXCpGzq92jIbp04KcToWqf75bNb6d5fjD2pfWY/K4YcgJ7MCk1hGL6w34ucCxuiCvC54GseWUnfApUYCse0n78WzwUXbFKJQUxwgRg/VNHito4cX
2vSqH7V0SB8nQMMN0eDLzk57WpGSsf4mUOxCa6EhDaOh03l0s14S0vMQokKGgVtfpHfHdquJ53OiOc8k7V81vNpEbE7ufmWPIkbD6HtwbZ2NBvSh4rabY0QpJ
9q/rdcCp9X12ajmG7POBo1H/5PuEamPRA7WR6eoP2aaqft78=

Page 40 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Setting HTTPS Password on HUAWEI CLOUD


• HTTPS is another protocol used by Git to manage versioned code. An HTTPS password is a credential for local hosts to
interact with the code hosting server using the HTTPS protocol.

• Log in to the CodeHub home page, click Set new password, and set the HTTPS password as prompted.

• No password is required when you download codes from public code repositories using HTTPS.

Page 41 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

21
10/19/2024

Creating a Code Repository on HUAWEI CLOUD


• A code repository is an object of code hosting and is the basis for collaborative software development.

Project homepage Service Code hosting

• Files in the repository on the cloud:

Page 42 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Cloning Code to the Local PC


• Click Clone with ssh/download on the code repository page, copy the SSH or HTTPS address of the
code repository, and run the git clone command on the local Git client to clone code from the code
repository on the cloud to the local PC.
SSH
$ git clone [email protected]:DevCloud_firstuse00001/Git_firstuse.git

HTTPS
$ git clone https://codehub.devcloud.cn-north-4.huaweicloud.com/DevCloud_firstuse00001/Git_firstuse.git

• Modify codes on the local PC and commit changes to the code repository on the cloud.

Page 43 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

22
10/19/2024

Managing Code on HUAWEI CLOUD


• On HUAWEI CLOUD, you can manage branches, including creating, switching, and merging branches. You can
also read, modify, and commit code online. Experience more functions in code repositories on HUAWEI
CLOUD.

Create and delete Merge branches.


branches. Edit a file online by clicking it.

Page 44 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

Quiz
1. (Single) What type of version control system is Git? ( )

A. Local version control system

B. Centralized version control system

C. Distributed version control system

D. Open version control system

Page 45 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

23
10/19/2024

Summary
• Version control systems allow developers to manage versioned code effectively in software
development.

• Version control systems are classified into local version control systems, centralized version
control systems, and distributed version control systems. Git is a distributed version control
system.

• Git has CLI- and GUI-based clients available. Basic Git operations include clone, pull, push, and
merge.

• HUAWEI CLOUD provides a Git-based online code hosting service for software developers to
resolve issues such as cross-region collaboration, multi-branch concurrent development, code
version management, and code security.

Page 46 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

More Information
• Git: https://git-scm.com/

• TortoiseGit: https://tortoisegit.org/

• CodeHub operation guide: https://support.huaweicloud.com/codehub/index.html

Page 47 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

24
10/19/2024

Thank
谢 谢You
www.huawei.com

Page 48 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.

25

You might also like