Onboarding Documentation: Git and GitHub for ChaiCohort
A concise guide to understanding Git and GitHub
This documentation is specially designed for beginners to introduce them to two amazing tools: Git and GitHub, which are frequently used in our company. The purpose of this article is to help those who have no prior knowledge of Git and GitHub and to guide them in setting up these tools. Additionally, we will learn how Git helps us with code management and collaboration.
Basics of Git and GitHub :
What is Git?
Git is a distributed version control system (VCS) designed to help developers in:-
track changes in their code,
collaborate on projects,
And manage multiple versions of their work.
It allows teams to work efficiently on the same codebase without interfering with each other's work.
What is meant by a version control system (VCS)?
A Version Control System (VCS) is a tool or software that helps you track changes made to files over time.
Why Use a Version Control System?
Avoid Losing Work
Collaboration
Maintain History
What is GitHub?
GitHub is a web-based platform that makes it easy to collaborate on code projects, manage versions of files, and host repositories online. Built on top of Git (a version control system), GitHub adds tools and features for collaboration, project management, and automation, making it one of the most popular platforms for developers and teams worldwide.
Setup of Git and GitHub :
How is Git installed and set up on the system?
Installing and setting up Git on your system is a straightforward process.
- Install Git ( for Windows ) :
Download Git → Go to the Git for Windows website and download the latest installer.
Run the Installer.
Verify Installation → Open
Command Prompt
orPowerShell
. And run the given command.git --version
What is a Repository?
A repository is a collection of files and directories that are stored together. It is a way to store and manage your code. A repository is like a folder on your computer, but it is more than just a folder. It can contain other files, folders, and even other repositories. You can think of a repository as a container that holds all your code.
By default, Git does not automatically track your repository on your local machine. To check whether Git is tracking your files or to see the current state of your repository, you can run the following command:
git status
Creating a repository:
Creating a repository is similar to creating a folder on your local machine, but with the added step of initializing Git to enable version control, you can use the following command:
git status
git init
git status → It shows you the current state of your repository.
git init → It initializes Git version control in an existing directory.
To create a new directory and initialize a Git repository in it, you would first create the directory manually and then run git init
inside it:
mkdir my_project # Create a new directory
cd my_project # Navigate into the directory
git init # Initialize the Git repository
In Git, the git add
command is used to track changes in files and stage them for the next commit. When you modify a file in your working directory, Git doesn't automatically track the changes. To tell Git which files you want to include in the next commit, you use git add <filename>
or git add .
(to stage all changes in the current directory). After that, you can commit those changes using git commit
.
git add <filename> # Stages changes in a specific file.
git add . # Stages all changes in the current directory and its subdirectories.
Stage :
staging refers to the process of preparing changes for inclusion in the next commit. The staging area (also called the index) is a place where changes are stored temporarily before they are committed to the repository. You can use the following command to stage a file:
git init
git add <file> <file2>
git status
Here we are initializing the repository and adding a file to the repository. Then we can see that the file is now being tracked by git. Currently, our files are in the staging area, this means that we have not yet committed the changes but are ready to be committed.
Commit :
commit is a way to save your changes to your repository. It is a way to record your changes and make them permanent. You can think of a commit as a snapshot of your code at a particular point in time. When you commit your changes, you are telling git to save them in a permanent way. This way, you can always go back to that point in time and see what you changed.
WRITE \=> ADD \=> COMMIT
git commit -m "Describe the changes made in this commit"
-m: This option allows you to add a commit message inline.
If you don't use
-m
, Git will open an editor where you can write a more detailed commit message.
Some Rules for writing commit messages:
Use the imperative mood ( always use present tense )
→ Write commit messages in the imperative tone, as if you're giving a command. For example:
→ Correct: "Fix bug in login function"
→ Incorrect: "Fixed bug in login function"
Keep the subject line short and concise. Example:
→ Correct: "Add unit tests for authentication"
→ Incorrect: "Added some unit tests for the authentication module, including edge cases."
Explain why the change was made in the body.
Use "Fixes" or "Closes" for issue tracking → If the commit fixes or addresses a specific issue or bug, mention the issue number and use keywords like "Fixes", "Closes", or "Resolves" to automatically close the issue when the commit is pushed.
Fix crash on user profile page Fixes #123 (Crash when loading profile page with empty fields)
Avoid using vague terms → Try not to use vague terms like "Updated", "Fixed", or "Changed" without context. Be specific about what was done. Example :
→ Correct: "Update API endpoint to support JSON response"
→ Incorrect: "Update some files"
Logs :
git log
git log --oneline # show commit history in one line ( in short form )
The git log
command is used to view the commit history of a Git repository. It displays a list of commits in reverse chronological order, starting with the most recent commit.
.gitignore :
Gitignore is a file that tells Git which files and folders to ignore. It is a way to prevent git from tracking certain files or folders.
node_modules
.env
.vscode
.gitkeep :
A .gitkeep
file is typically an empty file used to ensure that Git tracks empty directories. Since Git doesn't track empty directories by default, placing a .gitkeep
file in a directory tells Git to include the directory in version control.
Branches in GIT :
A branch in Git represents an independent line of development. When you create a branch, you make a copy of the code at a specific point in time. You can then make changes on this branch without affecting the main project (often the main
or master
branch). This allows multiple developers to work on different features simultaneously.
Some developers can work on the Header, some can work on the Footer, some can work on the Content, and some can work on the Layout. This is a good example of how branches can be used in git.
HEAD in Git :
HEAD is a symbolic reference to the current branch or commit that you're working on. It always points to the latest commit of the branch you're currently on.
- For example, if you're working on the
main
branch,HEAD
will point to the latest commit on themain
branch.
Creating a new branch :
git branch
git branch bug-fix
git switch bug-fix
git switch -c bug-fix
git checkout orange-mode
Some points to note:
git branch → This command will list all branches in the current repository.
git branch bug-fix → This command creates a new branch called
bug-fix
.git switch bug-fix → This command switches to the
bug-fix
branch.git switch -c bug-fix → This command creates a new branch called
dark-mode
. the-c
flag is used to create a new branch.git checkout orange-mode → This command switches to the
orange-mode
branch ( make sure the orange-mode branch exists in your repository ).
NOTE: Commit before switching to a branch
Merging branches :
In Git, merging branches is the process of combining changes from one branch into another.
Fast-Forward Merge:
→ A Fast-Forward Merge is a type of merge in Git that occurs when the current branch (the one you're merging into) has not diverged from the branch you're merging. In simple terms, the target branch can be fast-forwarded to the tip of the feature branch because there are no new commits on the target branch after the feature branch is created.
For a fast-forward merge to be possible:-
→ The current branch (e.g.,
main
) must be behind the branch you're merging (e.g.,feature-branch
).→ The target branch (e.g.,
main
) must not have any new commits since the branch you're merging was created. Essentially, themain
branch is a direct ancestor of thefeature-branch
.→ In this case, Git does not need to create a merge commit. Instead, it simply moves the pointer of the target branch (e.g.,
main
) forward to point to the latest commit on the feature branch.Fast-Forward Merge (No Commit Required):
→ If
main
has not diverged, Git will simply move themain
branch pointer forward to the tip of thefeature-branch
. This happens automatically, and no new commit is created.→ Before Merge:
A --- B (main) \ C --- D (feature-branch)
→ After Fast-Forward Merge:
A --- B --- C --- D (main, feature-branch)
The
main
branch is now atD
, and no new merge commit is created. You simply run:git checkout main git merge feature-branch
Git performs the fast-forward and completes the merge without any additional commits.
Not Fast-Forward Merge ( Three-Way Merge ):
→ If main
has diverged (i.e., it has new commits that feature-branch
does not have), Git cannot fast-forward. Instead, it will perform a three-way merge, requiring a merge commit to reconcile the differences between the two branches.
→ Before Merge:
A --- B --- E (main)
\
C --- D (feature-branch)
→ After Three-Way Merge:
A --- B --- E --- M (main)
\ /
C --- D (feature-branch)
M
is the new merge commit created to combine the changes from main
and feature-branch
.
To perform this merge, Git will automatically create the merge commit:
git checkout main
git merge feature-branch
what is the difference between fast-forward and not fast-forward merge?
The difference is resolving the conflicts. In a fast-forward merge, there are no conflicts. But in a not fast-forward merge, there are conflicts, and there are no shortcuts to resolve them. You have to manually resolve the conflicts. Decide, what to keep and what to discard. VSCode has a built-in merge tool that can help you resolve the conflicts.
Managing conflicts :
There is no magic button to resolve conflicts. You have to manually resolve the conflicts. Decide, what to keep and what to discard. VSCode has a built-in merge tool that can help you resolve the conflicts.
Rename a branch :
You can rename a branch using the following command:
git branch -m <old-branch-name> <new-branch-name>
Delete a branch :
You can delete a branch using the following command:
git branch -d <branch-name>
Check out a Branch :
You can checkout a branch using the following command:
git checkout <branch-name>