Everything you need to know to get started with Git and GitHub on a Windows machine

68747470733a2f2f6d69726f2e6d656469756d2e636f6d2f6d61782f313430302f312a6d74736b3366515f4252656d466964686b656c3364412e706e67.png

Git is the golden standard in software version control and it will probably remain so for the foreseeable future. Git allows you to save and track changes to your code in a manageable way and collaborate with other developers in building scalable applications.

GitHub is the most popular cloud-based hosting service that lets you host and manage Git repositories.

This post aims to clarify the distinction between Git and GitHub and to explain everything you need to know before you get started with these tools on your Windows machine.

To follow along with this post, you will need:

  • a Windows 10 PC
  • a GitHub account
  • (optional) Visual Studio Code

Steps:

  1. Install Git
  2. Generate a local SSH key pair
  3. Upload the public SSH key to GitHub
  4. Connect and add local git repository to GitHub
  5. (Optional) Get the Git Bash terminal set up in Visual Studio Code
  6. Common Git commands

1. Install Git

  1. Download the latest Windows Git release from: https://git-scm.com/download/win and follow along with the installation steps. I recommend that you stick with the recommended options unless you are an advanced user and know what you are doing. Default options will work fine for most users.

2. Generate a local SSH key pair

To connect your local repository to GitHub or pull existing repositories from GitHub, you will need to generate a public/private key pair. There are several methods to generate SSH key pairs on a Windows machine, such as the built-in OpenSSH tool or downloading the PuTTY utility, however we will use the freshly installed Git Bash.

  1. Open Git Bash and enter the text below, substituting in your GitHub email address. $ ssh-keygen -t ed25519 -C "your_email@example.com"

    This creates a new ssh key, using the provided email as a label. The -t ed25519 parameter stands for the algorithm used to encrypt the keys. Ed25519 is one of the most powerful public-key algorithms available today, considered to be superior to the likes of DSA or RSA.

  2. Next, you will be prompted to enter a file in which to save the key - press Enter. This accepts the default Windows file location:
    /C/Users/your_user/.ssh/id_ed25519

  3. A prompt will come up, asking you to type a secure passphrase. It is a good practice to enter a strong passphrase to protect your key against someone who gains access to your machine, however alternatively you can hit Enter to skip this step.

    A set of public and private keys have been created in /C/Users/your_user/.ssh/: id_ed25519 (this is the private key and it will remain on your machine) and id_ed25519.pub (this is the public key and it will uploaded to GitHub).

  4. The final thing you want to do is to add your SSH key to the SSH agent. The SSH agent is essentially a key manager for SSH which holds your keys in memory, unencrypted, and ready for use by SSH. It also saves you from typing a passphrase every time you connect to a server.

$ eval "$(ssh-agent -s)" Creates an ssh agent process

$ ssh-add ~/.ssh/id_ed25519 Adds the newly created key to the agent

3. Upload the public SSH key to GitHub

As in any public-private key SSH connection, you should copy the public key to the remotely accessed machine (in this case, GitHub) and keep the private key securely on the client machine (your PC).

  1. Login to your GitHub account
  2. Find your user icon (right-top) and select Settings -> SSH and GPG keys
  3. In the SSH keys section, hit the New SSH key green button
  4. Give the key a relevant title and copy-paste the public key id_ed25519.pub content. The public key should look something like: ssh-ed25519 BBBBC3NtaC1lZDI1NTE5C0CCIM1mCPMJPX+fdrE9ptMnRtB7b6FKEcxPro2Bkyho9mSR your_email@example.com
  5. Hit Add SSH key

4. Connect and add local git repository to GitHub

Typically, you would want to create a blank repository in GitHub first, give it a name, and then use that same name for the local directory where your work is saved.

  1. Go to https://github.com/new
  2. Give the repository a name and leave the other options default then hit Create repository
  3. On your PC, create a folder with the same name and add your work inside it.
  4. You will need to initiate the local git repository, add it, commit it, and then link it to the GitHub repository by applying the following commands in Git Bash (first navigate to your folder using the cd command):

    $ git init Initializes your local Git repository, it will essentially tell your machine to treat this as a Git repo

    $ git add . Adds all the new files and changes to the repo

    $ git commit -m "My commit message here" Commits the updates

    $ git remote add origin git@github.com:github_user/github_repo.git Replace the URL with your repo. To find your URL, navigate to your repo in GitHub and access the green Code button, then select SSH and copy the string

    $ git push origin master Pushes the updates to the GitHub master branch

  5. At this point, your Git repo should be hosted by GitHub, so head to the website and refresh the repo page - you should see your files there.

5. (Optional) Get the Git Bash terminal set up in Visual Studio Code

Getting the Git Bash terminal configured in Visual Studio Code is extremely useful and simple. It allows you to visualize your files and run Git commands on them directly in VSC.

  1. Open Visual Studio Code and open the command palette by holding Ctrl + Shift + P
  2. Type - Select Default Profile and select Git Bash from the options
  3. Click on the + icon in the terminal window
  4. The new terminal now will be a Git Bash terminal. You can load the terminal by holding Ctrl + '
  5. You can now toggle between the different terminals as well from the dropdown in terminal.

6. Common Git commands

For a wrap-up of this guide, I will add a few very common Git commands alongside an explanation for each one:

  • git status Checks the current git status (if there are any unsaved/uncommitted files) in the local directory

  • git init Initializes git repository on local machine

  • git add file_name Adds specified file to the repo. Alternatively, you can use "." to add eveything

  • git commit -m "This is a commit message." -m "Some description here." Commits the updates locally only, not in GitHub

  • git clone git@github.com:github_user/github_repo.git clones GitHub repository to your local machine

  • $ git remote add origin git@github.com:github_user/github_repo.git Connects local repo with GitHub repo

  • git push origin master pushes the updates to the GitHub master branch

  • git branch Shows info about the current branches

  • git checkout -b this-is-a-new-branch Creates a new branch

  • git checkout master Switches to the master branch

  • git diff Shows what changes have been made to the repo

  • git merge Join two or more development branches together

  • git push --set-upstream origin new-feature-branch Pushes new branch to GitHub

Conclusion

I hope you enjoyed following this mini guide and learned something new today!

Comments (2)

Catalin Pit's photo

This would have been useful a few months ago when I set up my Windows machine!

Thank you for sharing!

Mihai Viisan's photo

Thanks for the comment Catalin :)