All writing

Git And Github | A Comprehensive Guide

Git and GitHub are powerful tools for version control and collaborative software development. This comprehensive guide will walk you through the basics of using Git and GitHub, explain how they work together, and provide essential tips to enhance your workflow.

On this page
  1. Introduction to Git and GitHub
  2. Git is a Version Control System
  3. Git vs GitHub
  4. Git Setup and Configuration
  5. Demo Repository Setup
  6. Staging Area Concept in Git
  7. Basic Git Workflow:
  8. Branching and Merging:
  9. Collaborating with GitHub:
  10. Add and Commit [Local Repo]
  11. Adding to the Staging Area:
  12. Committing to the Local Repository:
  13. Undoing from Local Repository:
  14. Checkout
  15. Branching
  16. Git Collaboration
  17. Essential Git Tips:

Introduction to Git and GitHub

Git is a distributed version control system that allows developers to track changes in their codebase and collaborate with others effectively. It was created by Linus Torvalds in 2005 and has since become the standard for version control in the software development industry. Git is designed to be fast, flexible, and scalable, making it suitable for projects of all sizes.

GitHub, on the other hand, is a web-based platform that provides hosting for Git repositories. It adds a layer of functionality on top of Git, making it easier to manage and collaborate on projects. GitHub offers features like issue tracking, pull requests, and team collaboration, making it a popular choice for open-source projects and team-based development.

Git is a Version Control System

  • Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.
  • Git is a version control system that allows you to track changes to your code and collaborate with others.

Git vs GitHub

FeatureGitGitHub
PurposeTool to control and track versions of code.Cloud service to host git repositories.
InstallationInstalled and maintained locally on a developer's computer.Remote connection provider and maintained on cloud/web.
InterfaceGenerally uses command line interface (CLI) with a limited GUI.Provides a graphical user interface (GUI) to interact with git repositories.
Branching and MergingAllows for branching and merging of code.Offers pull requests and code review features for collaboration.
Basic FeaturesProvides basic features like committing changes and creating branches.Offers additional features like issue tracking, project management, and code hosting.

Git Setup and Configuration

Before diving into the world of Git and GitHub, you need to set up Git on your machine. Start by installing Git from the official website https://git-scm.com and configuring your global settings, including your name and email address. This information will be associated with your commits.

  1. Download and install Git from https://git-scm.com/downloads

  2. Open Git Bash and check the version of Git installed:

    bash
    git --version
  3. Configure Git with your name and email address:

    bash
    git config --global user.name "Your Name"
    git config --global user.email "Your Email"

Demo Repository Setup

  1. Create a New Folder on your Desktop called yourFolderName:

  2. Open Git Bash and navigate to the folder you just created:

    bash
    cd yourFolderName
  3. **Initializing a Git Repository:
    **To start using Git for version control, you need to initialize a repository. Open your terminal, navigate to the desired project directory, and run the command git init. This command creates a hidden .git directory that will store all the necessary Git data for your project.

    bash
    git init
  4. If you did not set up global configuration, you can configure Git with your name and email address:

    bash
    git config user.name "Azraf Al Monzim"
    git config user.email "your@email.here"

    You can also check the configuration:

    bash
    git config --list

    Check Global Email and Name:

    bash
    git config user.email
    git config user.name
  5. Create some files in the folder:

    bash
    touch file1.txt touch file2.txt
  6. Check the status of the repository:

    bash
    git status

    Let’s add the new file to the staging Area

Staging Area Concept in Git

Basic Git Workflow:

The fundamental Git workflow consists of three stages: the working directory, the staging area, and the repository. The working directory represents the current state of your project, where you make changes to files. The staging area allows you to select specific changes to be included in the next commit, and the repository is where all the committed snapshots of your project are stored.

To track changes in your project, use the command git add <filename> to add specific files or git add . to add all changes. Then, use git commit -m "commit message" to commit the changes to the repository. Remember to write descriptive commit messages that summarize the changes made.

Branching and Merging:

One of the most powerful features of Git is its ability to handle branching and merging with ease. Branches allow you to create separate lines of development, making it possible to work on different features or bug fixes simultaneously. To create a new branch, use the command git branch <branch-name>, and to switch to a branch, use

bash
git checkout <branch-name>

When you want to integrate changes from one branch into another, you can use the git merge command. This combines the changes from the source branch into the destination branch. Alternatively, pull requests on GitHub provide a visual interface for reviewing and merging changes between branches. They allow collaborators to discuss changes, suggest modifications, and ensure that the code meets the project’s standards before merging.

Collaborating with GitHub:

