Lab3 - Basic Git Operations
Lab3 - Basic Git Operations
1.1 Introduction
In this experiment, you will perform basic Git operations using the CLI and GUI
(TortoiseGit in this experiment).
1.1.1 Procedure
The experiment procedure is as follows:
1. Prepare the environment: Install Git and TortoiseGit on your local PC.
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>"
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:
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.
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.
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/
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.
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:
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.
A variety of options are available to the git log command. For example, the --
pretty==oneline option displays each commit on a single line.
For more information about the git log command, visit https://git-
scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History.
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.
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.
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.
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."
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.
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)
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.
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.
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.
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.
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.
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.
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:
You can also filter historical commit records by the author, commit message,
SHA1, and other conditions.
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.
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.
The workspace is switched to the master branch. Only the readme.txt file
exists.
In the dialog box that is displayed, select the feature branch to be merged to
the master branch and click OK.
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.
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.
According to the commit record, the feature branch is merged to the master
branch successfully.