
Git and Github
Niket Girdhar / February 11, 2025
Version Control Overview
Version control systems (VCS) help track changes in source code, making it easier to recover older versions and collaborate effectively.
Types of Version Control
There are two major types of version control systems:
-
Centralized Version Control System (CVCS):
A single central repository is used for storing code. Examples include SVN, CVS, and Perforce. CVCS systems require an internet connection for most operations and commit changes directly to the central server. -
Distributed Version Control System (DVCS):
Each user has their own local repository. Examples include Git, Mercurial, and Bazaar. DVCS systems work offline, with changes committed locally and then pushed to a remote repository when online. It offers more flexibility and fault tolerance.
What is Git?
Git is a free and open-source DVCS that allows users around the world to work on the same project. Users can sync their changes with a remote server and collaborate without losing data. Git is the most popular VCS due to its distributed nature.
Key Features of Git
- Remote Access: Access and make changes to code remotely.
- Branching: Create branches for feature development, testing, and bug fixes.
- Commit: Record changes with a message describing the update.
What is GitHub?
GitHub is a web service that makes using Git easier. It provides hosting for Git repositories, with additional collaboration tools such as pull requests and issue tracking. Other alternatives include GitLab and Bitbucket.
Key Git and GitHub Terms
- SSH: A secure method for logging into remote computers.
- Repository: A storage location for project files and version history.
- Fork: Copy a repository to your account.
- Pull Request: Request to merge changes into a main branch for review.
- Branch: A separate line of development for testing or new features.
- Clone: Create a copy of a repository on your local machine.
- Merge: Combine changes from one branch to another.
Git Workflow
- Clone Repo: Copy the remote repository to your local machine.
- Create Branch: Work on a new branch to test or develop new features.
- Staging Area: Select files to be included in the commit.
- Commit: Save changes with a message describing the update.
- Push: Upload commits to the remote repository.
- Pull Request: Request to review changes before merging.
- Merge: After review, merge the changes into the main branch.
Basic Git Commands for daily used
Initializing a Git Repository
git init # Creates a new repository and sets up all the necessary files and data structures for version control.
Adding Files to Staging Area
git add <filename> # Adds a specific file from the working directory to the staging area.
git add . # Adds all modified and new files from the working directory to the staging area.
git add -A # Stages all changes (including deletions) from the working directory.
Committing Changes
git commit -m "commit message" # Takes a snapshot of the changes in the staging area and stores it with a descriptive message.
Cloning a Repository
git clone <repository URL> # Creates a local copy of a remote repository.
Pushing Local Changes to Remote Repository
git push origin <branch-name> # Pushes local commits to the specified remote branch.
With Git, collaboration becomes simpler and more flexible, allowing multiple developers to work on the same project with ease.