Comprehensive Guide To Learn Git and GitHub

·

4 min read

what is git ?

Git is a version control system that is used to track changes in code during software development. It allows multiple developers to work on the same codebase simultaneously and keep track of changes made by each developer. Git is widely used by software development teams to collaborate and manage their codebase efficiently. It was created by Linus Torvalds in 2005 and has since become one of the most popular version control systems in the world.

what is github ?

GitHub is a web-based platform that is built on top of Git, the version control system. It provides a centralized location for developers to store their code, collaborate on projects, and track changes. GitHub offers many features such as issue tracking, pull requests, and code reviews, making it an essential tool for software development teams. It is used by millions of developers worldwide and is owned by Microsoft.

How to install git

First things first check if it's already there in os

To check run this command in your terminal

git --version

If Git is installed, you'll see the version number. If not, you'll receive an error message indicating that Git is not recognized.

To install git in UNIX

sudo apt update
sudo apt install git

To install git mac

brew install mac

Verify Installation: After installation, verify that Git has been successfully installed by running:

git --version

Once Git is installed, you can configure it with your name and email address using the git config command. For example:

git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"

Replace "Your Name" with your actual name and "youremail@example.com" with your email address. These settings are used to identify your commits.

Git basics

  1. Repository: A repository (or "repo") is a collection of files and folders along with the version history of those files.

  2. Cloning a Repository: To clone an existing repository from a remote server (like GitHub, GitLab, or Bitbucket) to your local machine, use the git clone command:

     git clone <repository_URL>
    

    Replace <repository_URL> with the URL of the repository you want to clone.

  3. Initializing a Repository: To create a new Git repository from scratch in your current directory, use the git init command:

     git init
    
  4. Staging Changes: Before committing changes, you need to stage them using the git add command:

     git add <file1> <file2> ...
    

    This command stages specific files for the next commit. You can also use git add . to stage all changes in the current directory.

  5. Committing Changes: Once changes are staged, you can commit them using the git commit command:

     git commit -m "Commit message"
    

    Replace "Commit message" with a brief description of the changes you're committing.

  6. Checking Status: To see the status of your repository (i.e., which files have been modified, staged, or committed), use the git status command:

     git status
    
  7. Viewing Commit History: To view the commit history of your repository, use the git log command:

     git log
    

    This command displays a chronological list of commits, showing commit hashes, authors, dates, and commit messages.

  8. Branching: Git allows you to work on multiple features or versions of your project simultaneously using branches. To create a new branch, use the git branch command:

     git branch <branch_name>
    

    To switch to a different branch, use the git checkout command:

     git checkout <branch_name>
    

    Alternatively, you can create and switch to a new branch in one step using:

     git checkout -b <new_branch_name>
    
  9. Merging: Once you're done with the changes in a branch, you can merge it back into the main branch (often master or main). First, switch to the branch you want to merge into (e.g., master), then use the git merge command:

     git checkout master
     git merge <branch_name>
    
  10. git clone: Creates a local copy of a remote repository.

    git clone <repository URL>
    
  11. git push: Uploads local changes to a remote repository.

  12. git push origin main
    
  13. git pull: Updates the local repository with changes from the remote repository.

    git pull origin main