Day 8 Basic Git & GitHub

Day 8   Basic Git & GitHub

What is Git?

Git is a version control system that allows you to track changes to files and coordinate work on those files among multiple people. It is commonly used for software development, but it can be used to track changes to any set of files.

With Git, you can keep a record of who made changes to what part of a file, and you can revert back to earlier versions of the file if needed. Git also makes it easy to collaborate with others, as you can share changes and merge the changes made by different people into a single version of a file.

What is GitHub?

GitHub is a web-based platform that provides hosting for version control using Git. It is a subsidiary of Microsoft, and it offers all of the distributed version control and source code management (SCM) functionality of Git as well as adding its own features. GitHub is a very popular platform for developers to share and collaborate on projects, and it is also used for hosting open-source projects.

What is Version Control?

Version control is a system that tracks changes to a file or set of files over time so that you can recall specific versions later. It allows you to revert files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more.

How many types of version controls we have?

There are two main types of version control systems: centralized version control systems and distributed version control systems.

  1. A centralized version control system (CVCS) uses a central server to store all the versions of a project's files. Developers "check out" files from the central server, make changes, and then "check in" the updated files. Examples of CVCS include Subversion and Perforce.

  2. A distributed version control system (DVCS) allows developers to "clone" an entire repository, including the entire version history of the project. This means that they have a complete local copy of the repository, including all branches and past versions. Developers can work independently and then later merge their changes back into the main repository. Examples of DVCS include Git, Mercurial, and Darcs.

Why we use distributed version control over centralized version control?

  1. Better collaboration: In a DVCS, every developer has a full copy of the repository, including the entire history of all changes. This makes it easier for developers to work together, as they don't have to constantly communicate with a central server to commit their changes or to see the changes made by others.

  2. Improved speed: Because developers have a local copy of the repository, they can commit their changes and perform other version control actions faster, as they don't have to communicate with a central server.

  3. Greater flexibility: With a DVCS, developers can work offline and commit their changes later when they do have an internet connection. They can also choose to share their changes with only a subset of the team, rather than pushing all of their changes to a central server.

  4. Enhanced security: In a DVCS, the repository history is stored on multiple servers and computers, which makes it more resistant to data loss. If the central server in a CVCS goes down or the repository becomes corrupted, it can be difficult to recover the lost data.

Overall, the decentralized nature of a DVCS allows for greater collaboration, flexibility, and security, making it a popular choice for many teams.

Basic Git Commands:-

Tasks:-

  1. Create a new repository on GitHub and clone it to your local machine

Creating a GitHub repository involves a few simple steps. Here's a step-by-step guide:

  1. Sign in to GitHub:

    • If you don't have a GitHub account, you need to sign up for one at GitHub.

    • If you already have an account, sign in.

  2. Navigate to Your Dashboard:

    • Once signed in, go to your GitHub dashboard by clicking on the GitHub logo or going to https://github.com/.
  3. Create a New Repository:

    • On your dashboard, click on the "+" sign in the upper right corner and select "New repository."

    • Alternatively, you can click on the "Repositories" tab and then click the green "New" button.

  4. Fill in Repository Details:

    • On the "Create a new repository" page, fill in the following details:

      • Repository name: Choose a name for your repository.

      • Description: Optionally, add a short description.

      • Public or Private: Choose the visibility of your repository.

      • Initialize this repository with a README: You can choose to create an initial README file.

      • Add .gitignore: You can select a template for your .gitignore file.

      • Add a license: You can choose a license for your project.

  5. Create Repository:

    • Click the green "Create repository" button.

  1. Copy Repository URL:

    • After creating the repository, you'll be redirected to the repository page. Copy the URL of your repository from the address bar. It will be something like https://github.com/your-username/your-repository.git.

Now, you have successfully created a GitHub repository. You can use this repository to store and manage your project's source code, documentation, and other files. If you've initialized the repository with a README, you'll already have a starting point for your project.

To clone a GitHub repository to your local machine, follow these steps:

  1. Open a Terminal or Command Prompt:

    • On Linux or macOS, you can use the terminal.

    • On Windows, you can use the Command Prompt or Git Bash.

  2. Navigate to the Directory Where You Want to Clone the Repository:

    • Use the cd command to change to the directory where you want to store the local copy of the repository. For example:

        cd /path/to/your/directory
      
  3. Clone the Repository:

    • Use the git clone command followed by the repository URL you copied earlier.

        git clone https://github.com/your-username/your-repository.git
      
    • Replace the URL with the actual URL of your GitHub repository.

  4. Enter Your GitHub Credentials (If Required):

    • If this is your first time interacting with GitHub from this machine, Git may prompt you to enter your GitHub username and password. Alternatively, if you have set up SSH keys, you might not need to enter credentials.
  5. Verify the Clone:

    • After the clone is complete, you should see a new directory with the name of your repository. Navigate into that directory:

        cd your-repository
      

Now, you have successfully cloned the GitHub repository to your local machine. You can start working on your project, make changes, and use Git commands to manage your code locally. If you make changes, you can commit them and push them back to your GitHub repository.

  1. Make some changes to a file in the repository and commit them to the repository using Git:-

    (A). Open the File for Editing:

    • Use your preferred text editor or code editor to open the file you want to modify. For example, you can use nano, vim, gedit, notepad, or any other text

(B). Make Changes to the File:

  • Add or modify content in the file as needed

(C) Save the Changes:

  • Save the changes and exit the editor.

  • For example, in Vim, you can press esc, then press :wq to confirm changes, and finally press Enter.

(D) Check the Status of Your Repository:

(E) Stage the Changes:

  • Use the git add command to stage the changes for the next commit

(F) Commit the changes:

Use the git commit -m "any message" command to commit the change