DEV Community

Cover image for Get your way started towards Git & GitHub!
Warda Liaqat
Warda Liaqat

Posted on

Get your way started towards Git & GitHub!

In my previous article, I covered a brief introduction to Git and GitHub. Have a look here if you haven't yet:

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!


Setting up the environment

  • Code editor – I prefer VS Code

You can download from here if you don’t have

  • Git installation on your system

You can download from here if you don’t have

  • GitHub account

Create your GitHub account by following few simple steps

  • 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

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

Basic Commands

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

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

Run the following command:

  • Git clone\-Repo.git (Repo will be cloned with all files and folders)
  • You can view all hidden files using ls command

Image description

  • 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

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

Image description

You can also see all the commits

Image description

Image description

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

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

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
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

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

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


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

Connect with me:


Discussion (0)