From Zero to Git & GitHub Heroโšก๐Ÿ‘จ๐Ÿผโ€๐Ÿ’ป

JAY GOVIND KUMARJAY GOVIND KUMAR
10 min read

Introduction

Welcome to ChaiCode! ๐Ÿš€ As a developer in our cohort, Git and GitHub will be your go-to tools for version control and seamless collaboration. This guide will walk you through setting up Git, working with GitHub repositories, and following best practices for smooth teamwork. By the end, youโ€™ll be ready to contribute to projects, collaborate effortlessly with the team, and manage code like a pro!


๐Ÿค” Ever wonder how developers collaborate on the same codebase without stepping on each otherโ€™s toes? ๐Ÿ‘จโ€๐Ÿ’ป The answer lies in Git and GitHub! ๐Ÿš€๐Ÿ’ป

โ†’ Git is your personal version control system ๐Ÿ“‚๐Ÿ”„, while GitHub ๐Ÿ’ป is your teamโ€™s collaboration platform ๐ŸŒ. Together, they ensure that developers can work on the same project without stepping on each otherโ€™s code ๐Ÿ‘ฃ.

โ†’ Now, learn this in detail ๐Ÿ“š๐Ÿ”


Some Key Concepts Before Diving into Git and GitHub ๐Ÿš€

Version Control System ๐Ÿ“‚๐Ÿ”„

  • A Version Control System helps you track the history of your code, so you can see who changed what, when, and why. Itโ€™s like a game checkpoint ๐Ÿ•น๏ธ: if something goes wrong, you can always return to a previous working version. In software development, version control ensures that multiple developers can work on the same project without overwriting each other's work, and easily revert to earlier versions when necessary.

Repository (Repo) ๐Ÿ“ :

  • A repository (repo) is a digital container where all your projectโ€™s code, files, and version history are stored. Think of it as a project folder ๐Ÿ“ where everything you need for a project is organized and managed. Git and GitHub rely on repositories to keep everything structured, making collaboration smoother and more efficient.


Basics of Git and GitHub

What is Git?

Git is a distributed version control system that helps developers track and manage changes to their codebase over time. It allows you to:

  • Track Changes: Git records changes made to files, helping you keep track of modifications and prevent data loss.

  • Collaboration: Multiple developers can work on the same codebase without overwriting each other's work.

  • Version History: Git maintains a history of changes, allowing you to go back to previous versions of your code.

๐Ÿค” What happens if we accidentally delete important code? How does Git help in saving us from such errors?

โ†’ With Git, if you accidentally delete code or make a mistake, you can easily revert to a previous version where the code is intact. Git keeps track of every change, allowing you to undo mistakes and restore your project to a working state, just like having an undo button for your entire codebase! ๐Ÿ•น๏ธ

What is GitHub?

GitHub is a web-based platform that hosts Git repositories and provides tools for collaborative development. GitHub enables:

  • Hosting Repositories: You can upload your projects and share them with your team.

  • Collaboration: GitHub allows teams to work on different branches, create pull requests (PRs), and review code.

  • Code Sharing: You can share your code publicly or privately, depending on your project.


Installation and Setup

Installing Git

To begin using Git, you'll need to install it on your local machine. Follow these instructions for your operating system:

On Linux

  1. Open the terminal and type the following command based on your distribution:

    • For Ubuntu/Debian: sudo apt-get install git

    • For Fedora: sudo dnf install git

  2. After installation, type git --version to verify.

On MacOs

  1. Open the Terminal and type brew install git (requires Homebrew).

  2. Once Git is installed, type git --version to verify the installation.

On Windows

  1. Download the latest version of Git from Git for Windows.

  2. Run the installer and follow the prompts, accepting the default settings.

  3. After installation, open Git Bash and type git --version to verify the installation.

Check git version

Configuring Git

Once Git is installed, you need to configure your user information to track your commits.

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

This ensures that your commits are associated with your identity.

Creating a GitHub Account

