Basics of using Git & Github

Over the past few weeks I have been using VSCode with the Git integration to save all my code to Github. Here I will share what I have found useful, and how I got myself up and running using Git via the CLI on Windows.

Basics of using Git & Github

Background / Overview

(Feel free to skip over this section!)

This is the start of a blog post series about how I have started using Git, GitHub, VSCode etc. Now I would like to start by saying that I am not a developer by any shape or form, but more of a "script kiddy". My background started with writing batch scripts (albeit I still do write the occasional batch script!), then I started on VB scripts and now onto PowerShell. It is on my to-do list to start on some Python scripting, but that is definitely on the back burner at the moment!

I have been using VSCode as my primary coding tool for all my PowerShell scripts and even for the occasional batch script and JSON file editing (thanks to Windows Terminal, which I will come onto!). I then started to look at Github after Microsoft acquired it back in June 2018, but never did much with it as I mainly wanted to keep a private repo for myself that I could share with all my other team members. At that stage, it was a bit too expensive to warrant purchasing a Teams plan. Then back in April 2020, GitHub announced that it would then be free for teams 🥳. I immediately created a company/team account on GitHub, joined it and signed up my other team members onto it so that they can take advantage of sharing some scripts and automation in place.

I have to say that after using VSCode with GitHub has changed the way I store and use all my scripts. Everything is now on GitHub, and my colleagues have even made some changes to my scripts to make them better which is just awesome. The fact that we can share useful scripts has been a real time saver for us, and has improved our operational efficiency.

So if you are just starting with Git and GitHub, then I hope that you found some of this useful. I am going to be using this area as almost a scratchpad for reference later, and I will keep adding to this post, and I may create new posts if it warrants a separate post.

Where to start ❓❓

OK, I am making a big assumption that you are a Windows user (because that is what I am 😉) so therefore my instructions will be for those people that are using Windows 10.

I am going to begin by using the CLI (Command Line Interface) so that you can get to grips with using Git. The beauty of doing this is that the commands will be the same if you use Windows, Mac or Linux so, therefore, understanding some of the basic commands is the foundation to ensuring that you can use Git in any environment.

Firstly, I would highly recommend that you go and grab yourself a copy of Windows Terminal if you haven't already from the Microsoft Store. You can use the normal CLI or PowerShell, but I much prefer to use Windows Terminal:

Windows Terminal is continuously updated, and if you get it from the Windows Store it will automatically update itself on your PC. If you want to learn more about Windows Terminal, take a look on the GitHub repo:

microsoft/terminal
The new Windows Terminal and the original Windows console host, all in the same place! - microsoft/terminal

Next you need to install Git for Windows so that your PC has access to the required Git commands either from the command line or from VSCode and here is the download link for that application:

Git for Windows
We bring the awesome Git VCS to Windows

Once Git has been installed, you should probably go and grab yourself a free account on GitHub so that you can start doing some testing!

Build software better, together
GitHub is where people build software. More than 50 million people use GitHub to discover, fork, and contribute to over 100 million projects.

Phew! 🥵 That was a lot to take in, but hopefully you are in a position to start some testing. To recap, this is what you need:

  1. Windows Terminal or PowerShell
  2. GitHub for Windows installed
  3. An account on GitHub

Time to get started 🚀

Now that we have everything in place, I would first log into GitHub and create a new repository. Follow this awesome guide:

Create a repo - GitHub Docs
To put your project up on GitHub, you’ll need to create a repository for it to live in.

Once created, you should have something which looks like this:

Click on the green Code button, and copy the URL:

Open PowerShell in your terminal of choice (hopefully Windows Terminal 😉). First thing you should do is probably create a directory where you are going to store a local copy of your repo. In my case I have created a folder called github on my D:\ partition where I store all of my repos:

Don't worry if your terminal doesn't look like mine - I have customised mine with posh-git and oh-my-posh which makes it a little easier to use Git. But that will be a different blog post, as we are just starting with the basics here.

So make sure that you are in the directory where you want to store the repo (in my case it is D:\DATA\github).

Now run the following command, which is the URL that you copied earlier:

git clone http://github.com/username/repo.git

For example:

When doing a DIR, I can see that the repo has been cloned into a new folder called testRepo (where testRepo is the name of the repo that you created in GitHub).

CD into the folder, you should see the README.md file and a LICENSE file if you chose to create these. The posh-git and oh-my-posh modules are showing me that I am currently working on the master branch (more on branches to come!). Lets try creating a text file in this directory, and see what happens and how we can look at the status using Git.

Type on the command line:

notepad test.txt

Enter some text into Notepad and save it:

Hop back to your terminal, and do a DIR and you should be able to see the new file and I have been informed that there is a change:

That makes sense, as we just created a file in that directory. However, if you don't have these modules you can use git to get the status. Type the following command:

git status

Doing this, should tell you that you have some untracked files; namely test.txt. The message Git returns back is informative:

We haven't committed anything yet, but it telling us that we should probably run git add to ensure that Git knows to track these files. OK so lets do that, by running the following command:

git add .

or

git add test.txt

Running the git add command doesn't return anything, but running git status again which show us that the change has been tracked but hasn't yet been committed. The next step? Yep lets commit change:

git commit -m "Initial commit"

The -m option allows you to add a commit message:

The git status command show that we are on branch master, but our branch is 1 commit ahead of the remote version on GitHub. Again the git status is telling us to use git push to publish our local commit to GitHub:

If you go back to your GitHub repo in your browser, and refresh the page, you should be able to see the file we just created:

You can keep making changes to it locally and then commit and push it back to GitHub. Here I made another change to the file, and changed the commit message:

Here you can see the commit message has been updated, and the number of commits has been increased.

That now concludes this blog post, but keep an eye open for future posts on Windows Terminal customisation, Git branches and much more!