GitHub, Git and VSCode

Today is Saturday February 15, 2020 and the temperature is about 35 F which is a great improvement from yesterday. We woke up at -15 F which was exactly 50 degrees cooler. I am on the second block for the day. My wife and I already had our tea with lemon and she is getting in the shower. She will let me know when ready so I can shutdown my computer and start working on lunch. Hopefully I will be able to complete this post today; otherwise it will be ready tomorrow morning.

Today we are planning on making a variation of arancini. Like any other family, we put our touch to the food we make. In general our arancini is a good representation of the original recipe. Today we are going to make a new variation because we want to make around 20 and would like to put some in the freezer. If you save arancini already make when you reheat them they tend to lose some character.

We are making the risotto as usual and setting it aside. We are making the meat component with onions and garlic as usual. When done we will add some cheese and green peas. The main variation is that we will mix the risotto and meat and make the balls. We are not stuffing the arancini with cheese and meat.

OK, my wife is calling me to start working on today’s lunch. I will pick up tomorrow morning.

I am back. It is Sunday morning. Yesterday my wife and I had some Genoa salami and crackers while working on the arancini. When we were ready to start the risotto but my wife was tiered of chopping so we decided to change the lunch plans. When we fix rice we tend to make more than we can consume at a time. We have some plastic containers that hold a pint of food. It happened that we had two containers with rice (not risotto) in the fridge. We just dumped them in the pot where we had the meat and veggies, added some red wine (all what was left in an open bottle), allowed the rice to reach temperature, and lunch was served!

We made enough meat sauce to have a couple servings each and to fill four pints with leftovers. We put the pints in the freezer. It is too early to decide on lunch for today. I just want to complete this post without further interruptions. That said I copied some software from this machine to another for testing. I started a test which should be done in a couple hours. I should be able to collect the log file for the storage server (a product I work on) and check if the run of 100,000 files generates issues. The build was from last Friday.

I read the article “Microsoft’s GitHub: Our new ‘gh’ command-line interface makes you more productive” and spent some time experimenting with the ‘gh’ CLI on my Windows machine.

GitHub offers developers an official command-line interface for managing code changes. GitHub has announced the beta of the command-line interface, called the GitHub CLI or ‘gh’ in the command line, which allows developers to type and execute code from the terminal rather than using a graphical integrated development environment (IDE) application. In general when a tool is integrated into different IDEs, the way to use varies. It is nice to have a CLI which you can use completely detached from the IDE implementation.

v0.5.5

https://github.com/cli/cli/releases/tag/v0.5.5

https://github.com/cli/cli/releases/download/v0.5.5/gh_0.5.5_windows_amd64.msi

c:\temp\gh_0.5.5_windows_amd64.msi

MIT License

C:\Program Files (x86)\GitHub CLI\

After installing the ‘gh’ CLI I issued some commands.

C:\>gh --version
gh version 0.5.5 (2020-02-13)
https://github.com/cli/cli/releases/tag/v0.5.5


C:\>gh --help
Work seamlessly with GitHub from the command line.

GitHub CLI is in early stages of development, and we'd love to hear your
feedback at <https://forms.gle/umxd3h31c7aMQFKG7>

Usage:
  gh [command]

Available Commands:
  help        Help about any command
  issue       Create and view issues
  pr          Create, view, and checkout pull requests

Flags:
      --help              Show help for command
  -R, --repo OWNER/REPO   Select another repository using the OWNER/REPO format
      --version           Show gh version

Use "gh [command] --help" for more information about a command.


C:\>gh help pr
Work with GitHub pull requests.

A pull request can be supplied as argument in any of the following formats:
- by number, e.g. "123";
- by URL, e.g. "https://github.com/OWNER/REPO/pull/123"; or
- by the name of its head branch, e.g. "patch-1" or "OWNER:patch-1".

Usage:
  gh pr [command]

Available Commands:
  checkout    Check out a pull request in Git
  create      Create a pull request
  list        List and filter pull requests in this repository
  status      Show status of relevant pull requests
  view        View a pull request in the browser

Global Flags:
      --help              Show help for command
  -R, --repo OWNER/REPO   Select another repository using the OWNER/REPO format

Use "gh pr [command] --help" for more information about a command.

