Git is a popular distributed version control system used by developers around the world to manage code repositories. If you’re new to Git and want to get started, this guide will help you with the initial setup and introduce you to the basics of setting up SSH keys, and cloning your first repository using PowerShell.
This guide will cover:
- Installing Git
- Creating and using SSH Keys
- Cloning your first Git project
- Uploading files to your GitHub Project
Prerequisites
- PowerShell
- GitHub account
- (Preferably Windows 11)
Installing Git
To begin using Git on your Windows 11 or Windows 10 device, the first step is to install it. Luckily, this can be easily done directly in PowerShell using the Windows Package Manager CLI, also known as “winget.” This method is only available on devices running Windows 10 1709 (build 16299) or later, so make sure your device meets this requirement before proceeding. If are not running Windows 11 or Windows 10 1709 (build 16299) or later, Git can be downloaded and installed manually from here.
To install Git with winget, open PowerShell and run the following command:
winget install --id Git.Git -e --source winget
By using this command your installation will commence as shown:
Found Git [Git.Git] Version 2.39.2
This application is licensed to you by its owner.
Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
Downloading https://github.com/git-for-windows/git/releases/download/v2.39.2.windows.1/Git-2.39.2-64-bit.exe
██████████████████████████████ 50.5 MB / 50.5 MB
Successfully verified installer hash
Starting package install...
Successfully installed
After installing Git, you must restart your PowerShell to activate the installation. Next, you’ll need to set up SSH keys to start using Git with GitHub.
Setting up SSH Keys
If you’re new to Git and GitHub, you might be wondering what SSH keys are and why they’re important. Simply put, SSH keys are a way to securely connect to GitHub without having to enter your username and password every time. This is one of the most common ways of interacting with Git and this section will walk you through the setup phase. To learn more about SSH and its function in Git, look here.
Step 1: Check for Existing Keys
Before we begin, we need to check if you already have an SSH key. Open PowerShell and run the following command:
ls ~/.ssh/
If you see a file named “id_<encryption>.pub"
, you already have an SSH key pair and you can move on to step 3. If not, move on to step 2
Step 2: Generate a New SSH Key Pair
To generate a new SSH key pair, run the following command:
ssh-keygen -t ed25519 -C "your_email@example.com"
Note: the encryption method chosen is ed25519 for this example, but others exist, for more look here
This will generate a new SSH key pair and prompt you to enter a passphrase. The passphrase is an extra layer of security that protects your private key. You can leave it blank if you prefer, but we recommend adding one.
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\AmBroke/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\AmBroke/.ssh/id_ed25519
Your public key has been saved in C:\Users\AmBroke/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:zWCsZggEKolO0r2HLHmJ3/J6TvZsr9G5QhfPdJOiz20 db@mindcore.dk
The key's randomart image is:
+--[ED25519 256]--+
|... |
|o+ . . |
|*.o . + . |
|= = = o +. o + |
| .+ B = S o* o . |
| + = ..o.o |
| o +...oo . |
| =.oo. .o E |
| .+o.++o . |
+----[SHA256]-----+
With this complete, you have just created your first SSH Key-set! Now we can start using it to connect with GitHub.
Step 3: Add Your Public Key to GitHub
Now that you’ve generated an SSH key pair, it’s time to add your public key to GitHub. To do so you must first find and open your public key. Assuming you used the default save file, use the “Get-Content” command as shown below.
Get-Content C:\Users\$env:UserName\.ssh\id_ed25519.pub
By running this command your public key shows up in the terminal, in my case:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPzcjXIWSUXpi8kFocFDqJNteKKAC0dbGrs3IbRVI5fU db@mindcore.dk
Note: Make sure to never share your private key with anyone!
To add this key, go to your GitHub account settings and click on “SSH and GPG keys”. Then, click on “New SSH Key”. If you are already logged in on GitHub, simply click here.
Then give the key a title, in this case “Mindcore Blog”, usually you would name it after your device, to make the key identifiable for you later, and insert your key in the Key field and press “Add SSH key”.
With your Key added, go back into the “SSH and GPG keys” section (here) and you should see your new key added.
If it looks like this, then you have sucsessfully added your SSH key to GitHub, and you can now start using GitHub in earnest.
Creating & Cloning Your First Repository to Github
With your SSH setup in hand, navigate on GitHub the plus (+) button in the top-right corner of the screen. Select “New repository” from the dropdown menu.
Give your repository a name and a brief description. You can choose to make it either public or private. A public repository is visible to anyone, while a private repository is only visible to you and those you grant access to.
In this example, the name is “Mindcore Blog Tutorial” which is automatically connected with Dashes (-) due to git convention. Secondly, the repository is set to private. Finally a README file is added, which we will use later.
Now that you’ve created a new repository, let’s clone it to your local machine using PowerShell. Navigate to the directory where you want to clone the repository using the “cd” command. For example, if you want to clone it to your “Documents” folder, enter:
cd ~\Documents
Copy the SSH address of the repository you just created by clicking on the green “Code” button on the repository’s home page and selecting “SSH” from the dropdown menu.
In PowerShell, clone the repository using the following command, replacing the git@github.com part with your reposity:
git clone git@github.com:dbmindcore/Mindcore-Blog-Tutorial.git
This will promp you to accept the ED25519 key fingerprint from github, simply type “yes” and the repository is cloned to your computer like as shown below:
Cloning into 'Mindcore-Blog-Tutorial'...
The authenticity of host 'github.com (140.82.121.4)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
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
Receiving objects: 100% (3/3), done.
To view your newly cloned GitHub repository either cd into it in powershell, or simply open it in File Explorer, where a README.md file is waiting for you as shown below:
Congratulations! You’ve successfully created and cloned your first GitHub repository using PowerShell, now lets start pushing files to it!
Pushing Files to Your GitHub Repository
Now that you have the repository cloned to your local machine, you can make changes to the code. In this example, we will take the README.md file. If you did not make one, simply create a README.md file in your repository folder. If you want to learn the basiscs of Markdown and why we use it in Git, click here.
In this example i use Visual Studio Code to open up the file, but something as simple as notepad would also work inserting the following Markdown:
## Want to Learn More About Markdown?
Check out [This Page](https://blog.mindcore.dk/2023/03/write-smarter-not-harder-a-beginners-guide-to-markdown/)
As shown in Visual Studio Code at line 4/5 the bottom i have added the text:
Before pushing your changes to the repository you’ll need to configure Git with your username and email address so that Git can properly attribute your commits. To do this, open up a terminal window and run the following commands with your own email and name:
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
Once you’ve made your changes and configured your Git, you’ll need to add and commit them to the local repository. To add a specific like the README.md file use the command below:
git add .\README.md
It is also possible to add all changes using “git add .”, however this can lead to merge conflicts and other complications to be disucssed in future blogposts. For now simply add the README.md file.
Next, commit the changes using the following command:
git commit -m "Commit message here"
Replace “Commit message here” with a brief description of the changes you’ve made.
Finally, you’re ready to push your changes to the remote repository. Use the following command to push your changes:
git push
This will push your changes to the repository, and look simmilar to the following in your terminal:
git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 441 bytes | 441.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:dbmindcore/Mindcore-Blog-Tutorial.git
08c86f7..7df74be main -> main
Now look on your Github repository and you will see the changes you have made pushed as shown below:
Congratulations! You’ve successfully pushed your changes to a Git repository using PowerShell and SSH.
Conclusion
In conclusion, Git and GitHub can be powerful tools for managing your code and collaborating with others. By installing Git, creating and using SSH keys, cloning your first Git project, and uploading / update files to your GitHub project, you now have a solid foundation for using Git and GitHub to manage your code. Whether you are working on a small personal project or a large team project, Git and GitHub provide a streamlined and efficient way to manage your code changes and collaborate with others. With the knowledge and skills you have gained from this tutorial, you are now ready to start using Git and GitHub to manage your code and take your projects to the next level. So get out there, start collaborating, and happy coding!
M.Sc Cybersecurity student specializing in Microsoft Sentinel Solutions.