Get your way started towards Git & GitHub!
Tags: github, git, vscode, codereview
Published: 2022-04-19 10:51:58

In my previous article, I covered a brief introduction to Git and GitHub. [Have a look here if you haven't yet](https://dev.to/wardaliaqat01/git-github-best-version-control-and-source-code-hosting-platforms-respectively-1m8b):

My goal for this article is to cover how you can easily get started with Git & GitHub, how to install it, and some basic/IMP commands. So, let's start!

## Contents:

- [Setting up the environment (Git, VS code installation, GitHub account creation, SSH key generation etc.)](#environment)
- [Basic Commands](#commands)
- [Cloning GitHub Repo on your local machine](#Cloning)
- [Creating Repo locally and initializing on GitHub](#initializing)
- [Undoing in Git](#undoing)
- [Forking in GitHub](#forking)
- [Conclusion](#conc)

## Setting up the environment <a name="environment"></a>

- Code editor – I prefer VS Code

[You can download from here if you don’t have](https://code.visualstudio.com/download)
- Git installation on your system

[You can download from here if you don’t have](https://git-scm.com/downloads)
- GitHub account

[Create your GitHub account by following few simple steps] (https://github.com/)
- SSH keys for authentication

Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username and personal access token at each visit.

After installing Git on your system and Creating GitHub account, to connect to your GitHub account from your local machine via SSH you need to generating a new ssh key for the authentication. Follow the below documentation for detail overview on creating and adding SSH keys.

[Generating a new SSH key and adding it to the SSH agent]
(https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)

When you set up SSH, you will need to generate a new SSH key and add it to the ssh-agent. It will generate two keys, one public and one private. You must add the public SSH key to your account on GitHub before you use the key to authenticate.
Follow the below documentation for the process of [Adding public SSH key to your GitHub account](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account)

## Basic Commands <a name="commands"></a>

Now that everything is set up, let's look at some common Git commands:

- `git Clone`: Bring a repository that is hosted somewhere like github into a folder on your local machine
- `git add:` Track your files and changes in git
- `git commit:` Save your files in Git
- `git pull:` Download changes from remote repo to your local machine, the opposite to push
- `git add .` To track all unsaved files and changes for github
- `git commit -m "title" -m "description"`
- `git push:` Upload git commits to a remote repo, like github

## Cloning GitHub Repo on your local machine <a name="Cloning"></a>

> It's similar to downloading or cloning someone else's open source project and updating/modifying it or even working with your own online repository. 

- In Visual Studio code, open an empty folder and from the top of that folder open a terminal window
- Clone with SSH that we just set up

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ov3c9ovvj0r8m9wgypq.PNG)

Run the following command:

- `Git clone git@github.com:WardaLiaqat01/Test\-Repo.git` (Repo will be cloned with all files and folders)
- You can view all hidden files using `ls` command

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9fpjmgm2vft53xbnmbe9.PNG)

- Make some changes to the file and run `git status` command, it will show you all modified and untracked created files to be saved and commit. 
- `git add .` will save all new changes. 
- `git commit -m` “message you want to add with commit” -m “add description of commit” will save your changes locally. 
- `git push origin master` will add update your changes on your GitHub Repo

Due to the recent "Replacing master with main in GitHub" action, you may notice that there is a `refs/heads/main`. As a result, the following command may change from `git push origin HEAD:master` to `git push origin HEAD:main`

You can try `git push origin HEAD:master` as a more local-reference-independent solution. This explicitly states that you want to push the local ref HEAD to the remote ref master.

[Git push documentation for detailed overview](https://git-scm.com/docs/git-push#git-push-ltrefspecgt82308203)

- After successful push you can see your all code changes are live on GitHub. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ayc6emnpcx9djuzh8g8y.PNG)

 You can also see all the commits

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4wrnixdolweoa2ygv4j.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vi46ocsjxxgbym23tegt.PNG)

Here are all the changes, green means it was added and red means it was removed. It's quite handy to be able to revert back to the previous version in case you don't like the changes.

## Creating Repo locally and initializing on GitHub <a name="initializing"></a>

> Essentially, it is just as if you were creating your development project locally and then uploading it to Git

- Create any Repo/Project locally in VS code
- It won’t be a part of GitHub Repo as for now
- After adding files your project and updating them we need to initialize our Repo so Git can recognize it
- `git init` (To initialize local git Repo)
- After your Repo is initialized then you can follow all the steps that we performed above (including status, add, commit etc.)
- `git push origin master:` Here this command will through you an error

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2d7c69pku7oip3axdyv.PNG)

It’s because we didn’t clone this down from GitHub repository. We didn’t create this Repo on GitHub, we created it locally. So git is saying `I don’t have any idea where to push this` because it doesn’t connect to anything. So, we need to create that connection.
The easiest way is to do it from GitHub online interface

- Create an empty GitHub repository with name and description and as a public repo

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vt91b0dnssl8y2uzquv9.PNG)
Now let's add that Repo as a origin so, git can recognize it.
- `git remote add origin [repo SSH/clone link]`
We are using this to add a reference to remote repo on GitHub
- `git remote -v` will show all the remote repos 
- `git push -u origin HEAD:main` (-u for upstream, that means I can push simply by "git push" command in future)

## Undoing in Git <a name="undoing"></a>

Undoing can be quite helpful. In some cases, if you made some changes mistakenly and want to revert them, for example if you want to revert a commit, the *git reset* command can prove very helpful.

- `git reset :` To undo add/stage changes. 
- `git reset HEAD~1 :` To undo commit  (will undo the last/most recent commit)
- `git log :` If we want yo undo any previous commit then this command will help
- `git reset (hash of that commit) :` You will get this from logs but it won't remove data
- `git reset --hard (hash of that commit) :` It will remove the code as well

## Forking in GitHub <a name="forking"></a>

In a nutshell, forking means copying someone else's code into your own local repository to make updates and then create a pull request to the original repository after successful changes or create a pull request in your own local repository. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqs6d9uy5gr2bgw3jpuu.PNG)

## Conclusion <a name="conc"></a>
As a programmer, you'll find Git and GitHub quite useful. Thus, we learned how to get started using Git & GitHub with a few basic commands. I hope you have found this article helpful in some way.
**`If you have any comments, let me know.`**

**if you would like to explore some more handy Git commands then visit the below URL.**

[Git Commands](https://github.com/WardaLiaqat01/Git-Commands)

## Connect with me:
[LinkedIn](https://www.linkedin.com/in/warda-liaqat/)
[Instagram](instagram.com/warda_Liaqat01)
[Twitter](https://twitter.com/CreativityWarda)

-