Git And Github | A Comprehensive Guide cover image

Git And Github | A Comprehensive Guide

AZRAF AL MONZIM
by

Published on

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.

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:

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

    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:

    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.

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

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

    You can also check the configuration:

    git config --list

    Check Global Email and Name:

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

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

    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

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:

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

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

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

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

    git add **/*.c

Checking the Differences between the previous and current versions:

git diff

Restore the earlier version of the file:

git restore < file_name>

Removing from Staging Area:

git restore --staged <file_name>

Committing to the Local Repository:

  1. Only Commit:

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

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

    git commit -a
  4. See Commit History:

    git log

    or for a short version:

    git log --oneline

    or for a short version with graph:

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

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

    git show <commit_id>

Undoing from Local Repository:

  1. Local Repo to Staging Area:

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

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

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

    git reset --hard HEAD^

Checkout

  1. To a specific commit:

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

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

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

    git checkout -

Branching

  1. Create a new branch:

    git branch <branch_name>
  2. List all branches:

    git branch
  3. Switch to a branch:

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

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

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

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

    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:

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

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

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

    git pull
  7. Fetch from Remote Repository:

    git fetch
  8. Push setting the branch upstream:

    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.