If you donโ€™t already have a GitHub account, follow these steps to create one:

  1. Go to GitHub.

  2. Click Sign up in the top-right corner.

  3. Follow the prompts to create your account (choose a username, email, and password).

  4. After account creation, you'll be directed to your GitHub Dashboard.

๐Ÿ’ก
By using the same user-name and user-email for both, you'll have a consistent identity across your local Git commits and GitHub contributions, making version control and team collaboration much smoother! ๐ŸŒ๐Ÿ‘จโ€๐Ÿ’ป

Cloning the ChaiCode Repository ๐Ÿง‘โ€๐Ÿ’ป๐Ÿ”—

Cloning a repository from GitHub to your local machine is the first step in getting started with a project. This allows you to have a local copy of the repository where you can make changes, commit them, and push them back to GitHub.

  • Cloning Command ๐Ÿ“ฅ

    1. Open your terminal/command prompt.

    2. In the terminal, type the following command to clone the ChaiCode repository:

       git clone https://github.com/ChaiCode/example-repo.git
      

      This command tells Git to create a local copy of the ChaiCode repository on your computer.

  • Navigating into the Cloned Folder ๐Ÿ“‚

    1. To start working in this new directory, you need to navigate into it using the cd (change directory) command.

       cd example-repo
      


Branching Workflow ๐ŸŒณ

At ChaiCode, we follow a branching strategy that involves three primary branches:

  • main: The stable version of the code.

  • development: The branch for ongoing development.

  • feature branches: Used for individual features or tasks.

Creating and Switching Branches

To create a new feature branch and switch to it:

git branch docs/git-github
git checkout docs/git-github

Or, you can combine these into one command:

git checkout -b feature/tea-menu

Merging Branches

Once you've finished working on a feature, you'll merge it into the development branch. First, make sure youโ€™re on the development branch:

git checkout development

Then, merge the feature branch:

git merge feature/tea-menu

If there are merge conflicts, Git will notify you. Youโ€™ll need to resolve conflicts manually and commit the changes.


Basic Commands & Commit Rules

Basic Git Commands for Your Workflow ๐Ÿ› ๏ธ

  1. git status ๐Ÿ“‹: It shows the current state of the working directory and staging area. It tells you which files have been changed, which are staged for the next commit, and which are untracked.

     PS D:\Coding\Git_Github> git status
     On branch main
     Your branch is up to date with 'origin/main'.
    
     Changes not staged for commit:
       (use "git add <file>..." to update what will be committed)
       (use "git restore <file>..." to discard changes in working directory)
    
             modified:   README.md
    
     Untracked files:
       (use "git add <file>..." to include in what will be committed)
    
             new-feature.txt
    
    • Modified files are those that you have changed but havenโ€™t staged yet for commit.

    • Untracked files are files that Git is unaware of, which may need to be added before committing.

  2. git add โž•: It stages changes files so that they can be included in the next commit.

     git add <file-name>    # Stage a specific file
     git add .              # Stage all modified and new files
    

    After running this command:

    • The files you added will be staged for the next commit.

    • A hidden .git folder will be created in your project directory if it doesnโ€™t exist. This folder contains the repository's configuration and versioning data.

  3. git commit -m ๐Ÿ“: It saves your changes to the local repository. The -m flag allows you to add a commit message describing what changes you've made.

     git commit -m "Your descriptive commit message"
    
  4. git pull ๐Ÿ”„: It fetches the latest changes from the remote repository and merges them into your current branch. It helps keep your local copy up-to-date.

     git pull origin <branch-name>
    
  5. git push ๐Ÿš€: It uploads your local commits to the remote repository (e.g., GitHub). This is how you share your changes with your team.

     git push origin <branch-name>
    
  6. git log ๐Ÿ“œ: It shows the history of commits in the current branch.

     PS D:\Coding\Git_Github> git log
     commit 1234567890abcdef
     Author: Your Name <your.email@example.com>
     Date:   Mon Jan 7 12:34:56 2025 +0000
    
         feat: Add tea selection feature
    
     commit abcdef1234567890
     Author: Your Name <your.email@example.com>
     Date:   Sun Jan 6 14:22:10 2025 +0000
    
         fix: Resolve login issue for tea enthusiasts
    

    Youโ€™ll see each commitโ€™s unique hash, author, date, and message.

