DEV Community

Cover image for Getting started with Git (on windows)
Dev
Dev

Posted on

Getting started with Git (on windows)

In this blog, we will get to know how to get started with Git.
So before we begin, first we need to know what exactly is 'Git'.

Git is a version control system which is used to maintain project journey and push and get the updates to and from an online repository.

A Version Control System is a software used to keep track of the changes made in a software by maintaining its multiple versions which can be referred to and retrieved as and when required. This helps the developers in a team to coordinate with each other easily.

Now we have got an idea about Git. So let's begin learning Git:

Requirements

To work with git on windows, we need to install Git Bash

1. Setting up SSH Keys

While working with Git, there are two types of repositories that we can encounter: local repository and remote repository.

A local repository is a folder in our system where all the files related to the project are stored. This repository is present in our local systems and is thus not accessible globally.

A remote repository is the one that exists on the internet on platforms like GitHub, Bitbucket and GitLab which can be accessed globally. GitHub is the most popular of all with a large number of repositories related to open source applications. In this blog, we will be using setting up our remote repository on GitHub.

For accessing the remote repository, we need to authenticate ourselves as the rightful user/collaborator of the repository and for that we need to enter our credentials of the remote repository account everytime we need to perform a task. So to make authentication process untidious, we setup SSH Keys.

SSH Keys created a connection between systems and thus we are able to communicate with remote systems easily.

To set up an SSH Key:
i) Go to the Command Prompt and type the following command:

ssh-keygen
Enter fullscreen mode Exit fullscreen mode

ii) Select the location where you want to store your SSH keys.
iii) Enter the passphrase characters. Passphrase characters are used to encrypt the text in a more secured way. For the ease of usage, we can skip this by simply pressing the "Enter" key. After this your SSH key will be generated.
iv) In the location where you have stored the SSH Key, find the file with .pub extension(this file contains your public key). Copy the contents of this file.
v) Go to the settings of your repository provider and then search for the label 'SSH' and generate a new SSH Key.
vi) Paste the contents of the file here and you can also name it to remember the system for which the key have been generated.

Now you have established a connection between your system and the remote repository account.

2. Setting up Global User and Email

While working with remote repositories, we need to mention the identity of the user who has performed a certain commit('commit' is explained in the coming text). For that, we need to set up global user and email.

To set up global user and email, enter the following commands in your git bash:

[Note: The things enclosed in <> are to be replaced with the actual content]

For user:

git config --global user.name <user_name>
Enter fullscreen mode Exit fullscreen mode

For email:

git config --global user.email <user_email>
Enter fullscreen mode Exit fullscreen mode

It is preferred to use the email with your repository account.

3. Creating a new remote repository

To create a new remote repository:
i) Open your web browser and navigate to repository provider website of your choice and create account(if you don't have already).
ii) Find and click on the 'New repository' button. Enter the details such as name of the repository, public/private access etc.
iii) Click on the create repository button. Your remote repository has been created.

4. Creating a local repository

We can create a git repository in two ways:

A) Initialising locally:

i) Go to the folder which you want to initialise as your git repository
ii) Inside the folder, open git bash and enter the following command:

git init
Enter fullscreen mode Exit fullscreen mode

Your folder as been initialised as a local git repository. A .git folder has been created in your working directory/folder(enable "Hidden items" settings from "View" if not visible).
iii) Now open your browser and navigate to the remote repository(create one if you don't have already) and click "Code".
iv) Under "HTTPS", copy the url using the clipboard button.
v) Go to git bash and paste it after the "git remote" command as follows:

git remote add origin <remote_repository_url>
Enter fullscreen mode Exit fullscreen mode

The local repository is now connected with the remote repository.

B) Cloning an existing remote repository:

i) Open your web browser and navigate to the repository you want to clone.
ii) Click "Code" and under "HTTPS", copy the url using the clipboard button.
iii) Go to git bash and paste it after the "git clone" command as follows:

git clone <remote_repository_url>
Enter fullscreen mode Exit fullscreen mode

You have successfully cloned the repository and the local repository is connected with the remote repository.

Now we can proceed with adding our files in our repository.

5. Staging and Unstaging Files

Once we have added our files to the local repository, it's time we add them to the remote repository as well.

To do so, we have to first mention that we are ready to send the updates in our local repository to the remote repository. For this, we stage our changes.

To stage the changes in your local repository, put the following commands in git bash:

A) For selective files:

git add <filename>
Enter fullscreen mode Exit fullscreen mode

B)For all the files:

git add .
Enter fullscreen mode Exit fullscreen mode

Now there may come situations when you want to remove some files from the staging area. In that situation we unstage the respective files by the following commands.

git reset HEAD <filename>
Enter fullscreen mode Exit fullscreen mode

or

git restore --stagged <filename>
Enter fullscreen mode Exit fullscreen mode

6. Commiting Files

Once we have stagged the files i.e. we have set the files we are ready to push to the remote repository, we commit them to save the changes in the local repository. This ensures that the changes to the file/folders have been recorded up to a certain extent/timeline. Make sure to add a meaningful message in the commit as it helps to review the code in the future instances.

To commit the changes:

git commit -m <commit_message>
Enter fullscreen mode Exit fullscreen mode

7. Selecting the branch

Now our changes have been committed. It is time for us to select our branch to which we will be pushing the changes in our repository.

In GitHub, the name of the default branch was changed from 'master' to 'main' recently.

To select the branch, we do:

git branch -M <branch_name>
Enter fullscreen mode Exit fullscreen mode

In default situations we put branch name as 'main'. Branch selection is required only in the first commit or in case we switch between branches.

8. Pushing the code

Now we are ready to push our changes into the remote repository. This action reflect all the changes that we have committed in the remote repository.

To push the work:

git push origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

By default, we put 'main' as our branch. In case we want to push the code to some other branch, then we mention that branch's name here.

9. Logs

We can view the logs of our commits on both the local repository and remote repository on their respective platforms. See the logs, we can determine which repository is ahead of the other.

A) To view the commit logs of the local repository:

git log
Enter fullscreen mode Exit fullscreen mode

B) To view the commit logs of the remote repository:

i) Open you web browser and navigate to your remote repository.
ii) Below the 'Code' button, there will be a label showing the number of commits made to the repository. Click on that label.
iii) You will be navigated to the page with list of commits made to the repository.

For advance concepts, checkout the Git Official Documentation.

Also there are some advance workflows which make working on Git extremely easy for big projects and team. Check them out here.

That's an end to this guide of Starting with Git on Windows. Have fun exploring more.

Top comments (0)