Git & GitHub: Onboarding Docs🚀

Git & GitHub: Onboarding Docs🚀

¡

9 min read

In this document, you will learn why Git and GitHub are the backbone of our ChaiCode Cohort Company. You might have heard about pushing and pulling before—gym lovers might relate these terms to their push-pull-leg routine—but in the development world, it’s a completely different story. Before jumping into the tutorial, we want to ensure that our new hires actually understand the purpose behind these tools.

Usually, when we explore any topic, we first analyze the ‘what’ and then proceed to the ‘why.’ But this time, let’s apply some reverse engineering to get a better grasp of things.

Why ‘Git’?

Most of us have used or at least heard of Google Docs. It’s a tool that records changes and keeps a history of previous versions, making it easy to navigate changes or collaborate with a team. Without this tracking system, teamwork would quickly descend into chaos.

Imagine you pour your heart and soul to write a piece of content only for your teammate to come in , sweep off the content and rewrite it without realizing you've already made changes. The frustration.

“What just happened??”
“Sorry mate , I didn’t know you already made changes!”

Similarly, in the real world of development, multiple people work collaboratively as a team on a single codebase, with each developer focusing on different sections (modularity). To manage this, a tracking system was introduced specifically for coding purposes—this system is called a Version Control System (VCS), and it runs locally on your computer.

While there are several VCS options available, the winner in the world of VCS is Git.

When you initialize Git in your project using git init, it creates a hidden .git folder that stores all the version history and metadata of your code. If you share your project by copying it to someone else (e.g., via USB), they will also get this .git folder. As long as they have Git installed, they can use commands like git log to view the history of changes you've made to the code.

Why ‘GitHub’?

Sending code through physical storage like USB drives is outdated. Developers today are spread across the globe, working together in real-time. So, we need a cloud storage solution, replacing USB drives with services like Google Drive or Mega. While you can share your code and the .git folder via these cloud services ;you could still see version history, logs, and commits since they stay inside the .git folder. However, these services are designed for general media like videos and images, not for coding collaboration. That's why GitHub exists—designed specifically for version control and seamless collaboration.

Importance of Git and GitHub

  • Git Advantages:

    • Keep a record of all changes

    • See progress over time

    • Work on different parts of a project without interfering with others

    • Use it anywhere, even without an internet connection

GitHub Advantages:

  • Cloud-based hosting that provides Easy collaboration (pull requests)

  • View anyone’s changes in the codebase around the globe

Installation Setup:Windows

  1. Download the Standalone Installer for Windows from Git’s official website https://git-scm.com/downloads and follow the prompts to complete the installation by running the .exe.

    In cmd , type git --version to check if you have installed git.

    If you haven’t created a GitHub account , then follow this official documentation of GitHub https://docs.github.com/en/get-started/start-your-journey/creating-an-account-on-github

    Now Open ‘Git Bash ‘ .

Run the following commands to set your name and email of your GitHub account

    git config --global user.name "Your Name" 
    git config --global user.email "your.email@example.com
  1. "Once you've set your name and email globally using the git config --global command, Git will use those settings automatically for every project you work on, so you don't need to enter them every time you make a commit

  2. Run the following command to check if your Git Configuration was set correctly :

     git config --global --list
    

    If you see values like these , then the configuration was set correctly.

     user.name=Your Name 
     user.email=your.email@example.com
    
  3. To set up an SSH key so that you don't have to enter your password every time you interact with GitHub, enter this in the Git Bash.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Press Enter to save it in the default location (~/.ssh/id_rsa).Enter a **Passphrase(**optional) else leave it empty and proceed.

  1. Type the following commands in the Bash

     eval "$(ssh-agent -s)"
    

    The output will show something like: Agent pid 1234.

     ssh-add ~/.ssh/id_rsa
    
     clip < ~/.ssh/id_rsa.pub
    

    This command would copy the SSH public key to your clipboard.

  2. Now go to https://github.com/settings/keys

  3. In the "Title" field, name your key (e.g., "My Laptop").

  4. Paste the copied key into the "Key" field.

  5. Click Add SSH key.

  6. Test if the SSH key is working by running:

    ssh -T git@github.com
    

    If successful you will see,

    Hi your_username! You've successfully authenticated, but GitHub does not provide shell access.

Cloning

