Git for Beginners
What is Git ?
Git is basically a Version Control System used to track the changes in the source code during software development.
Thus, Git helps developers identify what changes were made previously in the code.
Also, Git provides a feature for developers to revert (undo) the changes and go back to a previous version of their code.
In short
Git is a Version Control System
Git tracks the changes in the source code during software development
Tracking helps developers see what changes were made
Git also helps to revert the changes
Why Git is used ?
As we discussed earlier, Git is used for tracking changes in files. Let's clarify in more depth why and where we use Git.
Git is used to track all changes made in a file.
In a team, if we know what changes other team members have made in a source file, it makes collaboration much easier as everyone can track each other's work.
Your code history will be saved as every change gets tracked.
We can try new features, and if any bug occurs, we can go back to a previous version of our software.
In short
Making Collaboration easy
To get back to previous version if any bug or issue occurred in current version
To track our history
Git Basic and core Terminology
Now we have understood what is git and why it is used during software development, lets study Git’s some basic and terminologies
Repository
A repository is a folder created by Git where all your code files (project files) will be present. Git will track all your changes in this repository.
Commit
A commit is used to save changes in Git. Each commit includes a message that explains the changes and appears in the Git log.
Branch
When a Git repository is created, the code is structured in the Main Branch by default. A branch represents an independent line of development. A developer can create a different branch for their own testing or to add features so it won't affect the main branch code.
Head
HEAD is a pointer that refers to the current commit in the repository. It represents the latest commit on the branch you are currently working on.
Common Git Commands
Now let us understand how do we use git in our software development.
Initialize
So to start working with Git we need a repository ( as we already discussed Git works in repository )
thus to make repository we use the following command:
git init
This initialize a Repository in our working directory !
Stage
After working on a project you want to add your files at GitHub in your repository so first add your project files in staging
To add ALL files from your directory to staging we use following command:
git add .
To add some specific files from your directory to staging we use following command:
git add <file_name>
Thus the staging area prepares files to be included in the next commit.
Status
After adding your files to staging area you can actually check status to make sure you add correct files in staging area
To do that we use following command:
git status
Verification is must !
Commit
After adding files to the staging area, the next step is to save those changes in the local repository.
To do that we use following command:
git commit -m “ your_message “
Adding message can tell others what changes had you made .
Push
To upload local commits to a remote repository like GitHub we use following command:
git push <remote_name> <branch_name>
You need to mention remote name ( usually its origin ) and in which branch
You have successfully made a GitHub Repository
History
As we have discussed earlier we can track the history of changes made in our project files
this can be done by following command:
git log
It will show all Commit history made in your Repository
Undo
To go back to your previous version of software or previous codes we can do with help of Git
It can be done by following command:
git revert <commit_id>
Very helpful in software development
Reset
To permanently delete previous changes made we use it
It can be done by following command:
git reset --hard <commit_id>
The deleted data cannot be recovered