Mayank Singh
Git Basics and Workflow Guide
What is Git?
Git is a distributed version control system (VCS) that helps track changes in source code
during software development. It allows multiple developers to collaborate efficiently,
maintain versions of files, and revert to previous versions if needed.
Stages of Git
Mayank Singh
1. Working Directory: Where you work on files (write, edit, or delete).
2. Staging Area: A space where changes are temporarily saved before committing.
3. Local Repository: Where committed changes are stored locally on your machine.
4. Remote Repository: Central repository (e.g., GitHub, GitLab) that multiple
developers can access.
What is git init?
git init initializes a local Git repository in a project directory.
Backend Process:
Creates a hidden .git directory in the folder containing all metadata, configuration
files, and objects required to track changes.
Files Created:
.git/HEAD: Points to the current branch.
.git/index: Staging area metadata.
.git/config: Configuration for the repository.
.git/objects: Stores objects (commits, trees, blobs).
Workflow: Working Directory, Staging Area, Local Repo
Mayank Singh
1. Working Directory:
o Edit a file (e.g., file.txt).
o Changes remain untracked or modified until added to the staging area.
2. Staging Area (git add):
o git add file.txt moves the file from the working directory to the staging area.
3. Local Repository (git commit):
o git commit -m "message" saves the staged changes into the local repository
with a snapshot.
Example Workflow:
echo "Hello Git" > file.txt # Create file in Working Directory
git add file.txt # Move to Staging Area
git commit -m "Initial commit" # Save to Local Repository
Default Branch
When a repository is created, the default branch is usually main (formerly master in older
Git versions). It points to the latest commit in the local repository.
Mayank Singh
What is a Central Repository?
A central repository is a remote repository (hosted on platforms like GitHub or GitLab) used
for collaboration. It serves as a common location for developers to pull and push changes.
Pull and Push
Mayank Singh
Pull: Fetches updates from the remote repository to the local repository.
Push: Sends local commits to the remote repository.
Example:
git pull origin main # Sync local branch with remote
git push origin main # Upload commits to the remote repository
Version Control System (VCS)
A system to manage and track changes in files over time. It allows:
Collaboration among teams.
Tracking history and changes.
Mayank Singh
Reverting to previous versions.
Snapshot in Git
Git saves changes as snapshots of the entire project at a specific point in time. Each commit
is a snapshot of the files in the staging area.
Incremental Snapshot or Backup
An incremental snapshot only records the changes (delta) between the current and previous
commits, reducing storage usage.
Metadata in Git
Metadata is information about the repository, such as commit history, branch details, and
configuration stored in the .git directory.
Branches in Git
A branch is a separate line of development.
The master branch (now often main) is the default branch where stable code resides.
Mayank Singh
Workflow:
1. Create a branch:
git branch feature-branch
2. Switch to a branch:
git checkout feature-branch
3. Merge branches:
git merge feature-branch
4. Delete a branch:
git branch -d feature-branch