Skip to content

This repository is designed to provide you a comprehensive introduction to both Git and GitHub

Notifications You must be signed in to change notification settings

kamipakistan/git_and_github

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 

Repository files navigation

Git and GitHub Guide

Welcome to the Git and GitHub Guide repository! This repository is designed to provide you with a comprehensive introduction to both Git and GitHub. Whether you're a complete beginner or looking to expand your knowledge, this guide aims to help you understand the fundamental concepts, commands, and best practices associated with version control and collaborative software development.

Table of Contents

  1. Introduction to Git
  2. Getting Started
  3. Basic Git Commands
  4. Branching and Merging
  5. Collaborating on GitHub
  6. Advanced Topics
  7. Resources
  8. Contributing
  9. License

Introduction to Git

Git is a distributed version control system that helps you track changes to your codebase over time. It allows multiple developers to work on the same project simultaneously, keeping a history of changes and enabling efficient collaboration. Whether you're working on personal projects or large-scale software development, understanding Git is crucial for maintaining code quality and managing teamwork.

Getting Started

If you're new to Git and GitHub, this guide will walk you through the initial setup and configuration steps.

Step 1: Installing Git

  1. Download Git: Depending on your operating system, you can download Git from the official website.

  2. Install Git: Run the installer and follow the on-screen instructions to install Git on your system.

Step 2: Configuring Git

After installing Git, you need to configure it with your name and email. This information will be associated with your commits.

  1. Open Terminal (Linux/macOS) or Git Bash (Windows): This is the command-line interface where you'll interact with Git.

  2. Set your name: Enter the following command, replacing "Your Name" with your actual name.

    git config --global user.name "Your Name"
  3. Set your email: Enter the following command, replacing "your@email.com" with your actual email address.

    git config --global user.email "your@email.com"

Step 3: Creating Your First Git Repository Locally

Now that Git is set up, let's create your first Git repository.

  1. Choose a directory: Navigate to the directory where you want to create your repository using the cd command in the terminal.

  2. Initialize a repository: Use the following command to create a new Git repository in the current directory.

    git init
  3. Navigating the .git Tracking Folder:: After initializing the repository, you can use the following command to see the hidden .git tracking folder.

    ls -a

    In the .git folder, Git actively keeps tabs on and documents every change you make to your project. This discrete yet crucial folder acts like Git's vigilant eyes, making sure that no modification goes unnoticed.

  4. Understanding Git Status: Check the status of your repository using the following command. This will show you the current state of your files and any changes that need to be committed.

    git status
  5. Add Files: Add the files you want to track to the repository using the following command. This will stage all of your files, adding them to the staging area for the upcoming commit. This means that all the changes you've made will be ready to be included in the next commit snapshot.

    git add .

    or, to add a single file:

    git add <single-file-name>
  6. Commit Your Changes: Commit your changes with a meaningful message to mark the initial state of your project.

    git commit -m "Initial commit"

    The command git commit -m "Initial commit" creates a snapshot of the staged changes, establishing the starting point of your project. The message provided within the quotation marks serves as a concise description of the changes you've made in this commit.

Step 4: Pushing Your Local Git Repository to GitHub with SSH

Below is a step-by-step guide on how to push your local Git repository to GitHub using SSH keys for enhanced security and authentication:

Create a Repository on GitHub

  1. Log in to your GitHub account.
  2. Click on the "+" sign at the top right corner and select "New repository."
  3. Fill in the repository name, description, and don't add a license, README.md file, or any other files; otherwise, when pushing your repo from the local machine, it will raise a conflict because files already exist in the remote directory.
  4. Click "Create repository."

Set Up SSH Keys for Authentication

  1. Open a terminal window on your local machine.

  2. Generate an SSH key pair by running the following command, replacing the email address with your own:

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  3. Press Enter to accept the default file location.

  4. Enter a secure passphrase. or leave it blank by pressing Enter.

  5. Press Enter.

  6. SSH Key Files

    ls ~/.ssh/

    The output lists several files within the ~/.ssh/ directory:

    • id_rsa: This is your private SSH key. It's a sensitive file that should be kept secure and not shared.
    • id_rsa.pub: This is the corresponding public key to the id_rsa private key. You can share this key with services like GitHub for secure authentication.
  7. Display the contents of your public key

    cat ~/.ssh/id_rsa.pub
  8. Copy your public key to the clipboard: You can copy the public key when the has displayed using the above cat command. or with the help of the following commands.

    For Linux or Unix-like systems, you can use the xclip command:

    cat ~/.ssh/id_rsa.pub | xclip -selection clipboard

    For Windows systems, you can use the clip command:

    cat ~/.ssh/id_rsa.pub | clip
  9. Go to your GitHub settings.

  10. Under "SSH and GPG keys," click "New SSH key" and paste the copied public key.

  11. Provide a recognizable title for the key.

