Git And GitHub Basics

Git And GitHub Basics

What is Git?

It is a version control system tool. Git has the functionality, performance, security and flexibility that most teams and individual developers need.

Learn more about Git here.

Version control, also known as source control, is the practice of tracking and managing changes to software code. Version control systems are software tools that help software teams manage changes to source code over time.

Suppose your team is working on a project and you wrote some code for the front page of the project's website. After some time before you knew it, somebody made some changes to the code of the page. You thought of asking who did it so that you can revert it back but before you could do it some other colleague of yours started working on it and also made some changes to the code. Now you don’t know who made what change and when?

If only there were a record of changes done so that you could review and spot the problem.

Here Git comes to your rescue it keeps the records of all the changes done to the code of your repository and a version of them so you could revert back any time you want.

Now that you understand what Git is let’s get you setup,

Download Git from this link.

For Windows 10 and above you could type the following in Command Prompt;

winget install --id Git.Git -e --source winget

Git also is accessible on Mac and Linux at link. Just follow the instructions given at the site to install it.

Some Common Terminology

  1. Branch: It is a new/separate version(copy) of the main repository. You can make changes to it but it will not be reflected in the main repository unless you push those changes to it.

  2. Untracked state: It is the state of the file in which it exists in the repository but it is not being tracked by git for changes.

  3. Staged state: It is the state at which we have added the file to be tracked but it has not yet started. A file has to be staged first to start tracking it. Tracking starts when the staged file is committed at least once**.**

  4. Tracked state: When a file is being tacked and its changes are being recorded. After committing the file reaches the Unmodified state.

  5. Modified state: Git has traced changes to the file but the changes have not been committed(saved) yet.

Reference: https://twitter.com/tjelvar_olsson/status/712983590674505729

Some Common Git Commands

  1. git init : The git init command creates a new Git repository. It can be used to create a repository for an already existing project or to create a new empty repository.

    You can do

    git init

    in your project folder, Or

    git init <directory>

    Create an empty Git repository in the specified directory.

  2. git clone: It creates a copy/clone of the remote repository

    Syntax - git clone <url>

    <url> here refers to url/link of the repository you want to clone.

  3. git add: used to stage a file to start tracking it. Only needed to do it once for a file.

    Syntax - git add <file_name>

  4. git commit: keeps a snapshot of the code. On the first commit the file starts getting tracked.

    A commit command tells that the current version of the file is a safe version and it should not be changed unless asked.

    Syntax: git commit -m "your_message"

    The message is to explain the changes you have done to easily identify them later.

    git commit -m "deleted file"- deletes a file from git repository.

  5. git restore: to restore the file to the last committed state.

    Syntax: git restore <file_name>

  6. git retore --staged: to move a file from staged to untracked state.

    Syntax: git restore --staged <file_name>

  7. git revert: to revert a specific commit.

    Syntax: git revert <commit_hashcode>

  8. git push: used to publish your local commit to github repository.

    Syntax: git push origin <branch_name>

  9. diff: Diffing is a function that takes two input data sets and outputs the changes between them.

    Syntax: diff --git a/<file1_name> b/<file2_name>

  10. git pull: to update local files with changes done in the remote repository.

    Syntax: git pull origin <branch_name>

Github

It is the same as a git repository(the folder where .git resides) but just stored on the cloud.

It has a good user interface for git commands are taken care of internally and we just have to click and drag things.

For more information about git visit this link and for github visit this link.