It seems that it is somewhat premature to fully use the ‘gh’ CLI. Since I had already spent some time with it, decided to shift and use git. In general most of my code in GitHub is posted after it has been implemented and tested using my machine. In general I do not expect changes so I have not been using git for it. From now on I will use git on all my posts. That will motivate me in a few months to revisit the ‘gh’ CLI.

If you are interested in taking a look at ‘gh’ CLI you can find documentation on GitHub CLI. For my Windows machine I used an installer that can be found in the GitHub page for the CLI.

I went to GitHub and created an empty GitHubTest repository. I just added a README.md file and left the repository alone.

# **** clone the repository ****
c:\Users\John\workspace7>git clone https://github.com/JohnCanessa/GitHubTest.git
Cloning into 'GitHubTest'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.

# **** display the contents of my current directory ****
c:\Users\John\workspace7>dir
02/15/2020  10:48 AM    <DIR>          .
02/15/2020  10:48 AM    <DIR>          ..
01/30/2020  01:32 PM    <DIR>          .metadata
01/31/2020  09:02 AM    <DIR>          .vscode
01/05/2020  07:15 AM    <DIR>          3DSurfaceArea
01/06/2020  11:52 AM    <DIR>          AlmostSorted
02/06/2020  07:16 AM    <DIR>          AndProduct
01/24/2020  12:49 PM    <DIR>          AStarSearch
12/05/2019  01:23 PM    <DIR>          BiggerIsGreater
02/14/2020  03:00 PM    <DIR>          Cipher
12/22/2019  08:35 AM    <DIR>          DeterminingDNAHealth
01/08/2020  07:07 AM    <DIR>          EmasSupercomputer
02/12/2020  02:46 PM    <DIR>          FirstDuplicate
02/03/2020  09:07 AM    <DIR>          FlippingBits
02/15/2020  10:48 AM    <DIR>          GitHubTest       <===
01/31/2020  02:57 PM    <DIR>          guava-mini
01/31/2020  10:44 AM    <DIR>          HelloMaven
02/04/2020  02:04 PM    <DIR>          JumpingOnTheClouds
01/23/2020  08:05 AM    <DIR>          LarrysArray
12/03/2019  04:26 PM    <DIR>          MatrixLayerRotation
02/03/2020  10:42 AM    <DIR>          MiniMaxSum
01/25/2020  08:07 AM    <DIR>          PriorityQ
01/30/2020  01:42 PM    <DIR>          rtree2
02/03/2020  01:44 PM    <DIR>          SansaXOR
01/19/2020  09:16 AM    <DIR>          StacksReverseOrder
01/04/2020  07:51 AM    <DIR>          TheGridSearch
12/11/2019  10:09 AM    <DIR>          ToTrieOrNotToTrie

# **** display the contents of the new folder ****
c:\Users\John\workspace7>cd GitHubTest

c:\Users\John\workspace7\GitHubTest>dir /A
02/15/2020  10:48 AM    <DIR>          .
02/15/2020  10:48 AM    <DIR>          ..
02/15/2020  10:48 AM    <DIR>          .git
02/15/2020  10:48 AM                65 README.md

# **** list all remotes ****
c:\Users\John\workspace7\GitHubTest>git remote -v
origin  https://github.com/JohnCanessa/GitHubTest.git (fetch)
origin  https://github.com/JohnCanessa/GitHubTest.git (push)

Back in my machine I went to my workspace7 folder and cloned the repo just made to my machine. I then displayed the contents of the folder. As you can see the clone command created a new folder in my workspace.

I then moved to the new folder and listed its contents. As expected, only the README.md file was brought down from GitHub.

I checked that I was using the proper remote on my computer.

/**
 * Author: John Canessa - john.canessa@gmail.com
 */
public class Solution {

  /**
   * Test scaffolding.
   * 
   */
  public static void main(String[] args) {

    // **** display welcome message ****
    System.out.println("main <<< welcome !!!");

  }
}

I then wrote some basic code in Java using the Visual Studio Code IDE.

# **** get status ****
c:\Users\John\workspace7\GitHubTest>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)

        Solution.java

nothing added to commit but untracked files present (use "git add" to track)

# **** added the brand new Solution.java file ****
c:\Users\John\workspace7\GitHubTest>git add Solution.java

# **** checked status ****
c:\Users\John\workspace7\GitHubTest>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:   Solution.java

# **** commit the new file ****
c:\Users\John\workspace7\GitHubTest>git commit -m "added Solutions.java"
[master 84c743b] added Solutions.java
 1 file changed, 14 insertions(+)
 create mode 100644 Solution.java