Linking Local Repository to GitHub

  1. On your local machine, navigate to the directory of your Git repository using the terminal.
  2. Run the following command to update the remote URL to use SSH, replacing username with your GitHub username and repository with your repository name:
    git remote add origin git@github.com:username/repository.git
    Example: git remote add origin git@github.com:kamipakistan/git_and_github.git
    git branch -M main

Pushing to GitHub

  1. First, ensure that you have committed your changes locally using git commit.
  2. Now, push your local repository to GitHub's remote repository using the following command:
    git push origin main
    With SSH keys set up, your local repository will be securely connected to your GitHub account. You can now push changes to your remote repository without needing to enter your GitHub credentials, ensuring a smoother and more secure authentication process.

Step 5: Cloning an Existing Repository from GitHub

Cloning allows you to create a local copy of a remote repository hosted on platforms like GitHub. To clone a repository, follow these steps:

  1. Find the repository on GitHub.

  2. Click on the "Code" button and copy the repository URL.

  3. In your terminal, navigate to the directory where you want to clone the repository.

  4. Run the following command, replacing <repository-url> with the URL you copied:

    git clone <repository-url>

    This will create a new folder with the repository's name and download its contents.

Git Branching

Git makes it easy to work on multiple aspects of a project simultaneously through branching:

Creating and Switching Between Branches

Branches in version control allow you to work on different features, bug fixes, or experiments without affecting the main codebase. Here's how to create and switch between branches:

  1. Create a New Branch:

    git checkout -b new-feature   # Creates a new branch named "new-feature" and switches to it
  2. List Branches:

    git branch                  # List all local branches
    git branch -a               # List all branches (local and remote)
  3. Switch Between Branches: Before switching ensure to commit your changes in the feature branch.

    git checkout master          # Switches to the 'master' branch
    git checkout new-feature     # Switches to the 'new-feature' branch
  4. Viewing Differences: Before merging branches, it's a good practice to review the differences between them using the git diff command:

    git diff   # Show differences between 'master' and 'new-feature'

    When starting work on a new branch in Git, we follow these simplified steps:

    1. Ensure You're on the Right Branch.
    2. Make Your Changes.
    3. Stage Your Changes.
    4. Create a Commit.
    5. Repeat [ii-iv] as Needed:
    6. Push Your Changes (Optional).
  5. Push Your Changes (Optional): While not required, you have the option to push your branch and commits to a remote repository to collaborate with others or to keep a backup. Use the git push command to push your local changes to the remote repository:

    git push origin new-feature

Merging Branches and Resolving Conflicts:

After working on a feature or bug fix in a separate branch, you can merge the changes back into the main branch (often called the "master" or "main" branch). The git merge command is used for this purpose. For example, to merge the changes from the "new-feature" branch into the "main" branch:

  1. Merging Branches (Locally): Switch to the branch you want to merge into (e.g., main)

    git checkout main
    

    Merge the feature-branch into main

    git merge new-feature
  2. Merging Remotely (using Pull Request on GitHub): Assuming you've pushed your feature-branch to the remote repository:

    • On GitHub, create a Pull Request (PR) from feature-branch to main-branch.
    • Reviewers will review your changes, discuss them, and eventually approve the PR.
    • Once approved, the changes can be merged using the Merge button on the PR page.

Collaborating on GitHub

GitHub is a web-based platform that enhances Git's functionality, providing tools for collaboration, code review, and project management:

  • Forking repositories
  • Pull requests: proposing and reviewing changes
  • Issue tracking: reporting and resolving problems
  • GitHub Actions: automating workflows

Advanced Topics

As you become more comfortable with Git and GitHub, you can explore advanced concepts and techniques:

  • Git hooks for automation
  • Submodules: managing nested repositories
  • Git rebase vs. merge: choosing the right strategy
  • Managing large repositories and dealing with binaries

Resources

In this section, you'll find a curated list of external resources to further deepen your understanding of Git and GitHub:

  • Recommended reading materials
  • Online tutorials and courses
  • Helpful community forums and discussion platforms

Contributing

Contributions to this Git and GitHub Guide are highly encouraged! If you spot errors, have suggestions for improvement, or want to add new sections, feel free to contribute by opening pull requests.

License

This repository is distributed under the MIT License, allowing you to use, modify, and distribute the content. Make sure to review the license before using the materials for your own projects.

Happy coding and collaborating!


Disclaimer: This guide is intended for educational purposes. It's important to refer to official documentation and additional resources for comprehensive understanding and best practices.

About

This repository is designed to provide you a comprehensive introduction to both Git and GitHub

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published