What is Git ?
Git is a distributed version control system (VCS) that helps developers track their codebases, collaborate with others and manage multiple versions of a project.
It was invented and developed by Linus Torvalds, also known for inventing the Linux kernel, in 2005.
Git Distributed means, 2 or more developers can have access on the codebase easily and operate changes on it with ease. The system knowing exactly who did what.
Git Version control means, Versions of the codebase are stored lightly and accessible with a simple command. By then, no matter what happens to the project, we can revert to a specific version that worked fine and continue the development with it. (we'll go through this in the advanced level).
For better summarization, we have organized the understanding at 3 levels; Basic, Intermediate and Advanced. In this post we make emphasis on the basics.
Prerequisite
- Download Git on your machine. (https://git-scm.com/downloads)
After installing, open a terminal and type
git --version
The version you installed should be displayed, if not, comment in this post, and we will figure out. π
- Configure Git with your credentials (Name and email)
Name:
git config --global user.name "Your name here"
Email:
git config --global user.email "Your email"
That's it β, you're ready to use Git, and therefore follow this tutorial.
1. Git understanding
View Git system as this...
To use git in the project, and have this virtual representation set, you should initialize git.
git init
Workspace
It is where you edit and manage your source code. Your code editor or Integrated Development Environment (IDE).
Here you do your coding as you know so well π, in JS, Python, Go...
Stagging area
It is where the files supposed to be in a commit (version of the code) are stored.
When you decide to validate a version, you can store the files here. View it like this
This id done with the command
git add <file name>
or add all files newly created or modified
git add .
When you have your code in the stagging area, you can either remove a file, or validate it to be saved as a version (commit).
To remove a file from stagging area, hit
git restore <file name>
or
git restore .
Local repository
Where all the versions of the source code are stored.
To create a version, save the code in the stagging area to the local repository (do a commit).
To do a commit, run
git commit -m "Commit message"
You can view all the commits (versions) by using the command
git log
or to have a better view on the versions, you can run
git log --oneline
This can be helpful for advanced practices.
2. GitHub
GitHub is a Git hosting platform (remote repository). This helps other developers in the team to access the codebase and apply changes on it.
There exist other popular hosting platforms, like GitLab and BitBucket. We put emphasis on GitHub as it is the most popular Source
You need to create your account in order to create remote repositories.
A remote repository is an online space where you store your local repository. This will permit the entire team to have access to the source code.
To send the code commited from your local repository to remote, you must first create the repository on GitHub. (Note that you have to login first)
After creating, you should fill the form with the repository information
Usually name, description, and visibility is okay.
Then scroll and validate
You will be presented steps to follow inorder to connect your local and remote repository.
So depending on your situation, choose your path π
If you followed the tutorial, you will follow the second path in your local repository, that is running these commands...
git remote add origin "Your remote url here"
git branch -M main
git push -u origin main
If you refresh your page, you will see your local files on GitHub π.
Let's break down these commands;
git remote add origin "Your remote url here"
: This sets the connection between your local and remote repository. With that you can send your code to remote.git branch -M main
: This renames your local branch name to main.
What a branch ?
It is the path through which your code goes, for it to reach online. The default branch is "main", but can be "master" if you have an old version of git or have specified it at installation.
In this basics, we will consider the branch being main.
You can view it by running
git branch
We'll dive deeper into branching as we will be talking of Git and GitHub at an intermediate level. For know, let's work with the default branch.
-
git push -u origin main
: This pushes your code to remote as in the figure below.
The -u
flag sets the branch upstream. This means, the next time you will have to push code to remote repository, you will just type git push
.
The final part of this blog post is, getting code from remote repository to local.
To do that, run
git pull origin main
Note that if you have used the -u
flag above, you can archieve this by just typing git pull
.
Final though
You can't dev in this era without using Git, practice and get confortable using it, as it is a prerequisite for all devs nowadays. Visit the official documentation for more understandings.
I hope you learned from the post π.
If it was basic for you, don't worry, the intermediate level will be done next, and finally the advanced.
Thanks to @javascriptmastery, this article follows a tutorial done by them.
Thanks for reading, see you. π€
Top comments (0)