# **** checked  status ****
c:\Users\John\workspace7\GitHubTest>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 # **** pushed the code to the repo **** c:\Users\John\workspace7\GitHubTest>git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 475 bytes | 475.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/JohnCanessa/GitHubTest.git
   1ab81f9..84c743b  master -> master

# **** checked status ****
c:\Users\John\workspace7\GitHubTest>git status
On branch master
Your branch is up to date with 'origin/master'.     <=== nothing to commit, working tree clean # **** listed the files in the repo **** c:\Users\John\workspace7\GitHubTest>git ls-tree master -l
100644 blob 3663da69de7a09042221118b5c2987fe4315ed15      63    README.md
100644 blob 685e9a26d2ce7d099a85a41df75a7c4b4f21448f     255    Solution.java   <===

Note that I have edited the commands to add an arrow “<===” to call your attention to relevant information.

The initial commit displayed a single message. The idea was to add some code in a couple iterations to illustrate the use of git (not ‘gh’ CLI) when accessing a GitHub repository.

The git commands I used are quite descriptive. I added some comments in case they are needed. When I was done with them I checked my GitHub repo and the Solution.java file had been uploaded.

c:\Users\John\workspace7\GitHubTest>git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean       <===

I went back to VSCode and made some additional changes.

# **** display git status ****
c:\Users\John\workspace7\GitHubTest>git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   Solution.java       <=== no changes added to commit (use "git add" and/or "git commit -a") # **** add the Solution.java file to the next commit **** c:\Users\John\workspace7\GitHubTest>git add Solution.java

# **** check how things are doing ****
c:\Users\John\workspace7\GitHubTest>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)

        modified:   Solution.java       <=== # **** commit our changes **** c:\Users\John\workspace7\GitHubTest>git commit -m "added good bye message"
[master 04d833e] added good bye message
 1 file changed, 5 insertions(+)

# **** check status ****
c:\Users\John\workspace7\GitHubTest>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 # **** check on the files in our repo **** c:\Users\John\workspace7\GitHubTest>git ls-tree master -l
100644 blob 3663da69de7a09042221118b5c2987fe4315ed15      63    README.md
100644 blob 3c477fdb81011124e75ef15a7d404b01eb61a774     407    Solution.java   <=== # **** push the changes to our master branch **** c:\Users\John\workspace7\GitHubTest>git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 408 bytes | 408.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/JohnCanessa/GitHubTest.git
   84c743b..04d833e  master -> master

# **** check status ****
c:\Users\John\workspace7\GitHubTest>git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

The git status command provides information about the state of your files. In our case git indicates that we have made changes to the Solution.java file. We follow the suggestion and add the Solutions.java file so our changes will be committed.

We then commit our changes. The git status command informs us that we are ready to commit our changes. We commit them with a message indicating we just added a good bye message.

We now are ready to push our changes back to the master branch. For sanity we check the sizes of the files in our repo. We then push our changes to the master branch. Status indicates that our local and remote repositories are in sync. You can verify this by using the GitHub web site.

In general when I work on a problem outside work, I tend to use the master branch. That said, when you are working on projects, the proper approach is to create a branch. Work on the branch. When all appears to be up and running on the branch code, then you can merge the contents of the branch back to master.

# **** list all branches ****
C:\Users\John\workspace7\GitHubTest>git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

# **** create new branch ****
C:\Users\John\workspace7\GitHubTest>git branch timefunc

# **** list all branches ****
C:\Users\John\workspace7\GitHubTest>git branch -a
* master
  timefunc              <=== remotes/origin/HEAD -> origin/master
  remotes/origin/master

# **** switch to the timefunc branch ****
C:\Users\John\workspace7\GitHubTest>git checkout timefunc
Switched to branch 'timefunc'

# **** ****
C:\Users\John\workspace7\GitHubTest>git branch -a
  master
* timefunc              <== remotes/origin/HEAD -> origin/master
  remotes/origin/master

# **** ****
C:\Users\John\workspace7\GitHubTest>git status
On branch timefunc      <==
nothing to commit, working tree clean

We start by checking the branches in our project. The * before master indicates we are on the master branch. The remote seems to be on sync with our master.

We create a new branch called timefunc. You should call your branch something descriptive. At work you could use the number for a bug or feature you will be working on.

