Use Git for Version Control
Here, you will learn how to get started working with Git, one of the main tools when developing code. You will learn how to clone a repository, manage the local repository, and push the changes back to the remote. To understand how GUI-based Git software such as Git extension in Visual Studio Code works, you will be managing the repository using the command-line commands.
You are using a US English keyboard layout. This cannot be changed once the lab has initialized.
Visit Device Help for info about changing the OS keyboard layout and screen resolution after lab initialization.
You may navigate away from this page once you begin initializing the lab.
You will be notified once the devices are ready.
Clone Git Repository
In this procedure, you will access the GitLab site available in the lab environment, log in to the site, and obtain the link for cloning an existing repository to your local working environment.
Step 3
From the taskbar, open the terminal, or open Visual Studio Code and use its terminal.
Step 4
Move to your working directory, /home/student/working_directory.
Answer
student@student-workstation:~$ cd /home/student/working_directory
student@student-workstation:~/working_directory$
Step 5
Configure the Git username and email. Git will use these credentials when you publish changes to the remote repository.
Answer
student@student-workstation:~/working_directory$ git config --global user.name "student" student@student-workstation:~/working_directory$ git config --global user.email student@dev.local student@student-workstation:~/working_directory$
Step 6
On the GitLab Projects site click on devnet-lab02 project to get to the project for devnet-lab02. Copy the link for cloning the repository by clicking Clone in the top right and then clicking the copy icon next to the HTTP URL. The URL will be copied in your clipboard. Dismiss the banners on top of the page.
Step 7
In the terminal, using the copied URL, clone the repository from GitLab into the current directory (using the "." as the current directory) so that you will be able to work with files in the repository. Enter the username student and the password 1234QWer, the same credentials used when you logged in to the GitLab site.
Answer
student@student-workstation:~/working_directory$ git clone http://dev.gitlab.local/student/devnet-lab02.git . Cloning into '.'... Username for 'http://dev.gitlab.local': student Password for 'http://student@dev.gitlab.local': 1234QWer remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done.
Step 8
List the contents of the cloned repository. You can see the same README.md file as on the remote repository now downloaded locally. There is also the .git file where everything that Git needs for keeping track of your project will be stored.
Answer
student@student-workstation:~/working_directory$ ls -la
total 16
drwxr-xr-x 3 student student 4096 Sep 23 05:56 .
drwxr-xr-x 29 student student 4096 Sep 23 05:17 ..
drwxrwxr-x 8 student student 4096 Sep 23 05:56 .git
-rw-rw-r-- 1 student student 36 Sep 23 05:56 README.md
Commit and Push Changes
In this procedure, you will learn how to verify the status of the local repository, how to add files, commit them, and finally push them to the remote repository—all from the terminal and using the Git extension in Visual Studio Code.
Step 9
In the working directory, create a file called lab02.py using touch.
Answer
student@student-workstation:~/working_directory$ touch lab02.py
Step 10
Run the git status
command to check the current state of the repository.
Answer
student@student-workstation:~/working_directory$ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
lab02.py
nothing added to commit but untracked files present (use "git add" to track)
student@student-workstation:~/working_directory$
Step 11
To add the lab02.py file to the repository, you first need to start tracking it. Run the git add lab02.py
command.
Answer
student@student-workstation:~/working_directory$ git add lab02.py
Step 12
Run the git status
command. The command reveals that the file is now staged and will be included in the next commit.
Answer
student@student-workstation:~/working_directory$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: lab02.py
student@student-workstation:~/working_directory$
Step 13
Commit the file to your local repository. The commit message has to describe what is included in this commit. Run the git status
command afterward, and you will see that the local copy of the master branch is ahead of origin/master on the GitLab server for one commit. This means that the lab02.py file is currently part of your local repository but not yet on the GitLab remote server.
Answer
student@student-workstation:~/working_directory$ git commit -m "Add lab02.py file" [master 126171c] Add lab02.py file 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 lab02.py student@student-vm:~/working_directory$ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working tree clean student@student-vm:~/working_directory$
Step 14
To publish the commit, use the git push origin master
command. Your local master branch will now be synchronized with the remote branch. Use the git status
command to confirm that you are back to a clean working directory.
Answer
student@student-workstation:~/working_directory$ git push origin master Username for 'http://dev.gitlab.local': student Password for 'http://student@dev.gitlab.local': 1234QWer Counting objects: 3, done. Delta compression using up to 4 threads. Compressingobjects: 100% (2/2), done. Writing objects: 100% (3/3), 271 bytes | 271.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To http://dev.gitlab.local/student/devnet-lab02.git 1c0ddd7..126171c master -> master student@student-workstation:~/working_directory$ git status On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
Step 17
From the desktop, open Visual Studio Code, and then Click Open folder... in the Start section on the Welcome page. Click Home and open working_directory. In the top-right corner, click OK.
Step 18
Open the lab02.py file, add a print statement, and save the file.
Answer
print('Working with Git')
Compare Branches
In this procedure, you will learn how to create a branch, move between different branches, and how to compare them, finding the differences in the files.
Step 26
In the terminal, create a new branch on your local repository called staging and switch to it.
Answer
student@student-workstation:~/working_directory$ git checkout -b staging
Switched to a new branch 'staging'
Step 27
Back in Visual Studio Code add another print statement to lab02.py and save the file.
Answer
print('New branch')
Step 28
Compare the two branches using the git diff master
command in the terminal. The current branch (staging) has two additional lines compared with the master branch.
Answer
student@student-workstation:~/working_directory$ git diff master
diff --git a/lab02.py b/lab02.py
index a82951a..1904e55 100644
--- a/lab02.py
+++ b/lab02.py
@@ -1 +1,3 @@
-print('Working with Git')
\ No newline at end of file
+print('Working with Git')
+
+print('New branch')
\ No newline at end of file
Step 29
Add the file to start tracking it, and commit the changes.
Answer
student@student-workstation:~/working_directory$ git add lab02.py student@student-workstation:~/working_directory$ git commit -m "Add print statement" [staging 8a5f68a] Add print statement 1 file changed, 3 insertions(+), 1 deletion(-)
Merge Branch
Merging branches is a common task when working with Git. You will list the existing branches, check on which you are currently working on, and merge to the master branch without conflicts.
Step 30
To check the available branches, enter the git branch
command in the terminal. The star will indicate the current branch.
Answer
student@student-workstation:~/working_directory$ git branch
master
* staging
Step 31
Before merging the staging branch into the master branch, first switch to the master branch using the git checkout master
command.
Answer
student@student-workstation:~/working_directory$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
student@student-workstation:~/working_directory$
Step 32
To merge the staging branch into the master branch, use the git merge staging
command.
Answer
student@student-workstation:~/working_directory$ git merge staging
Updating 68391bd..8a5f68a
Fast-forward
lab02.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
Step 33
Verify the status of the local repository and push the changes to the remote branch. Enter the username student and password 1234QWer.
Answer
student@student-workstation:~/working_directory$ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working tree clean student@student-workstation:~/working_directory$ git push origin master Username for 'http://dev.gitlab.local': student Password for 'http://student@dev.gitlab.local': Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 316 bytes | 316.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To http://dev.gitlab.local/student/devnet-lab02.git 68391bd..8a5f68a master -> master
No comments:
Post a Comment