Git & GitHub
Git is a distributed version control system. This simple definition has two parts: distributed and version control. Let’s look into each one.
Version control
The Version control parts help us manage and track code changes over time.
Distributed
The distributed means that every developer system has complete copy of the code base including entire history of changes and information about who change what and when
Working Without Git: Manual Change Tracking
Although Git is a powerful tool for version control, it is technically possible to work without it by manually tracking changes. For instance, if we have a project named My_Project, we could create different folders for each version at every milestone. For example:
- My_Project_v1
- My_Project_v2
- My_Project_v3
Each folder would represent a snapshot of the project at a specific stage of development.
Issues with Manual Change Tracking
- Version Management Complexity
As the number of versions grows, managing them manually becomes increasingly complicated. It’s easy to lose track of what changes were made in each version. Naming conventions can become inconsistent (e.g., _v1.0.1, _final, _newest), and there’s no guarantee that collaborators will follow the same system. - Redundant Files and Wasted Space
With each new version, you’ll likely end up duplicating the entire project folder. This wastes storage space, especially in large projects with media files, libraries, or complex assets. - Difficulty in Merging Changes
If someone downloads the latest version of the project and starts adding new features or making fixes, combining their changes with your own can be tedious. Without Git, you’ll have to manually compare files side by side to identify differences. This process is slow, prone to human error, and risks overwriting important updates. - No Clear History of Changes
Without a version control system, there’s no clear record of who made changes, what the changes were, or why they were made. If a bug is introduced, it’s difficult to trace it back to a specific change. Git, by contrast, maintains a comprehensive history of changes, including commit messages that describe what was done and why. - Collaboration Issues
When multiple developers are working on the same project without Git, each person must manually download the latest version, make changes, and upload their modified version. If two people modify the same file, merging their changes becomes a manual, error-prone process. - Lack of Branching
Git allows you to create branches, enabling different features or versions to be developed simultaneously without affecting the main project. Manually managing this would require creating additional folder structures, which increases the complexity and potential for errors. - No Rollback or Undo
If you accidentally make an error in a manually tracked project, you cannot easily revert to a previous version or undo your mistake. Git, on the other hand, allows you to instantly revert to any previous state, ensuring that no work is permanently lost.
Git Environment Setup
To use Git on your system, you must download it based on your operating system, such as Windows, Linux, Unix, or macOS. After Git is installed, check the version using the following command
git --versionConfigure git with your Name and Email
It is important to configure Git with your name and email, as it helps track who made changes in the project.
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"What is a repository?
A repository, also known as a “repo,” is a place where Git tracks all the changes in your project. Think of a repository as a folder that stores all the versions of your code. Let’s create a new repository using terminal:
git init // This will create a new Git repository in the current directory.
// You will get the follwoing output
$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>At this stage, you won’t see any visible changes in the folder. The folder remains in its original state, but the git init command creates a hidden .git directory, which contains all the information Git needs to track changes.
Get Mbasamahmad’s stories in your inbox
Join Medium for free to get updates from this writer.
main is the default branch name of your repository created by Git. Every time you initialize a Git repository, this branch is automatically created for you.
A branch is essentially a parallel version of your project. It allows you to work on new features, bug fixes, or experiments independently from the main codebase. Each branch operates as a separate line of development, meaning changes in one branch won’t affect others. Once you’re satisfied with the updates in your branch, you can merge it back into the main branch, integrating your changes into the overall project. This makes branches a powerful tool for collaborative development, ensuring that new changes can be tested and refined before becoming part of the final product.
Track the changes
To track the changes in the project use the following command:
git statusFor example, if we create two files, hello.js and readme.md, in the folder where we initialized Git, and then run the git status command, you will see the following output:
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.js
readme.md
nothing added to commit but untracked files present (use "git add" to track)To track the files you git add command, so to track this use
git add readme.md
// If you want to track all files use .
git add .After adding a file using the git add command, we need to commit it. Committing a file means taking a snapshot of the project at a certain point in time. Committing means taking a snapshot of your entire project with a date and timestamp. In the future, if your project becomes corrupted, you can revert to a previous commit. Use the following command to commit your files:
git commit -m "Add read me files to the project"
// Output
master (root-commit) ee709a7] Add read me files to the project
1 file changed, 3 insertions(+)
create mode 100644 readme.mdThe -m option represents the commit message. After -m, write the message you want to include with the commit.
History in git
To see the history of the commit use git log command:
git log
commit a3026fd4ecc2554f2d8174561a064ac9c3c9bb94 (HEAD -> master)
Author: Basam <xyz@gmail.com>
Date: Mon Oct 7 15:25:09 2024 +0500
Two new files are added
commit ee709a7b406dfa68804cee36df31e80c89473310
Author: Basam <xyz@gmail.com>
Date: Mon Oct 7 15:20:17 2024 +0500
Add read me files to the project
Git history contains the commit ID or hash, which is automatically generated.
How to switch older commit and restore it?
To restore an older version of the project in Git, you need to copy the hash from the Git history using the git log command. After that use git checkout command and paste hash with like this
git checkout <hash>
git checkout a3026fd4ecc2554f2d8174561a064ac9c3c9bb94
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at a3026fd Two new files are addedA detached HEAD state in Git occurs when you check out a specific commit instead of a branch. HEAD is a reference to the current commit in your Git repository. It typically points to the latest commit on the current branch.
git checkout main
// To move it to main branchGit is used to track changes, whereas GitHub is a cloud platform that allows you to store your Git repository online and collaborate with others.
To change the master branch name to main use the following command:
git branch -M mainTo push the local repo to the remote repo use he following link
Conclusion
In this guide, we explored the fundamental concepts of Git and GitHub, including the importance of version control and the benefits of using a distributed version control system like Git. We discussed how manual change tracking can complicate version management and lead to issues such as redundancy, difficulty in merging changes, and a lack of clear change history.
We walked through the process of setting up Git, configuring user information, and creating a repository. Understanding branches and how they allow parallel development was emphasized, highlighting their significance in collaborative projects.
We also covered essential Git commands for tracking changes, committing files, viewing commit history, and restoring older versions of a project. Finally, we differentiated between Git and GitHub, explaining how GitHub provides a platform for online collaboration and storage of Git repositories.




