Git - pranavkumarpk01/MD-DevOps GitHub Wiki
Git & GitHub: Complete Guide
Git is a version control system that helps developers track changes in their codebase, collaborate with teams, and manage code history efficiently.
- Collaboration: Multiple developers can work on the same project simultaneously.
- Version Control: Keeps track of every change made to the code.
- Backup & Recovery: Easily revert to previous versions of the code.
- Branching & Merging: Developers can work on features separately and later merge them.
- Open Source Contribution: Helps in contributing to public repositories on GitHub.
Definition: This command initializes a new Git repository in a project directory.
git init
Example: If you have a new project and want to track changes using Git, navigate to the project folder and run git init
.
Definition: This command adds modified files to the staging area before committing them.
git add filename
Example: After modifying a file, use git add
to stage it before committing.
git add index.html
Use git add .
to add all changes in the directory.
Definition: Commits the staged changes with a message.
git commit -m "Added a new feature"
Example: Use commit after adding files to save changes with a meaningful message.
Definition: Shows the status of changes (staged, unstaged, untracked files).
git status
Example: Run this command to check which files are modified before committing.
Definition: Clones a remote repository to your local machine.
git clone https://github.com/user/repo.git
Example: Use this command to make a local copy of a GitHub repository.
Definition: Fetches and integrates changes from a remote repository.
git pull origin main
Example: Sync local repository with the latest changes from the remote repository.
Definition: Uploads local commits to a remote repository.
git push origin main
Example: Use git push
to upload your commits to GitHub or another remote repository.
Definition: Moves or combines a sequence of commits to a new base commit.
git rebase main
Example: Used when working on feature branches to keep them updated with the main branch.
Definition: Creates a new commit that undoes the changes of a previous commit.
git revert <commit-id>
Example: Use git revert
instead of git reset
to maintain history while undoing changes.
## Git Revert vs Git Rebase
Concept:
-
git revert
is used to undo a specific commit by creating a new commit that reverses the changes. - It preserves the commit history and does not rewrite the commit tree.
- Recommended for public/shared branches to avoid breaking history.
Scenario:
A developer accidentally commits broken code to main
. Instead of deleting the commit, they use:
git revert <commit-id>
A Pull Request (PR) is a request to merge changes from one branch into another.
It allows team members to review, discuss, and approve code before merging.
PRs help maintain code quality, prevent errors, and ensure proper documentation.
They are commonly used in collaborative development to manage contributions.
-
Code Review & Quality Assurance – PRs allow other developers to review and suggest improvements before merging.
-
Collaboration – Multiple team members can work on different features and merge them safely.
-
Avoid Direct Pushes to Main Branch – PRs ensure only reviewed code is merged, preventing accidental errors.
-
History & Documentation – PRs act as a record of changes and discussions about modifications.
-
Automated Testing & CI/CD Integration – PRs can trigger automated tests before merging to avoid breaking the application.
A branching strategy helps teams organize and manage their code efficiently by defining how and when to create, merge, and delete branches.
It ensures smooth collaboration, minimizes conflicts, and enables continuous integration and deployment.
-
Feature Branching
- Each new feature is developed in a separate branch.
- Once completed, the branch is merged into
main
ordevelop
. -
Scenario: A developer is working on a "dark mode" feature. They create a branch
feature/dark-mode
, work on it, and merge it intodevelop
after review.
-
Git Flow
- Uses multiple branches:
-
main
(production-ready code) -
develop
(integration branch for ongoing work) -
feature/*
(individual feature branches) -
release/*
(stabilization before deployment) -
hotfix/*
(critical fixes applied directly tomain
)
-
-
Scenario: A team is preparing a v1.1 release. They create a
release/v1.1
branch to finalize testing and documentation before merging it intomain
.
- Uses multiple branches:
-
Trunk-Based Development
- Developers work directly on
main
with short-lived feature branches. - Frequent small commits ensure continuous integration.
-
Scenario: A startup with a small team follows trunk-based development. Developers push changes to
main
daily, ensuring rapid deployment.
- Developers work directly on
-
Release Branching
- Dedicated branches for each release (
release/v1.0
,release/v2.0
). - Allows maintaining old versions while continuing development.
-
Scenario: A SaaS company maintains
release/v2.0
for bug fixes while developing new features indevelop
forv3.0
.
- Dedicated branches for each release (
-
Hotfix Branching
- Used for urgent fixes in production.
- A branch (
hotfix/fix-issue
) is created frommain
, fixed, tested, and merged back. -
Scenario: A security vulnerability is found in production. A
hotfix/security-patch
branch is created, tested, and merged intomain
.
Each strategy suits different team sizes and development workflows. Choosing the right approach ensures efficient collaboration and code stability.
Answer: Git is a distributed version control system used to track changes in code and collaborate with teams.
Answer: Git is a version control tool; GitHub is a hosting platform for Git repositories.
Answer: Using git init
in the project directory.
Answer: Stages files for commit.
Answer: Using git status
.
Answer: git pull
fetches and merges; git fetch
only fetches updates.
Answer: A snapshot of changes in the repository.
Answer: Using git revert
.
Answer: Using git branch branch-name
.
Answer: Using git checkout branch-name
or git switch branch-name
.
Answer: A conflict occurs when changes from different branches overlap.
Answer: By manually editing conflicted files and running git commit
.
Answer: To temporarily save changes without committing them.
Answer: git reset
removes commits, git revert
creates a new commit to undo changes.
Answer: Using git branch -m old-name new-name
.
Answer: A file that specifies untracked files Git should ignore.
Answer: A fork is a copy of a repo on GitHub; a clone is a local copy.
Answer: Using git remote -v
.
Answer: Using git branch -d branch-name
.
Answer: Using git rebase -i
.
Answer: Selectively applies a commit from another branch.
Answer: Helps find the commit that introduced a bug.
Answer: Using git log
.
Answer: Using git checkout commit-id -- filename
.
Answer: It ensures code quality before merging.
Please Refer Interview Questions -> Git_Interview_Questions