Cloning in Git refers to the process of copying a remote repository (often public) including its histories to your local machine. This allows you to work on the project locally, make changes, and commit those changes independently from the remote repository. However, the changes you make locally won't affect the remote repository unless you push your changes back to the remote.

Note:If you clone a public repository you don't own , you can't push changes unless you have permission. You'll get a 403 error.

Fork the public repository to create a copy in your account.

Then press ” Create fork” .Click on “Copy URL To clipboard”

  1. Now open your Command Prompt , and then type the following command

     git clone <the-url-which-you-copied>
    

Enter cd <cloned-project-folder-name> to go the folder directory. Now enter code . to go to the folder with your vs code

  1. Now open your Vscode terminal (Ctrl+Shift+`) to execute further git commands.

Basic Git commands

  1. git init - Adds a .git folder to the current folder which you are working on

  1. git add . - to add the entire file contents

    git add <foldername>-to add the particular folder

    git rm —cached <foldername> - to untrack / remove the tracked file

    Now before moving on , there are 3 important terms to understand with the files/folders which have been initialized with .git folder

State nameDescription
UntrackedThe files have not been added yet
UnstagedThe files have been added , but they have been modified and haven’t been added after the modification
StagedThe files have been added and the changes have been marked

3 .git status -shows the current state of tracked and untracked files, indicating whether they are modified, staged, or untracked.

4 . git commit -m "message" - save the staged changes to the repository with a message about what action has been taken

Note: A commit can only be made if the file has been modified, meaning it is different from the previous commit.

One line was removed from one.txt, making it unstaged. The line was then added back, and the file was staged again. After staging the changes, the file was committed.

Rules to be followed while writing a message

  1. Use the present tense ("Add feature" not "Added feature").

  2. Capitalize the first letter.

  3. Keep the message short (50 characters or less).

  4. Use prefixes like fix:, feat:, chore:, docs: for categorization.

    1. fix - bugs / issues related

    2. feat - addition/removal of a feature/functionality

    3. chore- routine maintenance /updates related

    4. docs - changes in the documentation

5 . git log - Shows history of all the previous commits

  1. git push-upload all your local commits to a remote repository
git push origin <your-branch>

Branching Workflows

A branch workflow is how developers use different branches in Git to work on and organize their code changes. Changes are made in independent branches, and once everything is tested and approved, they are merged into the main/master branch.

At Chai Code , we have 3 branches :

  1. Main

  2. Features

  3. Development

Note:The main branch is automatically created when you initialize a Git repository,

To create a branch and switch to it

git branch <branch-name>
git checkout <branch-name>

2 Now commit your changes as normal

Note:

  1. If you commit changes under one branch, you can't directly push those changes to another branch. To move your changes to a different branch, you'll need to merge the branches

  2. A child branch always inherits the changes from its parent branch. For example, if you create branch A with git branch A and then create branch B with git branch B, all the changes, commits, and logs from branch A will be reflected in branch B at the time of creation. This includes the content and history up until the point where branch B was created. After that both are independent of each other.

  3. The code to merge another branch to your current branch(It need not be the main branch) is:

git merge <branchname>

Conflicts

You will get a conflict in Git when:

  1. Two branches modify the same line in a file.

  2. One branch deletes a file, and another branch modifies or adds to that file.

You have to handle the conflicts manually by choosing which change to accept and which one to reject.

Best practices to avoid conflicts:

  1. Commit frequently and merge it with the main periodically

  2. Pull frequently from the main branch to keep in charge

  3. Avoid staging unwanted changes, you can run git status before committing to ensure only the desired files are staged.

Pull request:

If you want to contribute to the original repository ,you can create pull request.(you can change what branch you want to pull by selecting from the dropdown menu in top left corner).This tells the maintainers of the original repository that you have changes you'd like them to consider merging into their project.Pull requests are usually done on branches.

If you want to give pull request only on a certain commit ; Cherry-pick is a Git command used to apply a specific commit from one branch to another. This allows you to selectively apply changes from one branch without merging the entire branch .So if you want to only pull one commit from branch called’ feature’ and not on the entire branch , you would first copy its commit hash. Create a new branch and switch to it and then use ‘cherry-pick’.

git branch <branchname>
bit checkout <branchname>
git cherry-pick <commit-hash>

Thankyou!

Â