What is Git | Git Bash Tutorial | GitHub
What is Git | Git Bash Tutorial | GitHub-In this post we will discuss about everything related to git and GitHub.
To understand what is Git? First, we need to know why Git is used? What is the necessity of developing Git?
What is Git?
Suppose you are writing a book along with your friend. You wrote the first chapter of the book and gave it to your friend for writing the 2nd chapter.
Meanwhile, your friend is writing the second chapter you made some changes to your 1st chapter. And you ask your friend to update the changes which you made to the old copy of 1st chapter which you have given him in the past.
But your friend refuses because of the overhead which you have given to him. And it’s obvious because your friend hs to go line by line and recap the changes you made in your 1st chapter.
Now when you receive the updated book included the 2nd chapter from your friend, you have to manually find and make the same changes which you have made in your 1st chapter copy. Since it the only way to keep your book updated.
This instance can happen many time in the future and is very time consuming and overkill for your productivity.
This situation is applicable to IT and development projects as well.
So we need something which can comb the changes made by two and merge them together.
Here comes the GIT for rescue.
Basically, Git tracks the changes, compares and merge them. All the conflict which happens due to the changes made by collaborators on the single project is handled by GIT.
Now you don’t need to worry about the changes made by you or your friends. Git keeps the history of the changes made to your project and merges them together.
In short, Git makes collaboration on projects more efficient and productive.
How to setup Git for Project?
First, you need Git installed on your computer to use it for your project.
Download Git and install it on your PC/Mac/Windows.
In Windows search for Git Bash and open it. Mac user can simply open your terminal.
Now navigate to the directory/folder where your project files reside.
Tip: To navigate easily to your project directory simply type cd then drag & drop the project file folder into terminal and press Enter.
Git | Git Bash Commands
What is commit?
commit is a term used to confirm the changes made by you in your project. Basically, when you commit you are telling git that “Hey buddy these are the changes I have made and you take care of that“. Now GIT uses this as breakpoint and saves it in its history.
Once you are in the main directory of your project files, Type git init and press Enter. This will initialize your project as a git repository.
In short: Using this command you can create local git repository.
Tip: when you see (master) at he end of the project directory path, It means that your project is successfully initialized i.e your project has become a repository.
git add -A
Now GIT will not monitor changes to your files unless you tell him to do so. Type git add -A and hit Enter. Now every change you make in your project will be tracked by git.
In Git terms all your files are now staged.
This command will show you all those files whose content has been changed since the last commit. Type this command and you will list of files (in green) to which you have made changes.
git checkout — .
Suppose you or some other person by mistake deleted or made irrelevant changes to your project files. How you will undo this?
Here GIT takes your back. Type git checkout — .(dot inclusive) in the terminal and hit Enter. All your files which you have staged using git add command will be restored in the state as it was in during the last commit.
Before knowing what other commands do? Lets first understand what is GitHub or Bitbucket?
What is GitHub?
As I told you GIT makes collaboration more easy and productive.
You can easily collaborate with your friend sitting beside you locally. But how will you collaborate with others who are a far distance away from you. You will need to host your project files somewhere over the internet. This is what GitHub does for you.
GitHub provides you the hosting platform along with various features so that you can host your project files and easily collaborate with other sitting at home.
A project in GitHub is called a repository(GitHub support GIT).
In short: GitHub allows you to create remote repository to collaborate with other people.
what is clone?
This is the option you might have seen on the repository page on the GitHub website. Clone here basically means downloading a per-initialized project/repository. Hence when you clone a repository to your computer from GitHub you are basically downloading the files as it was during the last commit made by owner/others.
You don’t need to run git init command for the cloned repository (i.e don’t need to initialize).
To download the files type git clone <url> and press Enter. You will get the URL from the repository page on GitHub website.
In short: Using this command you can download GitHub repository.
git pull origin master
If you made changes to the cloned repository on your PC or deleted some files by mistake. Then you can use git pull origin master command to restore files from the GitHub repository.
Note: This command will only work for the repository downloaded/cloned from the GitHub or any repository hosting sites.
Suppose you have cloned a repository on your PC and you made some changes to it. By using git status command you will be able to view your files to which you had made changes. Also, this command suggests you commit these changes so that git can store it as a break point.
So using git commit you will be able to commit changes you made. Also, you can attach a message to your commit as a short description of what changes you have made to the project/repository? using git commit -m “<message>”
git push origin master
When you commit your changes in the project then your project is ready for export to GitHub. By using git push origin master you will update the repository from where you have cloned your project with the changes you made.
In short: Using this command you will be able to upload files to GitHub from windows/Mac.
Because of this, the other people with whom you are collaborating on a single project will be able to view the changes you made to the project.
Note: Your changes will only reflect on GitHub unless the owner of the project/repository allows your changes to be pushed.
git remote -v tells you the remote repository address where you can push or pull project files over the web (i.e git push/pull origin master will operate on which repository).
Suppose you have cloned my project on your local disk using my repository URL. Now after making changes and commit when you push your changes it will reflect on my repository (if I allow it :P). It is because origin here is my repository.
Also, after cloning my repository you can change the origin to your repository address using git remote set-url origin<your-repository-URL>
Now git remove -v will point to your repository address. Now your every pull/push will work on your repository.
But when you create a repository using git init command (i.e Your personal project or you haven’t cloned from any remote repository), your local repository is not connected with any remote/web repository address.
You can check using git remote -v. If it doesn’t show any address like in the picture below then your local repository is unlinked with the remote repository.
To connect your newly created repository with remote origin use command git remote add origin <your-repository-URL/Address>
These are the major git commands and GitHub tutorial, which are enough for using git in your project. Though in start you will find little bit difficulty but be assured that using git from command line will be more fruitful and efficient to you in the future. Hope you learned What is Git ,Git Bash and GitHub and will be able to use it efficiently.
If you have any problem then comment below.