03 Git Fundamentals and Practices
03 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.
Page 2 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Contents
1. Version Control Overview
Page 3 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
2
10/19/2024
1 2
XX thesis_0401.docx XX thesis_0402.docx
3 4
XX thesis_0403.docx XX thesis_0404.docx
• 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
3 4
Page 5 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
3
10/19/2024
1 2 5
3 4 6
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.
Page 7 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
4
10/19/2024
Version 1 File
Page 8 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Version 1 developers cannot collaborate and even data lost may occur;
therefore, security cannot be assured.
Page 9 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
5
10/19/2024
▫ 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.
Master
Clone Push
Local repository of developer A
Master
Master
Feature
Develop features.
Page 11 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
6
10/19/2024
Contents
1. Version Control Overview
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.
▫ Fully distributed
Page 13 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
7
10/19/2024
Remote repository
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.
▫ 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.
Page 15 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
8
10/19/2024
Page 16 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
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.
HEAD
Master
Origin
Master
Page 19 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
10
10/19/2024
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.
• 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.
• 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
Page 24 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
TortoiseGit SourceTree
SmartGit GitEye
End
Page 25 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
13
10/19/2024
Page 26 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Clone (TortoiseGit)
Page 27 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
14
10/19/2024
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.
Pull (TortoiseGit)
1
Page 29 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
15
10/19/2024
git push
Remote
Local PC Deleted
repository
Page 30 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Push (TortoiseGit)
1 Git commit
2 Git push
Page 31 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
16
10/19/2024
Local PC
Page 32 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Merge (TortoiseGit)
• Create a hotfix branch.
2
3
1
Page 33 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
17
10/19/2024
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
Page 35 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
18
10/19/2024
Page 36 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Add the
Install the Git Generate an Create a Create a
Start SSH public
client. SSH key pair. project. repository.
key.
Page 37 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
19
10/19/2024
Page 38 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
• 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
Page 40 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
• 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
Page 42 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
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
Page 44 Copyright © 2020 Huawei Technologies Co., Ltd. All rights reserved.
Quiz
1. (Single) What type of version control system is Git? ( )
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/
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