My experiments with git and github

What is git?
gitandgithub-image1A few years back I heard about this super awesome distributed version control tool that did not need me to be connected to the server all the time I was working on code (I was/am a TFVC/Azure Devops user). So not being connected to the server sounded really cool because that was not the case for me. I not only had to have Visual Studio open but also have the files checked-out before I could start working on them. So a version control system that kept track of the files as I made changes to them along with me not being connected to the server was awesome. And its free? What? So code stays on my laptop under version control… what if I lose my laptop or it crashes? How do I share my work with others? Do I lose all the work? Is there a centralized repo? Enter github.

What is github?
An online repository for your local git repository. Kind of like the server location of your local repository (as long as you’ve added them. More later in use cases).

How do the two tie up? Do I need both to be setup?
Both could be setup individually without depending on the other. However, once setup is done, it is a good idea to tie them up when you have to work on your code on your laptop and save it at some place other than your laptop.
There is a lot of good documentation around git and I refer to that from time to time.
https://git-scm.com/book/en/v2
For the purpose of this exercise, I am going to create a folder and use that as my sandbox. C:\MyTemporaryGitSandbox

UseCase: Setup a github profile
Please visit https://github.com/ to create your profile.

UseCase: Setup a new remote repository in github
(you need to have a github profile already created)
After creating a profile or after logging in, at the far top right corner you will see an option to create a repository

gitandgithub-image2

This is like creating a server location for your project with a unique name. Also notice that this repository gets created under your user id.
Setup is quite intuitive and easy to understand. Now let us install git on our local (laptop).

UseCase: Install and setup git on your laptop
https://git-scm.com/download/win …installation is quite simple and easy to follow.
Once installation is done, open a command prompt and type following command:

git --version


gitandgithub-image3
(-if you are familiar with vs code and prefer using the terminal (Ctrl+`) and type in same command)
This will list the version of git that is installed on your local.
After checking the version lets create your local git profile. You do that by the following commands:

git config --global user.name "FirstName LastName"

git config --global user.email youremailid


Once done, check your local configs by the command:

git config --list


This will list all the settings that git will be using for your local repository.
So up until this point you have a remote github repository created and you have a installed and configured git on your local. Now lets checkin some code into our local repository.
UseCase: How do I add/checkin code to my local git repo?
I created a folder inside the sandbox folder after navigating there, ran following command:

git init

gitandgithub-image4


Behind the scene, a new Git repository was created in FirstGitProject folder along with a .git hidden folder. There is a .config file that stores information related to this repository.
gitandgithub-image5
After initializing an empty git repository, I ran the next set of commands which pertain to creating a README.md file and then committing the changes into my local git repository.
These set of commands created a README.md file and checked that into my local git repository.
Commands:

git add README.md

git commit -m "first commit"


gitandgithub-image6
To check your commit history, type in the command:

git log --oneline


gitandgithub-image7
In this case, since there is only one commit, we are seeing that. Now we are going to upload our code from our local git repo to a remote repo and for that we need to first link them.

UseCase: Link your github repo with your local git repo
When we created a remote repository in github we landed on a page somewhat similar to:
gitandgithub-image8
To link our local empty repository with our remote repository run the command:

git remote add origin https://github.com/kunduso/TestProjects.git


gitandgithub-image9This updates the config file in our local git repo as shown below:
gitandgithub-image10
next we run the command:

git push -u origin master


gitandgithub-image11This adds more details to our config file in our local git repo:
gitandgithub-image12
With this change we have successfully pushed out local changes to remote github repository. You may check that in your remote repository.
Note: If you try to add a remote repository without having a local git repository setup this should throw an error:
gitandgithub-image13
This means that we need to have a local repository created before we can link that to our github remote repository. Go to  UseCase: How do I add/checkin code to my local git repo?

Reference:
https://git-scm.com/downloads
https://github.com/features
https://git-scm.com/docs/git-init
https://git-scm.com/docs/git-add
https://git-scm.com/docs/git-commit
https://git-scm.com/docs/git-log

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s