MASTERING GIT

What is Git

Git is a distributed version control system used to track changes in source code during software development. It
helps developers collaborate on projects by allowing them to work on the same codebase without overwriting each other’s work.

Features

  1. Distributed Version Control System: Git isn’t just a regular version control system it’s distributed. Each developer has their own complete copy of the project’s history (called a repository). Unlike traditional systems, where you rely on a central server, Git lets you work independently, even offline.
  2. Collaboration and Code Harmony: Git enables seamless collaboration. Developers can clone repositories, work on their changes, and then sync them back to the shared repository.
  3. Branching and Merging: Developers can create branches to work on new features or fixes independently, then merge these changes back into the main codebase.
  4. Staging and Committing: Changes are first added to a staging area, where they can be reviewed before being committed (saved) to the project history.

About Git

Git was created by Linus Torvalds, the same person who created the Linux operating system. He developed Git in 2005 to address a problem he was facing while managing the Linux kernel development.

Installing Git

On Windows

  1. Download Git for Windows: Go to the Official Git Website and download the installer for Windows.
  2. Run the Installer: Once the download completes, open the .exe file and start the installation process. Follow the default settings in the setup wizard unless you have specific needs.
  3. Configure Git: After installation, open Git Bash (installed alongside Git) and run the following commands to configure your name and email.
  4. Verify Installation: Open Git Bash or Command Prompt and type: git --version If Git is installed correctly, this command will display the installed version number.
  5. git --version

Git on macOS

  1. Use Homebrew (Recommended): If you have Homebrew installed, open the Terminal and run:
  2. brew install git

  3. Manual Installation: Alternatively, you can download Git from git-scm.com. After downloading the .dmg file, follow the instructions to install Git.
  4. Configure Git: Run these commands in your terminal to set your user name and email:
  5. git config --global user.name "Your Name"

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

  6. Verify Installation: Run: It will show the version of Git installed if everything is set up correctly.
  7. git --version

Installing Git on Linux

  1. Ubuntu/Debian: Open your terminal and run:
  2. sudo apt update sudo apt install git

  3. Fedora: Run:
  4. sudo dnf install git

  5. Arch Linux: Run:
  6. sudo pacman -S git

  7. Configure Git: Set your user name and email: user.name "Your Name"
  8. git config --global user.email "your.email@example.com"

  9. Verify Installation: Run: You should see the version number of Git installed.
  10. git --version

Initializing a Git repository

  1. Open your terminal or command prompt:

    Windows: Open Git Bash or Command Prompt.

    macOS/Linux: Open your Terminal.

  2. Navigate to your project folder: Use the cd (change directory) command to navigate to the directory where you want to initialize the repository.
  3. cd /path/to/your/project

    For example:

    cd /Users/yourusername/Documents/myproject

  4. Initialize the Git repository: Once you're inside the folder, run the following command to initialize an empty Git repository:
  5. git init

  6. Start Tracking Files: Now that the Git repository is initialized, you can start adding files to the repository, Check the status of your repository:

    git status

    This will show you which files are untracked (files that Git is not currently tracking).

    Add files to the staging area: To add all files in your project directory to Git:

    git add .

    Or, to add specific files:

    git add filename.ext

    Make your first commit: After staging the files, you can create your first commit:

    git commit -m "Initial commit"

    Writing Good Commit Messages:

    A good commit message is important for understanding the history of your project. Follow these guidelines:

    • Start with a short (50 characters or less) summary of what was changed.
    • Leave a blank line between the summary and detailed description.
    • Use the imperative mood (e.g., “Add feature” instead of “Added feature”).
    • Provide a detailed explanation of why changes were made, especially for complex commits.
  7. After initialization:

    Your project is now a Git repository, and you can start using Git commands to track changes, create branches, and collaborate with others. If you need to connect this local repository to a remote repository (such as on GitHub, GitLab, or Bitbucket), you can do so by running:

    git remote add origin https://github.com/yourusername/your-repo.git

    git push -u origin master

    Viewing Past Commits:

    To view the history of past commits, use:

    git log

    This command will display all previous commits along with their messages and metadata (author, date, etc.).

    Reverting to a Previous Commit:

    To go back to a specific commit, you can use the following command:

    git checkout

    Replace with the commit ID you want to go back to. To return to the latest commit (the HEAD), use:

    git checkout master

    Branches in Git

    Branches in Git are used to develop features, bug fixes, or experiments isolated from the main codebase (typically the master or main branch). They are important because they allow multiple developers to work on different parts of a project simultaneously without interfering with each other's work.

    Why Branches Are Important:

    Creating a Branch

    To create a new branch in Git, use the following command:

    git branch new-branch-name

    To switch to the new branch right after creating it, use:

    git checkout -b new-branch-name

    Viewing All Branches

    To view all the branches in your repository, use the following command:

    git branch

    The active branch will have an asterisk (*) next to it.

    Switching Active Branches

    To switch between branches, use:

    git checkout branch-name

    For example, to switch to a branch named feature-branch:
    git checkout feature-branch

    Merging a Branch

    To merge a branch into the current branch, use the following command:

    git merge branch-name

    For example, if you are on the master branch and you want to merge feature-branch into it:

    git checkout master

    git merge feature-branch

    Deleting a Branch

    After merging a branch, you may want to delete it. To delete a branch, use:

    git branch -d branch-name

    If the branch hasn't been merged yet and you still want to delete it, use:

    git branch -D branch-name

    Learn about GitHub:

    GitHub is a hosting service for Git repositories, where you can collaborate with others.

    Explore pull requests: A pull request is a way to propose changes to a repository on GitHub.

    Study rebase: Rebasing is an advanced Git feature for integrating changes from one branch into another without creating a merge commit.