GitHub provides a platform for effective collaboration among developers. To collaborate on GitHub, start by creating a repository and pushing your local repository to GitHub using git remote add origin <repository-url> and git push -u origin <branch-name>. This establishes a connection between your local repository and the repository on GitHub.

Collaborators can clone the repository, make changes, and submit pull requests for code review. Pull requests provide a centralized location for reviewing code changes, leaving comments, and discussing potential improvements. Project maintainers can review the changes, suggest modifications, and, once approved, merge the changes into the main branch. GitHub also offers additional features like issue tracking, wiki pages, and project management tools, further enhancing collaboration within a team.

Add and Commit [Local Repo]

Adding to the Staging Area:

  1. Only a specific File:

    bash
    git add <file_name>
  2. Only files of a folder but not the subfolder:

    bash
    git add .
    # or
    git add *
  3. All files and subfolders recursively:

    bash
    git add -A
  4. Only a particular type of file from the directory:

    bash
    git add *.c
  5. Only a particular type of file from the directory & subdirectory:

    bash
    git add **/*.c

Checking the Differences between the previous and current versions:

bash
git diff

Restore the earlier version of the file:

bash
git restore < file_name>

Removing from Staging Area:

bash
git restore --staged <file_name>

Committing to the Local Repository:

  1. Only Commit:

    bash
    git commit -m "Your Commit Message"
  2. Commit and Add:

    bash
    git commit -am "Your Commit Message"
  3. Commit and Add with Editor:

    bash
    git commit -a
  4. See Commit History:

    bash
    git log

    or for a short version:

    bash
    git log --oneline

    or for a short version with graph:

    bash
    git log --oneline --graph
  5. See Commit History with Author:

    bash
    git log --oneline --graph --all --decorate --author="Azraf Al Monzim"
  6. See Specific Commit:

    bash
    git show <commit_id>

Undoing from Local Repository:

  1. Local Repo to Staging Area:

    bash
    git restore --staged <file_name>
  2. Local Repo to Staging Area (Multiple Commit):

    bash
    git reset --soft HEAD~<number_of_commit>
    bash
    git reset --soft HEAD~2
  3. Local Repo to Working Directory:

    bash
    git restore <file_name>
  4. Total Deletion of a Commit:

    bash
    git reset --hard HEAD^

Checkout

  1. To a specific commit:

    bash
    git checkout <commit_id>
  2. To a specific branch:

    bash
    git checkout <branch_name>
  3. To a specific tag:

    bash
    git checkout <tag_name>
  4. Move back to the previous branch:

    bash
    git checkout -

Branching

  1. Create a new branch:

    bash
    git branch <branch_name>
  2. List all branches:

    bash
    git branch
  3. Switch to a branch:

    bash
    git checkout <branch_name>
  4. Create a new branch and switch to it:

    bash
    git checkout -b <branch_name>
  5. Delete a branch:

    bash
    git branch -d <branch_name>
  6. Merge a branch:

    bash
    git merge <branch_name>
  7. Merge a branch with a commit message:

    bash
    git merge <branch_name> -m "Your Commit Message"

Git Collaboration

  1. Create a new repository on GitHub

  2. Copy the remote repository URL

  3. Add the remote repository URL to your local repository:

    bash
    git remote add origin <remote_repository_URL>
  4. Clone a Remote Repository:

    bash
    git clone <remote_repository_URL>
  5. Push to Remote Repository:

    bash
    git push -u origin
  6. Pull from Remote Repository:

    bash
    git pull
  7. Fetch from Remote Repository:

    bash
    git fetch
  8. Push setting the branch upstream:

    bash
    git push --set-upstream origin <branch_name>

Essential Git Tips:

  1. Regularly commit your changes: Committing frequently allows you to capture incremental snapshots of your work, making it easier to track and revert changes if needed.
  2. Write meaningful commit messages: A well-written commit message provides context and facilitates understanding when reviewing the project’s history.
  3. Use branches for new features or bug fixes: Branching allows for parallel development and isolates changes, keeping the main branch clean and stable.
  4. Regularly pull changes from the main branch: Keeping your branch up to date with the latest developments in the main branch helps avoid conflicts and ensures a smooth merging process.
  5. Leverage the power of Git aliases: Git aliases allow you to create shortcuts for frequently used commands, saving time and reducing typing errors.
  6. Explore Git’s extensive documentation and online resources: Git has a rich ecosystem of documentation, tutorials, and online communities that can help you expand your knowledge and discover advanced features.

Git and GitHub are indispensable tools for modern software development. By understanding the basics of version control, branching, merging, and collaborating on GitHub, you can enhance your productivity and streamline your development workflow. Remember to practice regularly, explore advanced Git features, and leverage the collaboration capabilities of GitHub to become a proficient Git user.

esc

Recent