Open CheatSheet Back To List Next Tutorial

Intermediate Git Exercise (using Source Control GUI)

Intro

  • This practice exercise repeats the actions from the Intermediate Git Tutorial, without the detailed explanations. 
  • It uses the VS Code Source Control GUI toolbars and menus where possible.
  • Go through the exercise one or more times until you are comfortable with all the actions and the interface.  Use the CheatSheet as reference.

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.

Reset Git back to the beginning of the tutorial:
  • To start over from the start of the Intermediate Git tutorial, do a hard reset from the commit with the v1-beginning-git tag.
    • git reset --hard v1-beginning-git
  • Open the Source Control sidebar: 
  • Delete the tags:
    • ⋯ > Tags > Delete Tag > Select tag name: v1-beginning-git  
    • ⋯ > Tags > Delete Tag > Select tag name: v2-intermediate-git 
  • Delete the practice branch:
    • ⋯ > Branch > Delete Branch > select practice



Intermediate Git (using VS Code Source Control GUI)

Tags

Add tag to last commit from Beginning Git project.

  • git glog log the list of commits.

  • Open the Source Control Sidebar.
  • Add a tag to the last commit:
    • ⋯ > Tags > Create Tag > Tag name: v1-beginning-git > Optional tag message Leave the message blank and press escape.
  • git glog Tag is listed in the log.
  • git tag -l List tags in alphabetical order. We just have the one.
  • Delete the tag:
    • ⋯ > Tags > Delete Tag > Select tag name: v1-beginning-git 
  • git glog Tag should be gone.
  • Add the tag again, this time with a message:
    • ⋯ > Tags > Create Tag > Tag name: v1-beginning-git > Optional tag message: Beginning Git 
  • git glog
  • git tag -ln List all tags including the message.



Git Undo/Redo

Create Practice Branch:

Create and checkout a new branch in one of the two following ways:
  • ⋯ > Branch > Create Branch > Name: practice 
  • Click the current branch name "main" at the bottom left of the status bar > Create New Branch > Name: practice



Stage/Unstage Changes

  • Open the Explorer sidebar: 

Add file:
wrongfile.txt
Some text.

Add a line to the bottom of app.txt 
app.txt
This is a demo app to learn Git.
Second line modified.
Third line modified in main branch and modified in feature3 branch.
Feature 1.
Feature 2.
Feature 3.
Wrong text.

  • Open the Source Control sidebar: 
  • Stage the changes using one of the two following ways:
    • + Click the + to the right of the Changes heading or to the right of the file name.
    • ⋯ > Changes > Stage All Changes 
  • Unstage the Changes using one of the two following ways:
    • - Click the - to the right of the Staged Changes heading or to the right of the file name.
    • ⋯ > Changes > Unstage All Changes 


Discard all changes from the Working Directory

  • Permanently discard the changes since your last commit:
    •  ⋯ > Changes > Discard All Changes 


Amend the last commit

  • Open the Explorer sidebar: 
  • Add a file called practice.txt with some text. 
practice.txt
Some text.

  • Open the Source Control sidebar: 
Stage and commit the changes using one of the two following ways:
  • Using Source Control toolbars:
    • + Stage the changes by clicking the + to the right of the Changes heading or to the right of the file name.
    • Enter message in box: Add practice.txt > ✓Commit button  Enter 'Add practice.txt in the Message box, then click the ✓Commit button.
  • Using Source Control menu items:
    • ⋯ > Changes > Stage All Changes 
    • ⋯ > Commit > Commit Staged  > In the COMMIT_EDITMSG file Enter message: Add practice.txt, save and close it.

  • Add a line to the file and save it: 
practice.txt
Some text.
Second line.

  • + Add the file to the staging area.
  • In the Terminal, log the last 2 commits: git olog -2 

  • Amend the staged change to the last commit:
    • Using the same commit message: 
      • ⋯ > Commit > Commit Staged (Amend) > Close the COMMIT_EDITMSG file.
    • Using a new commit message: 
      • Message box: Add practice.txt amended > ⋯ > Commit > Commit Staged (Amend) 

  • git olog -2 Logging the last 2 commits shows the last commit was replace with this amended commit.



Revert 

  • Note there is not a Source Control menu item that duplicates the git revert command. 
  • Git revert creates a new commit that undoes the changes from the previous commit.


Undo last commit

  • The last commit added practice.txt file.
  • To undo the commit and put all changes in the staging area:
  • ⋯ > Commit > Undo Last Commit 
  • git olog -1 Logging the last commit shows the Add practice.txt commit is gone.

  • Commit it again: 
    • Enter message in box: Add practice.txt > ✓Commit button  Enter 'Add practice.txt in the Message box, then click the ✓Commit button.
    • git olog -2 The log will show the commit is back, with a different commit Id.



Stash code

  • Go back to the main branch:
    • Click the current branch name "practice" at the bottom left of the status bar > Select "main".
  • Delete the practice branch: ⋯ > Branch > Delete Branch > select: practice

  • Add a line to the app.txt file. Then save it.
app.txt
This is a demo app to learn Git.
Second line modified.
Third line modified in main branch and modified in feature3 branch.
Feature 1.
Feature 2.
Feature 3.
Work in progress.

  • Stash the change: ⋯ > Stash > Stash > Message: 'Add line to app.txt' 
  • List your stashes: git stash list 
  • Remove the stash and put it back into your working directory: ⋯ > Stash > Pop Latest Stash
  • You should have no stashes: git stash list 
  • Discard all changes since last commit: ⋯ > Changes > Discard All Changes 
  • Add the Work in progress. line back in app.txt and save the file.
  • Stash it again but add a message to the stash:  ⋯ > Stash > Stash > Message: 'Add line to app.txt' 
  • Add file named feature4.txt.
feature4.txt
Some text.

  • Stash the file: ⋯ > Stash > Stash (Include Untracked) > Message: 'Feature 4' 
  • List the stashes: git stash list 
  • Show information about the stash with index 1: git stash show 1 
  • If you add the -p option it will show the specific changes: git stash show 1 -p 
  • Retrieve the stash code without deleting the stash: ⋯ > Stash > Apply Latest Stash
  • Feature4 stash is still there: git stash list 
  • Delete the last stash: ⋯ > Stash > Drop Stash > select Feature 4
  • Feature 4 stash is gone: git stash list 
  • Stash it again: ⋯ > Stash > Stash (Include Untracked) > Message: 'Feature 4' 
  • Remove all stashes: ⋯ > Stash > Drop All Stashes
  • All stashes are gone: git stash list 


Add tag to last commit

  • Add a tag to the last commit with a tag message. 
    • ⋯ > Tags > Create Tag > Tag name: v2-intermediate-git > Intermediate Git 
  • List all your tags: git tag -l 
  • List all your branches (main and practice): git branch 
  • Log all your commits: git glog 

Open CheatSheet Back To List Next Tutorial