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
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
"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
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
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.
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.
Now go to https://github.com/settings/keys
In the "Title" field, name your key (e.g., "My Laptop").
Paste the copied key into the "Key" field.
Click Add SSH key.
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â
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
- Now open your Vscode terminal (Ctrl+Shift+`) to execute further git commands.
Basic Git commands
git init
- Adds a .git folder to the current folder which you are working on
git add .
- to add the entire file contentsgit add <foldername>
-to add the particular foldergit rm âcached <foldername>
- to untrack / remove the tracked fileNow before moving on , there are 3 important terms to understand with the files/folders which have been initialized with .git folder
State name | Description |
Untracked | The files have not been added yet |
Unstaged | The files have been added , but they have been modified and havenât been added after the modification |
Staged | The 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
Use the present tense ("Add feature" not "Added feature").
Capitalize the first letter.
Keep the message short (50 characters or less).
Use prefixes like
fix:
,feat:
,chore:
,docs:
for categorization.fix - bugs / issues related
feat - addition/removal of a feature/functionality
chore- routine maintenance /updates related
docs - changes in the documentation
5 . git log
- Shows history of all the previous commits
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 :
Main
Features
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:
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
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 withgit 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.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:
Two branches modify the same line in a file.
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:
Commit frequently and merge it with the main periodically
Pull frequently from the main branch to keep in charge
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!