Git is the most used tool for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development.
When you are working in a team. you have different task every person is responsible for different task but everyone's work is dependent on each others work.
You have to send files to each-other when ever you changed something you have to send it back to back and maintaining those files becomes a tedious task.
Here comes the git. Git tracks changes in your files and save different version of files. if you make any crap in your current version you can easily go back to your previous stable version.
if you are working in a team and something goes wrong. It will help you to identity who made the change and where he/she made the change and when they made a change
Before we start working on git there are some prerequisites:
- [x] Install Git:
- [x] Github Account:
- If you don't already have one, you can make one
Before we start working with there are some things you need to know.
Git repository aka repo is like a normal folder but with some Super powers. It saves every change you make in it.
If your file is in current working directory(folder). it can be in three states:
- Working Directory: Working directory is the area where you are currently working. where your files resides. it is also called Working Tree or Untracked Area. every change you make here is not tracked by git.
Staging Area: Staging area is when git starts tracking and saving changes occur in file. after saving changes when you make another change in the file git will know where you made a change. with
git add filenameyou can move file from working directory(Untracked Stage) to Staging Area(Tracked Stage).
Local Repo: local repository is the place where you save your changes before sending them on remote repository. you can save changes from staging area with
git commit -m "message" filename)".
- Remote Repo: Remote repository is a place where you Push your code or Pull to update the local version of a repository from a remote repository. its usually Github or any other hosting service for version control.
Before you can use any Git super powers first you need a repository(Super Folder). Either you can connect your existing project folder to your git repository. or you can clone one from github.
First create a repository on github with new button from repository section.
Now add required details Repository name, Description and so on. then create a repository.
After creating a repository copy HTTP or SSH Address.
Now clone your repository into your local machine.
git clone address_of_your_repository
If you want to connect your existing project to your git repository you can use this.
First initialize a git repository in that folder.
Now connect your existing repository with remote repository.
git remote add remote_name address_of_your_repository
At remote_name you can specify any name you want to specify for your remote repository. usually it's origin.
To add changes from your working directory to staging area.
git add /path/to/file
You can add all changes at one(if you are lazy like me).
git add .
Now commit(save) the changes you added to your staging area:
git commit -m "Commit Message"
The message in " " is given so that the other users can read the message and see what changes you made.
You can check the status of the files in working directory and the staging area. which files are in staging area and which files or not tracked yet.
git status you can also check how many commits you have made in your git local repository(all the commits made to a repository).
To upload your local repository content to a remote repository(Github). we use.
git push remote_name branch_name
remote_name: usually its origin but in start if you initialize your existing project to git repository you may have added a remote_ name in
git remote add remote_name address_of_your_repository.
branch_name: its a branch where you want to submit your code. usually its master or main. you can check your branches with
git branchit will list all the branches. the one with * before name is your your current branch.
Note: git maintain history of your project when your local repo history doesn't match with remote repo you cannot push your code. It may happen because of some files changes made by you/teammate in remote repository which are not present in local repository.
So when you are working in team/collaborative project you may want to pull changes in to your local working directory.
git pull remote_name branch_name
git pull update the local version of a repository from a remote repository.
Now Push your changes to remote repository.
In this article we have learned what git is, why we need git, some basic git terminologies and some basic git commands.
Stay tuned for the next article on how to work collaboratively on git in a team.