Note that we are still on master but we have created out timefunc branch. We need to check out the timefunc branch so we can start working on it. Note that we have moved from the master branch to the timefunc. Since we have not made changes to the code yet, there is nothing to commit.

Now we spend some time adding code to our solution.

/**
 * Author: John Canessa - john.canessa@gmail.com
 * 
 * Testing a set of git commands to compare with the gh CLI.
 */
public class Solution {

  /**
   * This function loops displaying a message the number of times specified.
   */
  /**
   * Test scaffolding.
   * 
   * @throws InterruptedException
   */
  static void displayMessage(long delay, int times, String msg) throws InterruptedException {

    // **** loop displaying message ****
    for (int i = 0; i < times; i++) {

      // **** display message ****
      System.out.println("displayMessage <<< i: " + i + " msg ==>" + msg + "<==");

      // **** delay ****
      Thread.sleep(delay);
    }
  }

  public static void main(String[] args) throws InterruptedException {

    // **** display welcome message ****
    System.out.println("main <<< welcome !!!");

    // **** display messages a number of times ****
    long delay = 1000;
    int times = 7;
    String msg = "working hard ...";
    displayMessage(delay, times, msg);

    // **** display good bye message ****
    System.out.println("main <<< bye bye");
  }
}

The code is quite simple. Note that I have a typo which I deliberately made so you can go in and make an additional change on the branch before merging, or just leave it in the merge and come back later, make a branch or use the existing one, correct the location of the comment, merge and push.

main <<< welcome !!!
displayMessage <<< i: 0 msg ==>working hard ...<==
displayMessage <<< i: 1 msg ==>working hard ...<==
displayMessage <<< i: 2 msg ==>working hard ...<==
displayMessage <<< i: 3 msg ==>working hard ...<==
displayMessage <<< i: 4 msg ==>working hard ...<==
displayMessage <<< i: 5 msg ==>working hard ...<==
displayMessage <<< i: 6 msg ==>working hard ...<==
main <<< bye bye

The screen capture indicates that the welcome and exit messages work as expected. The displayMessage() function is called and seems to work well.

# **** used VSCode to add a function ... after we are done ****
C:\Users\John\workspace7\GitHubTest>git status
On branch timefunc
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   Solution.java       <=== no changes added to commit (use "git add" and/or "git commit -a") # **** add Solution.java to be commited **** C:\Users\John\workspace7\GitHubTest>git add Solution.java

# **** ****
C:\Users\John\workspace7\GitHubTest>git status
On branch timefunc
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   Solution.java       <=== # **** commit changes **** C:\Users\John\workspace7\GitHubTest>git commit -m "added displayMessage function"
[timefunc a95fc25] added displayMessage function
 1 file changed, 25 insertions(+), 1 deletion(-)

# **** get status ****
C:\Users\John\workspace7\GitHubTest>git status
On branch timefunc
nothing to commit, working tree clean

# **** push timefunc branch to GitHub ****
C:\Users\John\workspace7\GitHubTest>git push origin timefunc
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 777 bytes | 777.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'timefunc' on GitHub by visiting:
remote:      https://github.com/JohnCanessa/GitHubTest/pull/new/timefunc
remote:
To https://github.com/JohnCanessa/GitHubTest.git
 * [new branch]      timefunc -> timefunc

After we have made changes to our code and make it look like illustrated in this post, we check status. We have a file that has been modified.

We add the file with the changes. We commit the changes and finally we push them to the repo.

# **** inspect the log of the master branch ****
C:\Users\John\workspace7\GitHubTest>git log master
commit 04d833ec70cb4848c6833b538c6452b7c4082f6c (origin/master, origin/HEAD, master)
Author: JohnCanessa <john.canessa@gmail.com>
Date:   Sat Feb 15 11:10:22 2020 -0600

    added good bye message

commit 84c743be5ae39d7aa1869b78e488b8780fafb3d4
Author: JohnCanessa <john.canessa@gmail.com>
Date:   Sat Feb 15 10:54:53 2020 -0600

    added Solutions.java

commit 1ab81f911e8d4f1b49897c70cb7a3d82514fada4
Author: John Canessa <8019504+JohnCanessa@users.noreply.github.com>
Date:   Sat Feb 15 08:54:34 2020 -0600

    Initial commit

