First of all, let's see what is a version Control System.
Version Control System?
Version control, also known as source control, is the practice of tracking and managing changes to software code.
What is Git?
Git is software for distributed version control, tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. That's why Git is also known as Source Code Management(SCM) tool. In other words, we can also say, Git, is a tool or software which is used to manage our source code.
Git is used to track changes in the source code, enabling multiple developers to work together on non-linear development.
With Git, you can easily roll back to older code snapshots(commits) or develop new features without breaking production code.
Git allows a team of people to work together, all using the same files or on a single particular project.
What is GitHub?
GitHub is a Cloud Git Repository & Service Provider. A repository contains all of your project's files and each file's revision history. In Others words, we can also say that a repository is a place where you can host your project files and source code remotely in the cloud Databases.
Note: Git and GitHub both are not the same!
Git is a software or tool used for tracking the changes in our files, whereas GitHub is a Website or Platform which allows us to Host Our Project files and Source Code on Cloud Databases, known as Repositories.
There are many other Platforms like GitLab, BitBucket, Taravault etc. which allow us to host our Project files remotely on Cloud. You can know more about them here
Not only repositories, GitHub also offers us various features for doing Collaborative work remotely.
What is needed for Git and GitHub?
When you get stored all of your project files and Source Code in your local system, there might be some chances to get your system crash. In such situations, you will lose all of your Project files and Source Code.
These are the scenario where Git and GitHub come into action. You can use GitHub to Save your all Project files and Source code remotely on Cloud, then you do not need to get worried about your project work. If your system crashes or gets damaged, your files will be safely stored on Cloud. The Cloud Database here is GitHub Repositories.
Conclusion: By using Git, we can keep track of our file changes and By using GitHub, we can store our files remotely on the Cloud.
Let's now discuss how to use Git and GitHub :
First things first, We should have to install Git into our Operating System.
Go to Git Website and install Git.
After installation of Git, open your Terminal and run git
command to make sure you have properly installed Git on your computer or not? After running the git
command if you see some commands are prompted then, you would be good to go further steps.
If you get any trouble while installing Git, go to google and help from there.
After installing git successfully in our system, open your Terminal or Command Prompt and change the directory to your project directory which you want to Push your code to a Repository
Initialize a Git Repository :
We can use
git init
command to initialize git.
The git init command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository. Most other Git commands are not available outside of an initialized repository, so this is usually the first command you'll run in a new project.
Adding Files to Repository :
After Initializing a repository we should need to add files to the repository,
git add <fileName>
The command allows us to add a single file to the repository.
or
git add .
The command allows us to add all files to the repository.
\*Committing(Saving) Code Snapshots(Changes)\*:
After adding files to the repository we should need to commit them to the repository,
By using the
git commit -m "Commit Message"
commit is nothing but, just saving your changes to the repository. We should also pass a message relative to the file changes.
Git Branch :
A branch is a version of your repository, or in other words, an independent line of development. A repository can contain multiple branches, which means there are multiple versions of the repository.
Lets' take an Example of App Development.
After developing an app and releasing it to production. After a certain period, we may want to add a bug fix or new feature to the app.
We usually do this by changing the version of the app. If we update the code written Initially, we may encounter some troubles if the source code gets lost.
This is where the git branch feature comes in handy.
We can create a new branch of the repository without making changes to the main branch or first branch.
The command for creating a new branch is :
git branch <branchName>
By default, we will have a main or master branch.
Current Branch :
To know in which branch we are currently in, we can use the command.git branch
The above command also displays all the branches available in your repository.
Status of repository :
If we want to know the status of our repository we can use the command
git status
Git Log :
The git log command displays all of the commits in a repository's history like the time of commit, author of commit and branch of that commit etc.
git log
Git Checkout :
The git checkout command allows us to move on to particular commits or branches.
git checkout <Commit SHA_value>
SHA means Secure Hash Algorithm, For each commit we make, git will automatically create a Unique id which can be used to track changes in that commit or code snapshot.
git checkout <branchName>
The command allows us to jump between the branches. Indeed, it will take you to that branch.
Make a Connection to the Local and Remote Repositories :
Before going forward, we should make a connection between local and repositories.
To tell git to push our project files from a local repository to a remote repository we should need to make the connection between them.
We can do this by the following command,
git remote add origin https://github.com/user_name/repository_name.git
Here origin is the nickname of the remote repository, we can use any name of our choice.
After completing all the initial steps like initializing, adding files, committing the changes to a repository and making a connection to local and remote repositories.
The next step we should have to do is, Create a GitHub Account.
To do that,
Go to GitHub and create a GitHub account by providing your email and setting up your UserName and Password.
Once that is finished, it's time to create your new repository on GitHub. You can create a new repository with the plus(+) icon which is at the right top of the website.
Give a name to the repository, there you can see options like public and private, you can select your choice, public: means your repository will appear to all the public, which means all the other users on GitHub can be able to see that repository, private: means only you the owner or creator of that repository can be able to see that.
Now the final step is to push the source code onto the remote repository which you have created on GitHub
Push Code onto a Remote Repository :
git push -u origin <branchName>
To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.
or else you can use
git push
command, if you are pushing code to the 2nd or 3rd time etc.
Conclusion :
I hope this article gives you a basic idea of Git and GitHub.
If you want to learn more about git and GitHub,