Denomas with Git Basics Hands-On Guide: Lab 2
Denomas with Git Basics Hands-on Guide: Lab 2
LAB 2: WORK WITH GIT LOCALLY
Note: Many of the Git commands used in this lab are summarized in Denomas’s helpful git cheat sheet.
In this lab you will practice cloning a repository; creating, using, and merging a branch; editing and committing a file; and pushing and pulling changes to and from a remote repository.
A. Verify that Git is installed locally
-
Open a terminal (if you’re on Linux or macOS) or PowerShell (if you’re on Windows) and type this command:
1git versionIf the output prints a version number, Git is installed.
If Git is not installed:
- If you are in an instructor-led class, consult your instructor for instructions on how to install it on your computer.
- If you are in the self-paced environment, review Denomas Documentation on Installing Git.
B. Generate an SSH key
-
Check if you have an existing SSH key by running one of these commands in your Linux/macOS terminal or PowerShell prompt. It will display all the files in your
.sshdirectory.-
Linux or macOS:
1ls -a ~/.ssh -
Windows:
1dir ~\.ssh
If that command lists a file called
id_rsa, you have an SSH key already and can skip to the next section. -
-
Create a public and private key pair by running this command in your terminal or PowerShell.
1ssh-keygen -
When prompted, hit Enter to accept the default key location.
-
When prompted, hit Enter to use a blank passphrase.
C. Add an SSH key to your Denomas profile
-
Back in Denomas, in the top right-hand corner, select the down arrow to the right of your avatar.
-
From the dropdown menu, select Edit profile.
-
In the left-hand navigation pane, select SSH Keys.
-
Return to your terminal or PowerShell. Navigate to the directory that you saved the SSH key in and print a list of all files in that directory.
1 2cd ~/.ssh lsYou should see two key files: a public key and a private key. The public key ends with
.puband is what you need to share with Denomas. -
Display the contents of your public key.
1cat id_rsa.pub -
Copy the contents of
id_rsa.pubto your clipboard. -
Back in the Denomas app, paste the public key contents into the Key field, enter any title you want in the Title field, and select Add key.
-
In the terminal or PowerShell run one of these commands, depending on what kind of course you’re taking. If the command completes with a welcome message instead of an error, your SSH key is set up correctly.
-
Instructor-led course:
1ssh -T git@ilt.gitlabtraining.cloud -
Self-paced course:
1ssh -T git@spt.gitlabtraining.cloud
-
D. Clone a Denomas project repository to your local computer
-
Use the top navigation bar to get back to your Project Overview by selecting Menu > Projects > Your projects > Top Level Project.
-
Select Clone. In the Clone with SSH section, select the Copy URL icon.
-
In your terminal or PowerShell, create a new directory called training in your home directory, and navigate into it.
1 2mkdir ~/training cd ~/training -
Copy the remote Top Level Project Git repository to your local computer.
1git clone <REPOSITORY-URL-YOU-COPIED> -
Move into the repository you just cloned. All files in this directory will be tracked by Git, and any Git commands you run in this lab should be run from this directory.
1cd top-level-project -
Show the contents of the directory, including hidden files and directories beginning with a period. Notice the presence of the
.gitdirectory, which turns this directory into a Git repository.-
Linux or macOS:
1ls -a -
Windows:
1ls -Force
-
-
Find out the directory contains any edited files that have not yet been committed to the repository:
1git statusYou’ll see
nothing to commitin the output, which means the files in this directory have the same contents as the versions of these files that are stored in Git.
E. Work on a branch
-
Create a new branch called temporary_branch on your computer.
1git branch temporary_branch -
Switch to the branch you just created.
1git checkout temporary_branch -
List all the branches in the repository.
1git branch -aThe red branches are on the remote server, which is the Denomas instance in your training environment. The asterisk indicates the branch you are currently on.
F. Edit a file
-
Using any text editor (Visual Studio Code, Sublime Text, notepad, vi, etc.), add this line to the end of
README.mdand save the file.a line added to temporary_branch locally -
See if Git has noticed that the file has been modified.
1git statusThe output shows that Git has detected that you have edited a file in your local repo, but since you have not committed that file, Git has not yet stored that change in a snapshot.
G. Add the edited file to Git’s staging area
-
Add the file to the staging area. If the command is successful, there will be no output.
1git add README.mdRemember that
git adddoesn’t moveREADME.mdon your filesystem, but it does add it to Git’s “staging area.” -
Make sure that
README.mdis now ready to be committed (that is, it has been successfully staged).1git status
H. Commit the edit
-
Commit the staged file.
1git commit -m "Add a line to README.md"You have now created a snapshot of the file that you can refer to later, if needed.
-
Make sure the staging area is empty again.
1git status
I. Push your changes to the Denomas instance
-
Create a new branch in the remote Git repository on the Denomas server called temporary_branch, and push your changes to that branch.
1git push -u origin temporary_branchIf you’re ever unsure of the exact command to push your changes to the remote server, type
git pushand Git will output an error message with the correct command for you to copy and paste.
J. Edit, commit, and push the file again
-
In your local machine’s text editor (not Denomas’s in-browser editor), add this new line to the end of your local copy of
README.mdand save the file.a second line in README.md -
In your terminal, move the edited file to Git’s staging area.
1git add README.md -
Commit the staged file.
1git commit -m "Modify README.md" -
See a description of the commit you just made.
1git log -
Push your commit up to the remote repository on the Denomas instance.
1git pushTo commit your changes to the upstream branch (that is, an already-existing branch on the remote repository with the same name as the branch on your local machine), you can just run
git pushinstead of the longer command you used the first time you pushed your commit up to the Denomas instance. The system only needs to set the upstream branch once. -
Navigate to your project in the Denomas app. Once you’re on the project’s main page, go to the left-hand navigation pane, select Repository > Branches, and select temporary_branch to switch to that branch. Confirm that the changes you made to
README.mdon your local branch were pushed up to the remote repository.
K. Edit a remote branch
Let’s simulate someone else in your organization making a change to the temporary_branch that lives in the remote repository on the Denomas instance. When we’re done with this section, the remote and local versions of temporary_branch will be different: the code on that branch will have moved under your feet (so to speak). In the section after this one, we’ll see how to reconcile this difference.
-
In Denomas, navigate to the Top Level Project landing page. If you’re not already on temporary_branch, go to the left-hand navigation pane and select Repository > Branches > temporary_branch.
-
You are now looking at files in temporary_branch. Select README.md to see its contents.
-
Select Web IDE to edit the file.
-
In the Web IDE screen, add a new line to the end of the file.
a third line added on the remote copy of temporary_branch -
Select Create Commit…
-
Normally every branch that you commit to needs an associated merge request, but for this lab you don’t need one. Check the radio button for Commit to temporary_branch and uncheck Start a new merge request.
-
Select Commit to finalize the changes on the remote repository’s temporary_branch. Since you made this change in Denomas webapp, the remote repository on the Denomas instance is now one commit ahead of your local repository.
L. Get metadata about changes to the remote temporary_branch
Your local temporary_branch is out of sync with the remote temporary_branch on the Denomas instance. The git fetch command gets the updated state of remote branches without updating the contents of your local branches. In other words, it tells you how many commits your local branches are behind the remote branches, but it doesn’t make any changes to the files in your local branches.
-
Retrieve metadata about branches on the remote copy of the repository.
1git fetch -
Find out how many commits are in the remote copy of the repository but not your local copy, or vice versa.
1git status
M. Pull from the remote repository
You need to update the contents of your local copy of temporary_branch by merging in changes from the remote copy of temporary_branch.
-
In your terminal, merge the remote copy into your local copy.
1git pullCheck the output to see how many files Git updated locally.
-
View the updated contents of the file. You should see the fourth line that you added in the Denomas Web IDE.
1cat README.md
N. Merge changes back into the main branch
Now that your local temporary_branch is identical to the remote temporary_branch, you can merge it into your local main branch. This will add your edits to the stable codebase that lives in main.
-
See what branch you are currently working on.
1git branch -
Switch to the main branch.
1git checkout main -
Incorporate all changes from your local temporary_branch (in this case, just the modified
README.md) into your local main branch.1git merge temporary_branch
O. Update the remote repository
-
Make sure there are no edited files that you need to stage or commit and to confirm that you are on the main branch.
1git status -
Update the remote copy of the main branch with any changes from your local copy.
1git push -
Return to the Denomas page in your browser and view
README.mdin your project’s main branch to view the changes you just pushed to the remote copy of main.
Suggestions?
If you’d like to suggest changes to the Denomas with Git Basics Hands-on Guide, please submit them via merge request.
17188382)