# **** inspect the log of the timefunc branch ****
C:\Users\John\workspace7\GitHubTest>git log timefunc
commit a95fc25938b1700430cbcb470916c736b18f0405 (HEAD -> timefunc, origin/timefunc)
Author: JohnCanessa <john.canessa@gmail.com>
Date:   Sun Feb 16 08:48:12 2020 -0600

    added displayMessage function

commit 04d833ec70cb4848c6833b538c6452b7c4082f6c (origin/master, origin/HEAD, master)
Author: JohnCanessa <john.canessa@gmail.com>
Date:   Sat Feb 15 11:10:22 2020 -0600

    added good bye message

commit 84c743be5ae39d7aa1869b78e488b8780fafb3d4
Author: JohnCanessa <john.canessa@gmail.com>
Date:   Sat Feb 15 10:54:53 2020 -0600

    added Solutions.java

commit 1ab81f911e8d4f1b49897c70cb7a3d82514fada4
Author: John Canessa <8019504+JohnCanessa@users.noreply.github.com>
Date:   Sat Feb 15 08:54:34 2020 -0600

    Initial commit

We inspect the log for the master branch. All is well. Note that as expected, it says nothing about our timefunc branch.

We then check the log for our timefunc branch. It does make a reference to our new function. It seems like all is well since we did not notice the position of the header label which is incorrect. That is just a cosmetic issue and at some point, probably during a code review, it will be noticed and corrected.

In general code reviews should occur before the code is committed and for sure before the branch is merged back to master.

Now it is time to merge our branch back to master.

# **** to change the active branch back to master ****
C:\Users\John\workspace7\GitHubTest>git checkout master
Switched to branch 'master'     <=== Your branch is up to date with 'origin/master'. # **** merge the new feature into the master branch **** C:\Users\John\workspace7\GitHubTest>git merge timefunc
Updating 04d833e..a95fc25
Fast-forward
 Solution.java | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

# **** git push ****
C:\Users\John\workspace7\GitHubTest>git push
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/JohnCanessa/GitHubTest.git
   04d833e..a95fc25  master -> master

# **** check status ****
C:\Users\John\workspace7\GitHubTest>git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

# **** inspect the master branch log ****
C:\Users\John\workspace7\GitHubTest>git log master
commit a95fc25938b1700430cbcb470916c736b18f0405 (HEAD -> master, origin/timefunc, origin/master, origin/HEAD, timefunc)
Author: JohnCanessa <john.canessa@gmail.com>
Date:   Sun Feb 16 08:48:12 2020 -0600

    added displayMessage function

commit 04d833ec70cb4848c6833b538c6452b7c4082f6c
Author: JohnCanessa <john.canessa@gmail.com>
Date:   Sat Feb 15 11:10:22 2020 -0600

    added good bye message

commit 84c743be5ae39d7aa1869b78e488b8780fafb3d4
Author: JohnCanessa <john.canessa@gmail.com>
Date:   Sat Feb 15 10:54:53 2020 -0600

    added Solutions.java

commit 1ab81f911e8d4f1b49897c70cb7a3d82514fada4
Author: John Canessa <8019504+JohnCanessa@users.noreply.github.com>
Date:   Sat Feb 15 08:54:34 2020 -0600

    Initial commit

We switch to the master because we intend to merge the contents of our branch into master (not the other way around).

We merge into master the contents of our branch. We push the updated code.

Our status indicates that we have nothing to commit and we are back on master.

We look at the log on master and we have the addition of the new function. I did go back to GitHub and checked the contents of the master branch. All the code seems to be up to date.

It is important when working with tools to make sure you take the proper steps in the proper order. You can achieve this by having a recipe of steps. In general there are a reduce number of states for your code which should be taken. For example, you do not work on the code before you check it out. You do not work directly on the master branch, you need a separate branch. I encourage you to make recipes. As a matter of fact, I always like to have commands grouped based on the typical workflow. In my opinion, help on APIs and CLIs should always list commands in groups. That said, you always want the alphabetical order in case you are looking for additional information on a specific command.

The entire code for this project can be found in my GitHub repository.

If you have comments or questions regarding this, or any other post in this blog, or if you would like for me to serve of assistance with any phase in the SDLC (Software Development Life Cycle) of a project associated with a product or service, please do not hesitate and leave me a note below. If you prefer, send me a private message using the following address:  john.canessa@gmail.com. I will reply as soon as possible.

Keep on reading and experimenting. It is the best way to learn, refresh your knowledge and enhance your developer toolset!

John

Twitter:  @john_canessa

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.