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

Lab3 - Basic Git Operations

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)
22 views

Lab3 - Basic Git Operations

Uploaded by

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

HCIP-Datacom-Network Automation Developer Lab Guide

1 Basic Git Operations

1.1 Introduction
In this experiment, you will perform basic Git operations using the CLI and GUI
(TortoiseGit in this experiment).

On completion of this experiment, you will be able to:

● Create a local repository.


● Commit changes to the local repository.
● Create branches, merge branches, and resolve merge conflicts.
● Check version change records.

1.1.1 Procedure
The experiment procedure is as follows:

1. Prepare the environment: Install Git and TortoiseGit on your local PC.

2. Perform basic Git operations using the CLI.

3. Perform basic Git operations using TortoiseGit.

1.2 Environment Preparations


1.2.1 Installing and Configuring Git
1. Install Git.
Visit https://git-scm.com/download/win to download the 32-bit or 64-bit Git
installation package based on the operating system of your local PC.
Double-click the downloaded installation package. In the displayed
installation window, click Next and finally Install.

2020-09-01 Huawei confidential. Page 8 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

2. Configure Git.
Click the start icon of Windows, enter Git Bash in the search box, and press
Enter to open Git Bash. It is recommended that you fix it to the Windows
taskbar.
The first thing you need to do is to set your user name and email address.
Enter the following commands on Git Bash:
git config --global user.name "<Your user name>"
git config --global user.email "<Your email address>"

● Enter an email address in the standard format.


● If you specify --global in the preceding commands, Git will always use the user name
for anything you do in repositories on the local PC. If you want to configure different
names or email addresses for specific repositories, run the commands without specifying
--global.

After the configuration is complete, you can run the following command to view
the configuration:

git config -l

Generate an SSH key pair for authentication with the server where the remote
repository resides. Run the following command on Git Bash:

ssh-keygen -t rsa -C "<Your email address>"

Press Enter for three times. By default, the generated private key and public key
are saved in ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub, respectively.

$ ssh-keygen -t rsa -C "[email protected]" # Generate an SSH key pair.

2020-09-01 Huawei confidential. Page 9 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

Generating public/private rsa key pair.


Enter file in which to save the key (/c/Users/wjj1/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/wjj1/.ssh/id_rsa
Your public key has been saved in /c/Users/wjj1/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:XsRZZsryXcV+9ruJo8QUsYtexviMF9rPFjgVdpI6iSM [email protected]
The key's randomart image is:
+---[RSA 3072]----+
| .+ ...|
| o *o= o.|
| . Bo+ =. |
| E *+=oo +|
| So+B= .o|
| ...@o.. .|
| .+ B. . .|
| o oo...|
| .o+.o.|
+----[SHA256]-----+

So far, Git is installed and configured successfully.

1.2.2 Installing TortoiseGit


If you are not familiar with common Git commands or use a similar Subversion
(SVN) client (such as TortoiseSVN), the TortoiseGit client, a GUI-based Git client,
is your better choice. You need to install Git before running it. For details about
how to install Git, see section 1.2.1.

1. Install TortoiseGit and start it for the first time.


a. Visit the TortoiseGit official website at https://tortoisegit.org/download to
download a 32-bit or 64-bit TortoiseGit installation package based on the
operating system of your local PC.
b. Double-click the installation package. In the window that is displayed, click
Next and finally Install. After the installation is complete, click Finish to run
TortoiseGit for the first time.
c. In the displayed first-startup wizard, select a language, configure the Git
executable path (automatically filled with an available Git path), configure
the user name and email address, and click Next.

2020-09-01 Huawei confidential. Page 10 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

2. Configure TortoiseGit.
TortoiseGit also requires a key pair to authenticate with the code hosting
server. The SSH key pair has been generated on Git Bash. Set SSH client on
the Network page of TortoiseGit to ssh.exe in the Git installation directory.
Then, the SSH key pair generated on Git Bash can be used.

1.3 Git Operations on the CLI


This section describes basic Git operations on the CLI, including creating a local Git
repository, committing changes to the local Git repository, checking version
change records, creating branches, merging branches, and resolving merge
conflicts. Interactions between a local repository and a remote repository are
explained in 2 Code Hosting Practice on HUAWEI CLOUD based on CodeHub.

1.3.1 Creating a Local Repository


1. Create an empty folder on your local PC.
$mkdir GitLearning # Create a folder named GitLearning.
$cd GitLearning/ # Enter the GitLearning folder.

2. Run the git init command to turn the folder into a local Git repository.
$git init # Create a local Git repository.
Initialized empty Git repository in D:/workspace/GitLearning/.git/

2020-09-01 Huawei confidential. Page 11 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

After this command is executed, an empty local Git repository is created. The .git
folder is generated in the GitLearning folder, and is used by Git to track and
manage the version library.

1.3.2 Committing Changes to the Local Git Repository


After the local Git repository is created, create a readme.txt file and add it to the
repository. Run the following command to create the readme.txt file:

$ vim readme.txt # Create a readme.txt file.

Enter the following content in the readme.txt file.

This is a Git repository.

Run the git status command to view the status of each file in the local Git
repository. The command output displays the files that are modified, deleted, and
added.

$git status # Check the status of files in the local Git repository.
On branch master

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)
readme.txt

