Day 8: GitHub Repository – Creation, Cloning, and Useful Commands

Day 8: GitHub Repository – Creation, Cloning, and Useful Commands

Table of contents

Brief Overview

On Day 8, we will cover:

  1. Creating a GitHub Repository: How to create a new repository on GitHub.

  2. Cloning a Repository: How to copy a repository from GitHub to your local machine.

  3. Useful Git Commands: Key commands for working with Git and GitHub repositories.


1. Creating a GitHub Repository

A GitHub repository is a project workspace where you can store your project files, manage versions, and collaborate with others.

Steps to Create a Repository:

  1. Sign In to GitHub:

  2. Navigate to the Repository Creation Page:

    • Click on the "+" icon in the top-right corner of the page.

    • Select "New repository" from the dropdown menu.

  3. Fill in Repository Details:

    • Repository Name: Choose a descriptive name for your repository (e.g., my-project).

    • Description: Optionally, add a brief description of your project.

    • Public or Private: Select whether the repository will be public (visible to everyone) or private (only accessible to you and invited collaborators).

    • Initialize with a README: Optionally, check this box to add a README file to your repository. A README file provides information about your project.

    • Add .gitignore: Optionally, add a .gitignore file to specify which files should be ignored by Git. Choose a template based on your project's language.

    • Choose a License: Optionally, select a license for your project.

  4. Create the Repository:

    • Click the "Create repository" button to finalize the creation.

Example: Here’s how your repository creation form might look:

  • Repository Name: my-project

  • Description: A sample project to demonstrate Git and GitHub

  • Public

  • Initialize this repository with a README

  • Add .gitignore: Node

  • Add a license: MIT

2. Cloning a Repository

Cloning a repository copies the repository from GitHub to your local machine, allowing you to work on the project locally.

Steps to Clone a Repository:

  1. Find the Repository URL:

    • Go to the repository page on GitHub.

    • Click the "Code" button to reveal the repository URL.

    • Choose the HTTPS or SSH URL based on your preference.

  2. Clone the Repository Using Git:

    • Open a terminal or command prompt.

    • Navigate to the directory where you want to clone the repository.

    • Run the following command:

    # Clone using HTTPS
    git clone https://github.com/username/repository-name.git

    # Clone using SSH
    git clone git@github.com:username/repository-name.git

Example:

# Cloning with HTTPS
git clone https://github.com/mohit5upadhyay/FirstJavaProject.git

# Cloning with SSH
git clone git@github.com:mohit5upadhyay/FirstJavaProject.git

3. Useful Git Commands

Once you have cloned a repository, you can use various Git commands to manage your repository and collaborate with others.

Basic Commands:

  • Check Repository Status:

      git status
    

    Shows the status of changes in your working directory and staging area.

  • Stage Changes:

      git add <file>
    

    Adds a file to the staging area. Use git add . to stage all changes.

    Example:

      git add index.html
      git add styles.css
    
  • Commit Changes:

      git commit -m "Commit message"
    

    Commits staged changes to the repository with a descriptive message.

    Example:

      git commit -m "Add new feature to homepage"
    
  • Push Changes:

      git push origin main
    

    Pushes committed changes to the remote repository on GitHub.

    Example:

      git push origin main
    
  • Pull Changes:

      git pull origin main
    

    Fetches and merges changes from the remote repository to your local repository.

    Example:

      git pull origin main
    
  • Create a New Branch:

      git branch new-branch
    

    Creates a new branch named new-branch.

    Example:

      git branch feature/update-ui
    
  • Switch to a Branch:

      git checkout branch-name
    

    Switches to the specified branch.

    Example:

      git checkout feature/update-ui
    
  • Merge Branches:

      git merge branch-name
    

    Merges changes from the specified branch into the current branch.

    Example:

      git merge feature/update-ui
    
  • View Commit History:

      git log
    

    Displays the commit history for the repository.

    Example:

      git log
    

Example Workflow:

  1. Clone a Repository:

     git clone https://github.com/mohit5upadhyay/FirstJavaProject.git
    
  2. Navigate into the Repository:

     cd FirstJavaProject
    
  3. Create and Switch to a New Branch:

     git checkout -b new-feature
    
  4. Make Changes, Stage, and Commit:

     git add .
     git commit -m "Add new feature"
    
  5. Push Changes to GitHub:

     git push origin new-feature
    
  6. Create a Pull Request on GitHub:

    • Go to your repository on GitHub.

    • Click on "Pull requests."

    • Click "New pull request" and follow the prompts to create a PR from your new-feature branch to main.


By the end of Day 8, we will be comfortable in creating a repository on GitHub, cloning it to your local machine, and using essential Git commands to manage your project. For more topics of Git & Github Stay tuned !!