Skip to main content

Git : Todo and not Todo : Part-2

Hello there!

This is the second part to introduction to git. If you have not read the first part then please first go through that first, as proceeding along here without that introductory knowledge will not be really helpful.Here is a link

TL;DR

All the git commands that we will be talking about, now and later-:

git init
git clone
git add
git commit
git status
git remote
git push
git pull
git fetch
git branch
git reset


So, lately we learnt about Version Control Systems also abbreviated as VCS and got to know about git and how it is completely different from github and other VCS hosting websites.

In this part we will be looking on the more practical part of using this wonderful tool and also why we should learn it very first in our development career. Later, I will also help you understand that git is just a small part of the entire development career that you aspire to make and why it should not be considered just more than a medium to share your code with others.

There are so many powerful operations that git provides us and this is what it makes it very useful. I will explain the basic operations here so that it helps you getting started easily. The more advanced operations can be learnt by one on self exploration and usage.

For any code that is written, we assume some basic operations that it will go through. These include-:(Please note that for the following examples we have taken Github as our host)

0. Initialising your local
1. pushing your code to the host
2. Pulling others or your code from the host
3. Restructuring your local folders


Now we will look individually on these broadly defined segments:-



0. Initialising your local-:


When you are on windows you need to set up git manually as it is not already present. You can get going from here. For linux users you dont need to worry as you are already provided with git. Just open a terminal and check its presence with git -v. (I will seriously advice you to change to macOS or linux if you are using windows as this will certainly help you very very much in the long run. Linux and mac users,stay happy!)

After you have git installed make your personal configuations. This includes stating a username and a email. You can do this by

git config --global user.name "Your favourite username"

git config --global user.email "Your most used email"

Now you have a locally configured git and now you are all set to initialize your repository. You can do this by


git init

If you get this  short message- Initialized empty Git repository in /your/chosen/folder/path

You are all done!
Congratulations to have set up your empty git repo!

But dont you think it is really very EMPTY! We fill in code and move onto the next section.


1. Pushing your code to the host-:


Now since we have our repo initialized we can start making our project in this folder. Add files and more files and more files in this folder, what so ever your app requires. Use npm, pip, gems or any other package managers of node, python, ruby and others respectively to give shape to your dream application but feel secured because you have got everything ' Tracked '.

Yes, indeed. Every file you added, every line you wrote , every bug you deleted is being tracked by git. And you can see this by running this in the terminal

git status

It shows you every file you have in your project and differentiates between the ones already tracked and the ones still not tracked.

After this, when you feel ready, you decide to add all the files to the 'Staging area' . This is a virtual platform where all the files you have modified or added or deleted are tracked by git and then are available to you to ' Commit'. So first let us add these files to the staging area with

git add <file_name> 

also you can use

git add .        <- (notice the dot here)

to add every untracked file in your folder.

After you have staged all these files you can check this by once again by, Yes! you guessed it right. Using

git status

And now, Commit! Commiting is a very powerful feature that straight away saves versions of your code. You can browse through your commit history to get back to any version of your code any number of times. All this build up that we did has to eventually end with this. And we do this with

git commit -m ' Your commit message'

So, congratulations. You saved your first version of code. Now feel secured because your code is well preserved in the git timeline and is available to you at any time in future.

After this, we have to make a repository where we will be pushing our code. A repository is similar to a folder but has git initialized in it and is hosted on a VCS hosting service ,Ex- Github, Gitlab etc. So make your github account if you already haven't. Find a '+' glyph on the navbar. Click that and you will be given a option to make a new repository. 

When you click on this, you will be redirected to a new page where you can enter your repo name and choose from other well explained options for your repository. Then you 'create repository' and you are redirected to a page which has details about your repository.

You will also find a url of your repository. This url will be used to create the remote on which you will be pushing your changes to github. To add this url as your remote, use

git remote add  <remote_name>  <url>

In this way you are all set to push your 'initial commit' to github. For this use

git push <remote_name> <branch_name>

by default, your branch is master.

Kudos! We have pushed our code to the repo now. You can check this by refreshing/visiting your repo page. We have our code hosted and from here anyone can see it. This is the best part of open source. A community that is willingly ready to build new things on top of already built things and in the process learn new and more challanging stuff. This is idea that makes open source so powerful.
I guess, I will not be able to write much about open source as it is itself one complete article and so will leave it for some other day but the takeaway is that you need to start it immidiately. And github proves to be the best place.

The remaining two parts will be discussed in the next article. Add more and more code to your repository and if you face any problem, feel free to share with us in the comment section. I will try to help you out, and you may get many other's problems solved too. In the next part we will see how you can pull your code or any other repositories code to your local and also how you can structure your work well with the help of branches.

Till then, Keep Coding!

Thank you!

Comments

  1. Can you provide best link of how to install git for windows ?

    ReplyDelete
    Replies
    1. for installing git on any platform
      https://git-scm.com/downloads

      Delete

Post a Comment

Popular posts from this blog

Competitive Programming

Competitive programming is a mind sport usually held over the Internet or a local network, involving participants trying to program according to provided specifications. Contestants are referred to as sport programmers. Competitive Programming mainly focuses on our ability to find efficient solutions to well-defined problems. Usually, a question arises - Why Competitive Programming? Many of the top tech companies like Google, Facebook hire people through a test which includes competitive programming. Also, participation in the ACM International Collegiate Programming Contest (ACM-ICPC) is a great opportunity to be seen by Apple, Facebook, Google, IBM and many other authorities in the IT sphere. These major companies are constantly keeping track of the participants to find talented employees. Having experience in competitive programming shows employers you can work in a team, solve complicated problems, work in stressful situations, manage time and deadl...

A beginners guide to NumPy

The previous post took you through the basics of machine learning using python with scikit-learn and tensorflow, this post will give you a brief introduction to Numpy. NumPy is a python package which stands for "Numeric Python". It enables you to do mathematical computations easily and effectively hence making coding simpler. http://www.numpy.org/ Installing via pip: After having python3 and pip installed, installing packages can be done using the following command on windows cmd. >>> python - m pip install -- user numpy scipy matplotlib ipython jupyter pandas --user   makes pip install packages in your home directory instead, which doesn't require any special privileges. Import Numpy: >>> import numpy as np Now we use np as a reference to numpy in the code. Numpy arrays: Create a numpy array of type np.ndarray: >>> A = np.array([[1,2,3],[4,5,6],[7,8,9]]) Create a numpy array containing ones/...

Git : To do and Not To do: Part- 1

Git is a version control system that helps to maintain versions of your work over time. The above line may seem a bit overwhelming to read at first, as there are words which most of us not really understand as of now. Just read along and I will help you in getting some idea of why git is a resource every developer has to be sound with and why it is a integral part of every developer's everyday life. A version control system :- We all have heard the saying that Rome was not built in a day and believe me, if it is not your college assignments , no great app was ever built in a day or at least without using a VCS( version control system). Every app has multiple sections. Every section require some skills that is used to develop it. A skill may come under a set of skill that a contributor has.  When we work alone or in a group with other developers, making any single project, it tends to happen that people take up sections that they are good at, as they believe that they can contri...