Everything you need to know to get started with Git and GitHub on a Windows machine
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
- Install Git
- Generate a local SSH key pair
- Upload the public SSH key to GitHub
- Connect and add local git repository to GitHub
- (Optional) Get the Git Bash terminal set up in Visual Studio Code
- Common Git commands
1. Install Git
- Download the latest Windows Git release from:
https://git-scm.com/download/winand 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.
Open Git Bash and enter the text below, substituting in your GitHub email address.
$ ssh-keygen -t ed25519 -C "email@example.com"
This creates a new ssh key, using the provided email as a label. The
-t ed25519parameter 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.
Next, you will be prompted to enter a file in which to save the key - press Enter. This accepts the default Windows file location:
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).
- 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).
- Login to your GitHub account
- Find your user icon (right-top) and select Settings -> SSH and GPG keys
- In the SSH keys section, hit the New SSH key green button
- Give the key a relevant title and copy-paste the public key
id_ed25519.pubcontent. The public key should look something like:
ssh-ed25519 BBBBC3NtaC1lZDI1NTE5C0CCIM1mCPMJPX+fdrE9ptMnRtB7b6FKEcxPro2Bkyho9mSR firstname.lastname@example.org
- 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.
- Go to
- Give the repository a name and leave the other options default then hit Create repository
- On your PC, create a folder with the same name and add your work inside it.
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 initInitializes 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 email@example.com:github_user/github_repo.gitReplace 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 masterPushes the updates to the GitHub master branch
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.
- Open Visual Studio Code and open the command palette by holding
Ctrl + Shift + P
- Type - Select Default Profile and select Git Bash from the options
- Click on the + icon in the terminal window
- The new terminal now will be a Git Bash terminal. You can load the terminal by holding
Ctrl + '
- 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 statusChecks the current git status (if there are any unsaved/uncommitted files) in the local directory
git initInitializes git repository on local machine
git add file_nameAdds 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 firstname.lastname@example.org:github_user/github_repo.gitclones GitHub repository to your local machine
$ git remote add origin email@example.com:github_user/github_repo.gitConnects local repo with GitHub repo
git push origin masterpushes the updates to the GitHub master branch
git branchShows info about the current branches
git checkout -b this-is-a-new-branchCreates a new branch
git checkout masterSwitches to the master branch
git diffShows what changes have been made to the repo
git mergeJoin two or more development branches together
git push --set-upstream origin new-feature-branchPushes new branch to GitHub
I hope you enjoyed following this mini guide and learned something new today!