What Happens with the .git Folder? ๐Ÿ“‚
The .git folder is a hidden directory that Git uses to track your changes, versions, and history. When you initialize a repository using git init, it creates this folder to store all the necessary information about your project. It contains all the metadata and objects that track changes, commits, and configurations.

๐Ÿค” What happens if you make a mistake while committing? Can you undo it?

โ†’ If you accidentally commit the wrong changes, you can undo the last commit with:

git reset --soft HEAD~1

Rules for Writing Good Commit Messages ๐Ÿ“‹

To maintain clarity and consistency in your commits, follow these rules for writing commit messages:

  1. Use Present Tense โœ๏ธ: "Add feature" instead of "Added feature."

  2. Capitalize the first letter.

  3. Keep it Short and Sweet ๐Ÿ’ฌ: Aim for 50 characters or fewer.

  4. Capitalize the First Letter ๐Ÿ“Œ

  5. Use Meaningful Prefixes ๐Ÿ”‘ to categorize your commits:

    • feat: For new features

    • fix: For bug fixes

    • docs: For documentation changes

    • style: For formatting changes (e.g., spaces, indentation, missing semicolons)

    • refactor: For code changes that do not affect functionality

    • test: For adding or modifying tests

    • chore: For minor tasks that don't fit other categories (e.g., updating dependencies)

Example:

Commit TypeBad Commit MessageGood Commit Message
FeatureAdded new featurefeat: Add tea selection feature
Bug FixFixed bugfix: Resolve login form crash on submission
DocumentationUpdate READMEdocs: Add contributing guidelines to README
Code RefactorRefactor coderefactor: Simplify login function logic
TestAdd test for logintest: Add unit tests for user login functionality
ChoreUpdate dependencieschore: Update project dependencies to latest version

๐Ÿค” Why is it so important to write good commit messages?

  • Collaboration : Good commit messages allow your teammates to quickly understand what youโ€™ve done, making it easier for everyone to stay on the same page

  • Code History: As you and your team make changes, your commit messages serve as a timeline of your projectโ€™s history. Descriptive messages help you (and others) understand why a certain change was made.

  • Tracking Bugs: If a bug arises, you can easily identify which commit caused it by reading through the messages. Well-written messages will provide clues to help fix the issue.

  • Project Documentation: Your commit history becomes an important part of the project documentation, especially when revisiting a project after some time.


Pull Requests (PR)

A Pull Request (PR) is a way to propose changes to the main codebase. Here's how to create one on GitHub:

  1. Push Your Branch ๐Ÿš€ to GitHub:

     git push origin docs/git-github
    
  2. Navigate to your repository on GitHub ๐ŸŒ, and click the Compare & Pull Request button.

  3. Select the Base and Compare Branches โš–๏ธ

  4. Write a Clear Pull Request Title โœ๏ธ

  5. Provide a Descriptive Pull Request Description ๐Ÿ“„

  6. Click Create Pull Request.

Once the PR is reviewed and approved, it will be merged into the development branch.

๐Ÿค” What happens if thereโ€™s a conflict between branches? How do you resolve it?

โ†’ If thereโ€™s a conflict between the code in your branch and the base branch youโ€™re merging into, a merge conflict occurs.


Best Practices

๐Ÿ’ก
Commit Regularly: Commit your work often to track changes and make it easier to revert to previous states.
๐Ÿ’ก
Use Descriptive Commit Messages: Always write meaningful commit messages that explain why the change was made.
๐Ÿ’ก
Pull Regularly: Always pull the latest changes from the main repository to avoid conflicts.
๐Ÿ’ก
Work on Feature Branches: Avoid working directly on the main branch. Create a new branch for each feature or task.

Further Resources ๐Ÿ“š

๐Ÿค” Ready to start collaborating like a pro? Your Git and GitHub journey has just begun! โœจ

5
Subscribe to my newsletter

Read articles from JAY GOVIND KUMAR directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

JAY GOVIND KUMAR
JAY GOVIND KUMAR