VCS - GIT & GITHUB

Introduction

Git is a distributed version-control system for tracking changes in any set of files, originally designed for coordinating work among programmers cooperating on source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows

GitHub is where over 56 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and features, power your CI/CD and DevOps workflows, and secure code before you commit it.

BitBucket, GitLab are also similar to GitHub but GitHub is more popular.

In simple words, GitHub is like a storage bucket for your code(projects) whereas Git is used as an interactive tool between your local pc/folders and GitHub.

Git & GitHub together form the Version Control System.

Uses

  • Collaboration
  • Open source contribution
  • Storing versions properly
  • Restoring previous versions
  • Understanding what happened
  • Code backup

Git Workflow

A GIT Project consists of three major sections:

  1. Working directory: It is the directory where you add, delete and edit the files i.e. a folder on your local machine where you code your project.
  2. Staging area: The changes are then staged(indexed) in the staging area.
  3. Git directory: After you commit your changes, the snapshot of the changes made will be saved into the git directory.

Setup

For setting up Git in your pc, head over to this link, download and install the software.

For setting up your GitHub, head over to this link and sign up with new account.

10 Basic Git Commands

1. git init

This command simply initializes a working directory for your project as a git repository. Make sure you are in the project directory and type the following command to initialize a new repository.
Ex: git init

2. git add [ filename ]

This command puts the desired file to the staging area for operations to be performed on it.
Ex: git add index.html
To add all the files to staging area: git add .

3. git commit [ options ]

This command records the changes made to the git repository by saving a log message together with a commit id.
Ex: git commit -m "First commit"

4. git status

This command is used to see the status of working tree. It shows the list of unstaged files and list of staged files that needs to be committed to GitHub. If there is nothing to be committed then it will simply show that the branch is clean.
Ex: git status

5. git remote add origin [ location ]

First of all, you should create a new repository in your GitHub account and give it a name of your choice. Head over to this link to create a new repo.
This commands adds the destination repo that your local project repository(directory) must target to i.e. it connects local to remote repository.
Ex: git remote add origin https://github.com/username/repoName.git

6. git branch [ branch name ]

It creates a new branch of your repo. You can create multiple branches for your repo also.
Ex: git branch branch-1
By default you will be using the branch 'main' or 'master'.

7. git push origin [ branch name ]

This command sends all your local commits to the remote repo i.e. the repo which you have newly created on GitHub. Hence in this way, GitHub will act as a storage bucket.
Ex: git push origin master
(or) git push origin branch-1

8. git clone [ location ]

This command creates a local working copy of an existing remote repository. It simply clones to copy and downloads the repository to your computer.
Ex: git clone https://github.com/facebook/react

9. git rm [ filename ]

If by mistake, you had pushed a file which was not suppose to be then this command is useful. We can delete the file from the project(on your remote repo) and stage the removal for commit.
Ex: git rm secretkey.js

10. git revert [ commit id ]

This command is used to revert the changes back to any of the previous version of your project. Be careful with this command and never ever accidently hit this because it might cause your app to crash even if it was working fine previously.
Ex: git revert 6eb8701365c820330a6e4615cefb4ff4b75c390c

That's it from this blog post. If you liked it then do share this blog with your friends or people who wanna get into programming world. Thank You!

Copyright © NStF Blogs 2021