Understanding Git and GitHub: A Guide for Beginners

Understanding Git and GitHub: A Guide for Beginners

Chiranjibi Sahu
Chiranjibi Sahu

Share it on

Introduction

Git is a powerful and widely-used version control system that allows developers to track changes in their code over time. GitHub is a web-based platform built around Git that provides hosting for Git repositories, as well as additional features for project management, collaboration, and code review. This guide will help you understand what Git is, how it works, and how to use it effectively.

What is Git?

Git is a distributed version control system created by Linus Torvalds in 2005. It enables multiple developers to work on the same project simultaneously without overwriting each other’s changes. Git tracks changes to files and helps maintain a detailed history of modifications made to the codebase. This makes it easy to revert to previous versions, identify who made specific changes, and collaborate efficiently with others.

How Git Works

Git operates by creating snapshots of your files at specific points in time, known as commits. Each commit contains the state of your files at that moment and a message describing the changes made. Commits are linked together in a chain, forming a history of the project’s evolution. Git also supports branching, allowing developers to create independent lines of development. This enables multiple features or bug fixes to be worked on simultaneously without interfering with each other.

History of Git

  • 2005: Linus Torvalds created Git to manage the development of the Linux kernel.
  • 2007: GitHub was launched, providing a web-based interface for Git repositories.
  • 2012: GitHub became the largest host of source code in the world.
  • 2018: Microsoft acquired GitHub, further integrating Git into the software development ecosystem.

Basic Git Commands

Initialization and Configuration

 git init : Initialize a new Git repository in the current directory.
 git config --global user.name "Your Name" : Set the global username for all your commits.
 git config --global user.email "your.email@example.com" : Set the global email address for all your commits.

Working with Repositories

 git clone <repository_url> : Clone a repository from a remote server to your local machine.
 git status : Check the status of your files in the working directory and see which changes are staged for commit.
 git add <file> : Add a file to the staging area, preparing it for commit.
 git commit -m "Commit message" : Commit the staged changes with a descriptive message.
 git push : Push your commits to the remote repository.
 git pull : Fetch and merge changes from the remote repository to your local branch.

Branching and Merging

 git branch : List all branches in the repository.
 git branch <branch_name> : Create a new branch with the specified name.
 git checkout <branch_name> : Switch to the specified branch.
 git merge <branch_name> : Merge the specified branch into the current branch.

Viewing History

 git log : View the commit history with details of each commit.
 git log --oneline : View the commit history in a condensed format.
 git diff : Show changes between commits, branches, or the working directory.

Undoing Changes

 git reset <file> : Unstage a file, keeping the changes in the working directory.
 git checkout -- <file> : Discard changes in the working directory.
 git revert <commit> : Create a new commit that reverses the specified commit.

Advanced Git Commands

Stashing and Cleaning

 git stash : Save your local modifications temporarily and revert to the latest commit.
 git stash apply : Reapply the stashed changes to your working directory.
 git clean -f : Remove untracked files from the working directory.

Rebasing

 git rebase <branch_name> : Reapply commits on top of another base branch, creating a linear history.
 git rebase --interactive <commit> : Perform an interactive rebase, allowing you to edit, squash, or reorder commits.

Tags

 git tag <tag_name> : Create a lightweight tag.
 git tag -a <tag_name> -m "Message" : Create an annotated tag with a message.
 git push origin <tag_name> : Push a tag to the remote repository.

Submodules

 git submodule add <repository_url> : Add a submodule to your repository.
 git submodule update --init : Initialize and update submodules.

Git Hooks

Git hooks are scripts that run automatically in response to specific events in a Git repository. They can be used to enforce policies, run tests, or automate workflows. Common hooks include:

 pre-commit : Runs before a commit is made.
 pre-push : Runs before changes are pushed to a remote repository.
 post-merge : Runs after a successful merge.

Extra Features of Git

Git Bisect

Git bisect is a powerful tool for identifying the commit that introduced a bug. It uses a binary search algorithm to quickly narrow down the range of commits to inspect.

git bisect start
git bisect bad # Mark the current commit as bad
git bisect good <commit> # Mark an earlier commit as good

Git will check out a commit in the middle of the range, and you can test whether the bug is present. Based on your feedback, Git will narrow down the range until the offending commit is identified.

Git Worktrees

Git worktrees allow you to have multiple working directories associated with a single repository. This is useful for working on multiple branches simultaneously without needing to switch branches in the same directory.

git worktree add <path> <branch>

Git Flow

Git Flow is a branching model for Git that defines a standard workflow for managing features and releases. It introduces two main branches, develop and master, along with supporting branches for features, releases, and hotfixes.

Git LFS (Large File Storage)

Git LFS is an extension that improves the handling of large files in Git repositories. It stores large files outside the repository and replaces them with lightweight references.

git lfs install git lfs track "*.psd"

GitHub Features

GitHub enhances Git with a variety of features for collaboration and project management:

  • **Pull Requests**: Propose changes to a repository and review them before merging.
  • **Issues**: Track bugs, enhancements, and other tasks.
  • **Actions**: Automate workflows with CI/CD pipelines.
  • **Projects**: Organize tasks and track progress with project boards.
  • **Wiki**: Create and maintain documentation within the repository.

Conclusion

Git is an essential tool for modern software development, providing powerful version control and collaboration capabilities. GitHub builds on Git by offering a platform for hosting repositories and additional tools for managing projects and collaborating with others. By understanding and utilizing Git’s commands and features, you can effectively manage your codebase, work efficiently with your team, and take full advantage of the capabilities offered by Git and GitHub.

More Suggested Blogs