Mastering Git: A Comprehensive Guide to Git and GitHub

Sejal Shinde (MIT AOE Student)
6 min readMay 19, 2024

--

What is Git?

Git is a distributed version control system (DVCS) used for tracking changes in source code during software development. Created by Linus Torvalds in 2005, Git has become one of the most widely used version control systems in the world.

At its core, Git allows multiple developers to collaborate on a project simultaneously by managing a history of changes to the codebase. Each developer can work on their own copy of the code (called a “branch”), make changes, and then merge those changes back into the main codebase as needed.

Key Features of Git:

  1. Branching and Merging: Git allows developers to create separate branches to work on specific features or fixes without affecting the main codebase. These branches can later be merged back into the main branch.
  2. Distributed Development: Every developer has their own local copy of the entire project repository, including its full history. This enables developers to work offline and independently, making Git suitable for distributed teams.
  3. Commit History: Git keeps a detailed record of every change made to the codebase, including who made the change, when it was made, and what was changed. This commit history is invaluable for tracking down bugs, understanding the evolution of the codebase, and reverting to previous versions if necessary.
  4. Staging Area: Git has a staging area where developers can review and selectively choose which changes to include in the next commit. This allows for more granular control over the commit process.
  5. Open Source and Extensible: Git is open source, meaning its source code is freely available for anyone to view, modify, and distribute. It also has a rich ecosystem of third-party tools and integrations that extend its functionality.

What is GitHub?

Built on top of the Git version control technology, GitHub is an online platform. It offers software development projects hosting together with capabilities like continuous integration, code review, problem tracking, and collaboration.

Here are some key features of GitHub:

  1. Repository Hosting: GitHub hosts Git repositories, allowing developers to store their code and collaborate with others. Each repository on GitHub is a central location where developers can push changes and pull updates.
  2. Collaboration Tools: GitHub provides tools for collaboration among developers working on the same project. These include features like pull requests, which allow developers to propose changes to the codebase, discuss them, and merge them into the main branch.
  3. Code Review: GitHub’s pull request system includes features for code review, such as inline comments and approval workflows. This helps maintain code quality by allowing developers to review each other’s code before it is merged into the main branch.
  4. Issue Tracking: GitHub includes a built-in issue tracking system, allowing developers to report bugs, suggest new features, and track tasks and enhancements. Issues can be assigned to specific developers, labeled for organization, and linked to specific commits or pull requests.
  5. Continuous Integration: GitHub integrates with various continuous integration (CI) services, allowing developers to automate the testing and deployment of their code. This helps ensure that changes to the codebase are tested thoroughly before being merged into the main branch.
  6. Community and Social Features: GitHub has a large and active community of developers, and it includes features for social interaction such as following other users, starring repositories, and contributing to open-source projects.

Who uses Git and GitHub?

Many different people and companies involved in software development use Git and GitHub. Git version control helps developers of all stripes effectively manage changes to their codebases, from novices to seasoned pros. On the other side, GitHub is used for hosting code repositories, managing issues, collaborating on projects, and facilitating code review by individual engineers, open-source projects, small teams, and major corporations. GitHub and Git offer useful tools and workflows to speed up the development process, regardless of whether you’re an organisation managing several projects, a team working on a software product, or a lone developer working on a personal project.

How to use Git and GitHub?

Step 1: To use Git you need to install Git on your local machine . Go to following website and install Git on your machine based on your operating system.

Step 2: As GitHub is an online platform you need to create an account on GitHub. Go to below site and create an account on GitHub

After completing these steps you are ready to use git and github .

You can go to the following video to learn how to use git on your local system for any project:

Branching strategy in Git

What is branch?

A branch in Git is a portable, lightweight pointer to a particular commit within a repository’s version history. Consider branches as distinct development paths that depart from the primary codebase, sometimes referred to as the “master” or “main” branch. Until they are ready to merge their changes, developers can work on experimentation, bug fixes, and new features in branches that don’t impact the main source.

In essence, making a branch makes a copy of the code at a specific moment in time. After making modifications to the code in their branch, developers can commit those changes and, if the changes are finished and tested, merge the branch back into the main codebase (or another branch).

Why we use branch?

Branches in Git are used for several reasons:

  1. Isolation of Changes: Branches allow developers to work on new features, bug fixes, or experiments in isolation from the main codebase. This isolation prevents unfinished or experimental changes from affecting the stability of the main codebase until they are ready.
  2. Parallel Development: Multiple developers can work on different features or fixes simultaneously by creating separate branches. This parallel development improves productivity and enables teams to deliver features faster.
  3. Risk-Free Experimentation: Branches provide a safe space for developers to experiment with new ideas or approaches without impacting the main codebase. If an experiment doesn’t work out, the changes can be discarded without affecting the rest of the project.
  4. Code Review: Branches facilitate code review workflows by allowing developers to create pull requests or merge requests for their changes. Code review can be done in the context of the branch before it is merged into the main codebase, ensuring that changes meet quality standards.
  5. Versioning and Release Management: Branches are often used to manage different versions or releases of a software project. For example, a “release” branch may contain the code for a stable version of the software, while development continues on separate feature branches.
  6. Collaboration: Branches enable collaboration among team members by providing a mechanism for sharing and reviewing changes. Team members can work on their own branches and collaborate on features or fixes before merging them into the main codebase.

Overall, branches are a powerful feature of Git that enable flexible and efficient software development workflows, allowing teams to manage changes, collaborate effectively, and maintain the stability and quality of their codebase.

Git Commands:

Configuration

1. Set user name and email:

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

2. To Check configuration:

git config --list

Repository Management

3. Initialize a new Git repository:

git init

4. Clone an existing repository:

git clone <repository-url>

Basic Snapshotting

5. Check the status of the repository:

git status

6. Add files to the staging area:

git add <file-name>       # Add a specific file
git add .

7. Commit changes:

git commit -m "Commit message"

8. Commit all changes (skip staging):

git commit -a -m "Commit message"

Branching and Merging

9. List branches:

git branch

10. Create a new branch:

git branch <branch-name>

11. Switch to a branch:

git checkout <branch-name>

12. Create and switch to a new branch:

git checkout -b <branch-name>

13. Merge a branch into the current branch:

git merge <branch-name>

14. Delete a branch:

git branch -d <branch-name>

Remote Repositories

15. Add a remote repository:

git remote add <remote-name> <remote-url>

16. Fetch changes from a remote repository:

git fetch <remote-name>

17. Push changes to a remote repository:

git push <remote-name> <branch-name>

18. Pull changes from a remote repository:

git pull <remote-name> <branch-name>

Undoing Changes

19. Unstage a file:

git reset <file-name>

20. Revert a commit

git revert <commit-hash>

21. Reset to a specific commit (destructive):

git reset --hard <commit-hash>

Viewing History

22. Show commit history:

git log

23. Show a specific commit:

git show <commit-hash>

24. Show changes (diff):

git diff

Thank you for joining me on this journey into the world of cloud and devops. Together, let’s continue to innovate, learn, and grow in the cloud and devops!

--

--