nothing added to commit but untracked files present (use "git add" to track)

Untracked files are the files that are not traced by Git. Run the git add command
to add the readme.txt file to the staging area.

$ git add readme.txt # Add the readme.txt file to the staging area.
warning: LF will be replaced by CRLF in readme.txt.
The file will have its original line endings in your working directory

Run the git status command again to check the status of files in the local Git
repository.

$git status # Check the status of files in the local Git repository.
On branch master

No commits yet

Changes to be committed:

2020-09-01 Huawei confidential. Page 12 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

(use "git rm --cached <file>..." to unstage)


new file: readme.txt

Changes to be committed indicates that changes are not committed to the local
Git repository. The preceding command output shows that the readme.txt file is
staged but not yet committed to the local Git repository. Run the git commit
command to commit the readme.txt file to the local Git repository.

$ git commit -m "add readme.txt file" # Commit the readme.txt file to the
local Git repository.
[master (root-commit) 28b687a] add readme.txt file
1 file changed, 1 insertion(+)
create mode 100644 readme.txt

The -m option indicates your commit message. Based on the commit message,
you can easily find the desired commit record from history records.

1.3.3 Displaying Historical Commit Records


Now, you have created your first commit (the readme.txt file). It is hard to
remember what was changed with each commit after you have created several
commits. In this case, you can run the git log command to view historical commit
records. To view the commit records of a specified file, specify the file name in the
git log command.

$ git log # Display historical commit records.


commit 28b687ac29cc4940710b3bb112b371c6ee46a260 (HEAD -> master)
Author: wjj1 <[email protected]>
Date: Mon May 18 16:35:43 2020 +0800

add readme.txt file

A variety of options are available to the git log command. For example, the --
pretty==oneline option displays each commit on a single line.

$ git log --pretty=oneline # Display each commit on a single line.


28b687ac29cc4940710b3bb112b371c6ee46a260 (HEAD -> master) add readme.txt file

For more information about the git log command, visit https://git-
scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History.

1.3.4 Creating a Branch


Branching is an important feature of Git. With this feature, you can diverge from
the main line of development and continue to do work without messing with that
main line. You can run the git branch command to create a branch. The following
example creates a branch named feature and add functions in this branch.

$ git branch feature # Create a branch named feature.

Switch to the feature branch.

$ git switch feature # Switch to the feature branch.


Switched to branch 'feature'

2020-09-01 Huawei confidential. Page 13 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

The branch you are working on is displayed on the right of the CLI. The
information in the red box in the following figure indicates that you are working
on the feature branch.

Add the helloworld.md file to the feature branch. This file represents a new
function.

$ echo "hello world !" > helloworld.md # Create the helloworld.md file.
$ git add helloworld.md # Add the helloworld.md file to
the staging area.
warning: LF will be replaced by CRLF in helloworld.md.
The file will have its original line endings in your working directory
$ git commit -m "add helloworld.md file" # Commit the helloworld.md
file to the local Git repository.
[feature 9ce7bc3] add helloworld.md file
1 file changed, 1 insertion(+)
create mode 100644 helloworld.md

Check files in the GitLearning folder. You can see that the helloworld.md file is
added to the feature branch.

$ ll # Display files in the GitLearning folder.


total 2
-rw-r--r-- 1 wWX935519 1049089 14 May 18 17:06 helloworld.md
-rw-r--r-- 1 wWX935519 1049089 26 May 18 15:02 readme.txt

1.3.5 Merging Branches


After functions are developed in the feature branch, merge the feature branch to
the master branch.

1. Switch to the master branch.


$ git switch master # Switch to the master branch.
Switched to branch 'master'
$ ll # Check files in the master branch. Only the readme.txt
file exists.
total 1
-rw-r--r-- 1 wWX935519 1049089 26 May 18 15:02 readme.txt

