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
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.
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!
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!
Can you provide best link of how to install git for windows ?
ReplyDeletefor installing git on any platform
Deletehttps://git-scm.com/downloads