How to use GIT 🤓
What is GIT?
Git is a source code version control system. This is much useful when working with team members, but also it is so much useful when working by oneself too. By using GIT, we can keep track of the changes that others made in the code. The language used by Git is “C” language.
Advantages of GIT:
🔹Fast and Can work offline.
🔹 Can be restored the previous versions.
🔹 It’s free and open-source.
🔹 It’s Lightweight.
🔹 Don't need any powerful hardware.
🔹 It saves time.
🔹 Security.
Let's understand the Version Control System!
This is also known as source control. This permits us to keep track of our work and supports us to explore easily. Version control system helps software developing teams to work much faster as well as smarter.
Benefits of Version Control System:
🔹 Easier when merging.
🔹 Easy Branching.
🔹 Helps in backup and restore.
🔹 Track changes.
🔹 Offline accessibility.
Types of the version control systems are:
- Centralized Version Control System
✮ Comparatively works slower.
✮ Focuses on tracking and backup files.
✮ Always needs an internet connection.
✮ No local repositories.
2. Distributed Version Control System
✮ Works comparatively faster. (Distributed version control works faster than the centralized version control)
✮ Have local repositories.
✮ Focuses on sharing the changes.
✮ Can work with a local repository without an internet connection.
What is GIT HUB?🕵️♂️
GIT HUB permits to host GIT projects on a remote server(As GIT HUB is a hosting service based on the cloud). But remember, GIT HUB is not GIT and GitHub is a hosting service. So we can say that GIT HUB is a company that provided hosting services.
Things to know when using GIT 🕵️♂️
Repository
It's like a project database that stores everything from files to commits, deletions, and so on. Repositories are not restricted to the user and can be copied as well as can be shared.
There are two types of Repositories as below ⇾
- Bare Repositories:
✶ Not user-friendly and it doesn't allow for modifications. So users can not make new versions for this particular repository.
2. Non-bare Repositories:
✶ Non-bare Repositories are user friendly and it gives permission for the modifications. So users can make new versions for this particular repository.
We can obtain Git Repository in the below 2 ways ⇾
✶By turning a local directory into a Git repository that is not under version control.
✶By cloning an existing Git repository from somewhere else.
The below diagram shows the Remote repository and Local repositories.
Branches
Helps to create another line of development. Git has a master branch(by default), and is the same as the trunk. Actually, a branch is generated to work on a new feature. Once the feature is finished, it is merged back with the master branch.
Below is an example to show the use of Git branches.
Assume you need to add on a new option for a website. Therefore you make a new branch and begin to work. You haven’t concluded your new feature yet, but suddenly you receive a request to make an urgent change that requires a live show about the site today. So You switch back to the master branch, do the change, and push it. Then you can switch back to your new feature branch and complete the work. When you’re done, you merge the new feature branch into the master branch, and both changes are retained.
To create a new branch, use the command “git branch new-branch”.
$ git branch new-branch
$ git branch
* master
new-branch
And to begin working on the newly created branch, run the command “git checkout new-branch”. So This will change your active branch into the newly created branch.
$ git checkout new-branch
Switched to branch ‘new-branch'
$ git branch
master
* new-branch
Below is the command to push the new branch to the remote repository.
git push -u <remote> <branch-name>
Below is the command used to delete the branch.
git branch -d <branch-name>
Git status
By using the “Git status” command we can see the information about the current using branch.
git status
Git clone
It makes an instance from the target repository. Not only that, it checks out the working copy but also mirrors the complete repository. Git Users get the privilege to perform many tasks/operations with this local repository.
$git clone <repository URL>
Fork
It makes a copy of the repository.
🟡 The difference between Clone and Fork is If we clone a repository it will copy to our local machine and whereas if we fork a repository it will make an original repository and this repository will keep in the GitHub account.
Fetch
Fetch is refers to getting the branches and tags from other repositories.
Pull
If a user has changed the code on a separate branch of a project and then he needs to add it to the master branch, that specific user can do a pull request. So here what actually happens is when pulling a request, it asks the repo maintainers to analyze the commits made, and then, if allowable, merge the changes upstream. A pull occurs when putting the changes to the master branch.
git pull <remote>
Tags
By using Tag, we can mark a commit stage as important. There are two types of tags as
- Light-weighted tag
- Annotated tag
Push
Push is used to upload local repository content to a remote repository and to keep the changes permanently in the Git repository.
Below is the command to push new branch to the remote repository.
git push -u <remote> <branch-name>
Master
Refers to the primary branch of Git Repository. All the commits should be in the master branch.
Head
HEAD is used to show the most latest commit of the repository in which we are working. Whenever we add a new commit, HEAD will get updated and then become that new commit. (The heads of the branches are stored in .git/refs/heads/ directory)
Merge
The command “Merge” makes it easy for you to take the data produced by the git branch and integrate them into one single branch.
Below is the way to initialize a new Git Repository ➾
What is GitHub, GitLab and such a BitBucket. Are they the same?😲
Actually, the truth is Git is a distributed version control whereas Github, Gitlab, and Bitbucket are providing hosting services for Git.
☀️ So Remember, Github, Gitlab, and Bitbucket is Not GIT.
References