2. Merge the feature branch to the master branch.


$ git merge feature
Updating 28b687a..9ce7bc3
Fast-forward
helloworld.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 helloworld.md

The output of the git merge command indicates that the helloworld.md file is
added to the master branch. This file is the one created in the feature branch.

2020-09-01 Huawei confidential. Page 14 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

Run the following command to check files in the master branch. The command
output shows that the helloworld.md file is added to the master branch.

$ ll # Check files in the master branch.


total 2
-rw-r--r-- 1 wWX935519 1049089 15 May 18 17:19 helloworld.md
-rw-r--r-- 1 wWX935519 1049089 26 May 18 15:02 readme.txt

1.3.6 Resolving Merge Conflicts


Conflicts may occur when branches are merged. If the same file is modified in two
branches, Git may not let you merge the branches due to a conflict. In this case,
you need to resolve the conflict. The following uses the feature branch and master
branch as an example.

1. Switch to the feature branch, modify the helloworld.md file, and commit it
to the local Git repository.
$ git switch feature
$ echo "This is feature branch." >> helloworld.md # Write content in the
helloworld.md file.
$ git add helloworld.md
$ git commit -m "Modify helloworld.md in feature branch."

2. Switch to the master branch, modify the helloworld.md file, and commit it to
the local Git repository.
$ git switch master
$ echo "This is master branch." >> helloworld.md
$ git add helloworld.md
$ git commit -m "Modify helloworld.md in master branch."

3. Merge the feature branch to the master branch.


$ git merge feature
Auto-merging helloworld.md
CONFLICT (content): Merge conflict in helloworld.md
Automatic merge failed; fix conflicts and then commit the result.

The CONFLICT field indicates that a conflict occurs during branch merging. In this
case, the MERGING field is displayed at the end of the command.

4. Resolve the conflict.

Run the git status command to view the files that conflict with each other.

$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)

2020-09-01 Huawei confidential. Page 15 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: helloworld.md

no changes added to commit (use "git add" and/or "git commit -a")

The preceding command output indicates that the helloworld.md file is modified
in both branches. You need to manually modify the file to resolve the conflict. Run
the following command to open the conflicting file.

$ vim helloworld.md # Open the helloworld.md file.


hello world !
<<<<<<< HEAD
This is master branch.
=======
This is feature branch.
>>>>>>> feature

The content between <<<<<<<<HEAD and ======== are the modification of the
helloworld.md file in the master branch, and the content between ========
and >>>>>>>Feature are the modification of the helloworld.md file in the
feature branch. In order to resolve the conflict, you must modify the content as
expected. In this example, resolve this conflict by replacing the file content with
the following:

hello world !
This is master branch.

Enter :wq and press Enter to save the configuration and exit.

After the file content is modified, run the git add command to add the modified
helloworld.md file to the staging area. Then the file conflict is resolved.

$ git add helloworld.md

Run the git commit command. In the displayed window, enter :q to merge the
feature and master branches.

$ git commit
[master e0cafd3] Merge branch 'feature'

You can run the git log command with the -graph option to display a graph that
clearly shows the branch merging process.

$ git log --pretty=oneline --graph


* 5719cfe6dcc9ea7588a980ab2bcbb95af45ef7e5 (HEAD -> master) Merge branch 'feature'
|\
| * c7687eea3e5730e47062e32abe17e3c84e928acc (feature) Modify helloworld.md in feature
branch.
* | 85d92eb800f2d0f8ada4f279142ef0e5120f2148 Modify helloworld.md in master branch.

2020-09-01 Huawei confidential. Page 16 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

1.4 Git Operations on the GUI


This section describes basic Git operations on TortoiseGit.

1.4.1 Creating a Local Git Repository


1. Create a folder named GitLearning and create a .git folder in the
GitLearning folder. The .git folder is the folder where the local Git repository
resides, and the GitLearning folder is the local workspace. Git will check out
files to the GitLearning folder for developers to work on them.

2. Right-click in the blank area of the GitLearning folder, choose Git Create
repository here... from the shortcut menu, and click OK.

After the local Git repository is created, a green round icon is displayed in the
lower left corner of the GitLearning folder. TortoiseGit uses this icon to
identify the file status, for example, whether the file is modified or conflicted.
Compared with the Git CLI, you can directly view the file status on the
TortoiseGit GUI, without running the git status command.

The created .git folder is hidden and is displayed in semi-transparent mode. In


