Beginning Git Exercise (using Source Control GUI)
Intro
After you have gone through the Beginning Git tutorial, you can rebuild the git-demo project using the VS Code Source Control sidebar wherever possible.
The below actions are from the Beginning Git tutorial but without the detailed explanations.
Repeat the steps on this page one or more times until you are comfortable with the Git actions and how to use the Source Control GUI to enact them.
The below actions are from the Beginning Git tutorial but without the detailed explanations.
Repeat the steps on this page one or more times until you are comfortable with the Git actions and how to use the Source Control GUI to enact them.
VS Code Source Control feature:
- The Source Control sidebar is built into Visual Studio Code.
- Open it by clicking the Source Control toolbar on the left side Activity Bar:
- The More actions drop-down menu is available by clicking
⋯
at the top of the Source Control sidebar. - The most commonly used Git commands have a GUI counterpart. Some, including logs, do not.
Project setup
Create project folder:
Open the Explorer sidebar.
Create files:
Initialize git repository:
Open the Source Control sidebar.
Open the Source Control sidebar.
Stage and Commit Changes using Source Control toolbars
:
- mkdir git-demo
- cd git-demo
Create files:
app.txt
This is a demo app to learn Git.
.env
Sensitive Information Goes here
ignoreme.txt
Git ignores this whole file.
Set up a Local Git Repository
.gitignore: Create a .gitignore file:.gitignore
.DS_Store .env ignoreme.txt
Initialize git repository:
Initialize Repository
Click the Initialize Repository button
Commits
Make initial commit:
Stage and Commit Changes using Source Control toolbars
- Stage changes. Move files from "Changes" to "Staged Changes":
+
Click + to right of each file you want to stage. For all changed files click + to right of the "Changes" heading.
- Commit staged changes:
Enter message in box > ✓Commit
Enter a commit message in theMessage
box at the top of the Source Control sidebar. Since it's our first commit enter "First commit". Then click the✓Commit
button below it.
- Print the log of the first commit: git log
Make changes and second commit:
app.txt
This is a demo app to learn Git. Second line.
Add file2.txt file:
file2.txt
Some text.
Stage and Commit Changes using Source Control drop-down menu:
- Stage changes. Move files from "Changes" to "Staged Changes":
⋯ > Changes > Stage All Changes
- Commit staged changes:
⋯ > Commit > Commit Staged
Enter commit message, save and close.
- Print log of the first two commits: git log
Stage and commit changes with one command:
Modify app.txt file:
app.txt
This is a demo app to learn Git. Second line modified.
Create another file called file3.txt and save it.
file3.txt
Some text.
Stage and Commit Changes in one step:
Enter message in box > ✓Commit > Yes
Enter "Add file3" in message box > Then click✓Commit
button > Then click Yes to confirm.
- Print log of the first three commits: git log
Fourth commit
file4.txt
Some text.
app.txt
This is a demo app to learn Git.
Second line modified.
Third line.
Stage and Commit Changes using Source Control toolbars
:
Stage and Commit Changes using Source Control drop-down menu:
+
Click + to right of the "Changes" heading.Message box: Add file4 > ✓Commit
Enter commit message "Add file4" in theMessage
box. Then click the✓Commit
button.
Stage and Commit Changes using Source Control drop-down menu:
⋯ > Changes > Stage All Changes
⋯ > Commit > Commit Staged
Enter commit message "Add file4" and close.
- Print log of the first four commits: git log
Branches
Step 1: Create a new branch called feature1
⋯ > Branch > Create branch: Name: "feature1"
- Or Click status bar branch name:
Click "Main" branch in Status Bar > Select Create New Branch > Enter: feature1
Step 2: Make file changes
Add new file named feature1.txt:
feature1.txt
Some text.
- Then modify app.txt by adding "Feature 1" text to it.
app.txt
This is a demo app to learn Git. Second line modified. Feature 1.
Step 3: Stage and Commit Changes
Use one of the below methods:
Source Control toolbars:
+
Stage changes by clicking + to right of "Changes" or of individual files.Enter message in box > ✓Commit
Enter "Add feature 1" in message box, then click✓Commit
button.
⋯
Source Control dropdown menu:⋯ > Changes > Stage All Changes
Stage changes.⋯ > Commit > Commit Staged
Commit staged changes. Enter commit message and close.
Step 4: Switch back to main branch
Use one of the below methods:⋯ > CheckoutTo > Select main
Using the Source Control dropdown menu.
Using the Status bar branch section: Click "feature1" > Select "main" branch.
Step 5: Merge feature1 into the main branch
⋯ > Branch > Merge branch > Select feature1
Step 6: Delete feature1 branch
⋯ > Branch > Delete branch > Select feature1
- Print log of the first five commits: git log
Create and merge a second feature branch
Step 1: Create a new branch called feature2
Click "Main" branch in Status Bar > Select Create New Branch > Enter: feature2
Step 2: Make file changes
feature2.txt
Some text.
app.txt
This is a demo app to learn Git. Second line modified. Third line. Feature 1. Feature 2.
Step 3: Stage and Commit Changes
Use one of the below methods:
Source Control toolbars:
+
Stage changes by clicking + to right of "Changes" or of individual files.Enter message in box > ✓Commit
Enter "Add feature 2" in message box, then click✓Commit
button.
⋯
Source Control dropdown menu:⋯ > Changes > Stage All Changes
Stage changes.⋯ > Commit > Commit Staged
Commit staged changes. Enter commit message and close.
Step 4: Switch back to main branch
Using the Status bar branch section:On the status bar, click "feature2" branch name > Select "main" branch.
Step 5: Merge feature1 into the main branch
⋯ > Branch > Merge branch > Select feature2
Step 6: Delete feature1 branch
⋯ > Branch > Delete branch > Select feature2
- Print log of the first six commits: git log --oneline
File States and VS Code file name color coding
Make sure you are familiar with the file states and:
- Their associated Git areas: N/a, Working Directory, Staging Area
- Their color coding in VS Code Explorer: Gray, Green, White, Yellow
- And their headings in VS Code Source Control: N/a | Changes | Stage Changes.
File State | Git Area | VS Code Explorer filename color | VS Code Source Control heading |
---|---|---|---|
Ignored: In the .gitignore file | n/a | Gray | n/a |
Untracked: New file never committed to Git
| - Working Directory Staging Area | - Green U Green A | - Changes Staged Changes |
Tracked: Previously committed to Git
| - n/a Working Directory Staging Area | - White Yellow M Yellow M | - n/a Changes Staged Changes |