Git and GitHub

Git and GitHub

What is Version Control?

Version Control is a system that records changes to a file or set of files over time so that you can recall a particular version later. Ideally, we can place any file in the version control system.

Why Version Control?

A Version Control system allows you to revert the files or the entire project to the previous state, review changes made over time, and see who last modified the file which caused an issue and when. Version Control Systems also recover lost files easily.

What is Git?

Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people. Git is a Distributed Version Control System. So Git doesn't rely on the central server to store all the versions of a project's files. Instead, every user clones a copy of the repository (a collection of files) and has the full history of the project on their hard drive. This clone has all of the metadata of the original while the original itself is stored on a self-hosted server or a third-party hosting service like GitHub.

Git helps you track the changes you make to the code. It is the history tab of your code editor. If at any point while coding you hit a fatal error and don’t know what’s causing it you can always revert to the stable state. So it is very helpful for debugging. Or you can simply see what changes you made to your code over time.

Git also helps you synchronize the code between multiple people. So imagine you and your friend are collaborating on a project. You both are working on the same project files. Now Git takes those changes you and your friend made independently and merges them into a single "Master" repository. So by using Git you can ensure you both are working on the most recent version of the repository. So you don’t have to worry about mailing your files to each other and working with a ridiculous number of copies of the original file.

Repository:

A repository is a collection of source code.

You typically obtain a Git repository in one of two ways:

  1. You can take a local directory that is currently not under version control, and turn it into a Git repository, or

  2. You can clone an existing Git repository from elsewhere.

In either case, you end up with a Git repository on your local machine, ready for work.

Initializing a Repository in an Existing Directory

If you have a project directory that is currently not under version control and you want to start controlling it with Git, you first need to go to that project’s directory. If you’ve never done this, it looks a little different depending on which system you’re running:

git init

Fundamentals of Git Workflow:

There are four fundamentals of Git Workflow- Working Directory, Staging area, Local Repository and Remote Repository.

1*iL2J8k4ygQlg3xriKGimbQ

If you consider a file in your working directory, there can be three possible states:

  1. Staged: This means the files with updated changes are marked to be committed to the local repository but not yet committed.

  2. Modified: This means the updated changes to the file are not yet stored in the local repository.

  3. Committed: This means the changes made to the files are saved to the local repository.

Git Commands:

  1. git add -> git add is a command used to add a file to the staging area that is in the working directory.

  2. git commit -> git commit adds all the files from the staging area to the local repository.

  3. git push -> git push adds all the committed files of the local repository to the remote repository.

  4. git fetch -> git fetch is a command used to get all the files from the remote repository to the local repository but not the working directory.

  5. git merge -> git merge is a command used to get the files from the local repository to the working directory.

  6. git pull -> git pull is a command that brings all the files from the remote repository directly to the working directory. Equivalent to git fetch + git merge.

Getting started with GitHub:

  1. Get a GitHub account created.

  2. Make sure Git is installed in your system. If you are on Mac, Git is preinstalled. You can verify the status of installation with the below command.

git --version
  1. Add your git username, email in the global git config.

    git config --global user.name "abc"
    git config --global user.email "abc@gmail.com"
    git config --global --list
    
  2. Initialize the git.

    git init
    
  3. Edit the files you want.

  4. Use the below command to add the files for commit to the staging area.

git add .
# Adds all the files in the local repository and stages them for commit

OR if you want to add a specific file

$ git add README.md 
# To add a specific file
  1. Commit the files with appropriate message
git commit -m "Initial Commit"
  1. Add the remote git repository URL to the origin of the local git
git remote add origin remote_repository_url
  1. Push the commit to the remote repository.

    git push -u origin main
    
  2. Cloning an Existing Repository - If you want to get a copy of an existing Git repository — for example, a project you’d like to contribute to — the command you need is git clone

    git clone https://github.com/libgit2/libgit2
    

    Pull Requests:

    Pull requests let you tell others about changes you've pushed to a branch in a repository on GitHub. Once a pull request is opened, you can discuss and review the potential changes with collaborators and add follow-up commits before your changes are merged into the base branch.

    Create a pull request to propose and collaborate on changes to a repository. These changes are proposed in a branch, which ensures that the default branch only contains finished and approved work.

    We will learn more about Git in the next article.

Reference: Git