addition, some files are generated in the .git folder. In this case, the .git
folder is initialized as a Git repository.

2020-09-01 Huawei confidential. Page 17 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

1.4.2 Committing Changes to the Local Git Repository


In the GitLearning folder, use Notepad to create a readme.txt file and enter the
following content:

This is a Git repository.

You can easily figure out that the readme.txt file is not managed by Git because
the file icon does not have any status identifier, as shown in the preceding figure.
However, on the Git CLI, you need to run the git status to check the file status to
check whether a file has been managed by Git.

Right-click in the blank area of the GitLearning folder and choose Git Commit
from the shortcut menu.

In the dialog box that is displayed, select the check boxes as shown in the
following figure, enter a commit message in the Message text box, and click
Commit.

2020-09-01 Huawei confidential. Page 18 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

After the commit operation, the readme.txt file is committed to the local Git
repository and a green icon is displayed on the folder icon, indicating that the file
is the latest version in the Git repository. The commit function of TortoiseGit is
equivalent to running the git add and git commit commands in the Git CLI.

1.4.3 Displaying Historical Commit Records


Now, you have created your first commit (the readme.txt file). It is hard to
remember what was changed with each commit after you have created several
commits. You can use TortoiseGit to view historical commit records.

2020-09-01 Huawei confidential. Page 19 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

Right-click in the blank area of the folder where the working directory resides and
choose TortoiseGit > Show log from the shortcut menu. Information about
historical commit records is available in the displayed window, including:

1. A summary of historical commit records, including the commit time, author,


and commit message
2. Detailed information about a commit record
3. Files modified in a commit

2020-09-01 Huawei confidential. Page 20 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

You can also filter historical commit records by the author, commit message,
SHA1, and other conditions.

1.4.4 Creating a Branch


Create a branch named feature using TortoiseGit and develop functions in this
branch.

2020-09-01 Huawei confidential. Page 21 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

In the dialog box that is displayed, set the branch name to feature and select
Switch to new branch. After the branch is created, the working directory is
directly switched to the feature branch.

2020-09-01 Huawei confidential. Page 22 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

In the shortcut menu, Git Commit is followed by feature, indicating that you are
working on the feature branch.

Create the helloworld.md file in the feature branch and commit the change to
the local Git repository. This file represents a new function. All the involved
operations are the same as those described in the previous section. After
operations are complete, the structure of the GitLearning folder is as follows.

2020-09-01 Huawei confidential. Page 23 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

1.4.5 Merging Branches


After functions are developed in the feature branch, merge the feature branch to
the master branch.

1. Switch to the master branch on TortoiseGit.

2020-09-01 Huawei confidential. Page 24 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

The workspace is switched to the master branch. Only the readme.txt file
exists.

2. Merge the feature branch to the master branch.

In the dialog box that is displayed, select the feature branch to be merged to
the master branch and click OK.

2020-09-01 Huawei confidential. Page 25 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

The helloworld.md file is added to the workspace of the master branch,


which indicates that the feature branch is merged to the master branch
successfully.

1.4.6 Resolving Merge Conflicts


The following uses the feature branch and master branch as an example to
describe how to use TortoiseGit to resolve a merge conflict.

1. Switch to the feature branch, modify the helloworld.md file, and commit it
to the local Git repository. The file is modified as follows.

2. Switch to the master branch, modify the helloworld.md file, and commit it to
the local Git repository. The file is modified as follows.

2020-09-01 Huawei confidential. Page 26 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

3. Merge the feature branch to the master branch.


An error message indicating that a merge conflict occurs is displayed.

4. Resolve the merge conflict.


Click Resolve. The following dialog box is displayed, with the conflicting file
listed.

2020-09-01 Huawei confidential. Page 27 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

Double-click the file. The following window is displayed. The content in line 1
is the file content in the feature branch, and that in line 2 is the file content in
the current branch, that is, the master branch. The content in line 3 is the
merged content. Since a merge conflict occurs, you need to manually modify
the merged file content. After modifying the merged content in line 3, click
Mark as resolved on the toolbar and close the window.

Right-click in the blank area of the folder and choose Git Commit from the
shortcut menu. In the displayed window, click Commit. The conflict is
resolved and the feature branch is successfully merged to the master branch.

2020-09-01 Huawei confidential. Page 28 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

According to the commit record, the feature branch is merged to the master
branch successfully.

2020-09-01 Huawei confidential. Page 29 of 64


HCIP-Datacom-Network Automation Developer Lab Guide

2020-09-01 Huawei confidential. Page 30 of 64

You might also like