1. What is Git?
Git is a distributed version control system that tracks changes in source code and allows multiple developers to work on the same project without overwriting each other’s work.
2. How is Git different from GitHub?
Git is the tool used for version control on your local machine, while GitHub is a cloud platform that hosts Git repositories and provides collaboration features like pull requests, issues, CI/CD, and code reviews.
Example (if required):
- Git = MS Word (the tool)
- GitHub = Google Drive (where the file is stored and shared)
3. What is Version Control? Why do we need it?
Version Control is a system that tracks changes made to files over time, allowing you to restore previous versions, compare changes, and collaborate safely. We need version control to avoid losing work, track history, work collaboratively, and manage multiple versions of code without conflicts. It ensures safe, organized, and traceable development across teams.
4. What is a repository?
git init7. What does git clone do?
git clone is used to create a full local copy of a remote Git repository, including all files, commit history, branches, and tags. It essentially downloads the project from platforms like GitHub or GitLab to your machine so you can start working on it.
When you run:
git clone <repository-url>
git pull and git push.In short, git clone is used when you want to start working on an existing project hosted remotely.
8. What is the difference between git clone and git pull?
- git fetch (downloads changes)
- git merge (merges them into your current branch)
9. What is a commit?
A commit in Git is a snapshot of your project’s files at a specific point in time.
Whenever you make changes and run git commit, Git records:
- The exact state of your files
- Who made the change
- When it was made
- A message describing why the change was made
Commits form the history of your project, allowing you to go back to earlier versions, compare changes, and understand how the code evolved.
Each commit has a unique SHA-1 hash, which acts like an ID for that snapshot.
10. What is the staging area in Git?
The staging area (also called the index) is a place where Git collects and organizes your changes before creating a commit.
When you modify files, they don’t go directly into a commit.
You first add them to the staging area using:
git add <file>
- Choose which changes you want to include in the next commit
- Group related changes together
- Avoid committing accidental or incomplete changes
The staging area acts as a buffer between your working directory and the final commit.
It gives developers control and ensures each commit is clean, meaningful, and intentional.
11. Explain the Git file lifecycle (untracked → tracked → staged → committed).
git add <file>
Tracked files can be unmodified, modified, or staged.
git commit -m "message"
- Untracked: Git doesn’t know about the file.
- Tracked: Git knows the file exists.
- Staged: File is ready to be committed.
- Committed: File changes are permanently stored in Git history.
12. What do git add . and git add -A do?
- New files
- Modified files
- New files
- Modified files
- Deleted files
- git add . → add new + modified files (no deletions)
- git add -A → add new + modified + deleted files (complete sync)
13. What does git status show?
- “Use git add to stage changes.”
- “Use git restore to discard.”
14. What does git log do?
- Commit hash (SHA) → unique ID of the commit
- Author → who made the commit
- Date → when the commit was made
- Commit message → why the change was made
commit 8f3a2c... Author: John Doe Date: Tue Dec 2 Added login validation
15. What is .gitignore? Why is it used?
It is used to keep your repository clean and free from files that should not be version-controlled, such as:
- Temporary or cache files (e.g.,
dist/,temp/) - Build outputs and compiled code (
bin/,obj/) - Dependency folders (e.g.,
node_modules/) - Sensitive files (API keys, credentials)
- IDE/system-specific files (
.vscode/,.DS_Store)
Using .gitignore prevents unnecessary or private files from being committed, reducing repo size and avoiding security risks.
16. How to remove a file from Git but keep it locally?
To remove a file from Git’s tracking without deleting it from your local system, you use:
git rm --cached <file>
After running this, you commit the change:
git commit -m "Stop tracking file"
.gitignore.17. What is a branch in Git?
- main or master → stable production-ready code
- feature/login → new login feature
- bugfix/payment → fix for payment issue
18. How to create a branch?
19. Difference between git merge and git rebase.
- git merge: When you merge a branch, Git combines the commit histories of two branches and creates a merge commit.
- git rebase: Rebase takes your commits and reapplies them on top of another branch, as if you created them from there originally.
A merge conflict happens when Git is unable to automatically combine changes from two branches because both branches modified the same part of the same file, or one branch deleted something that the other branch edited.
Git doesn’t know which version is correct, so it stops the merge and asks the developer to resolve it manually.
During a merge conflict, Git marks the conflicting sections in the file using:
<<<<<< HEAD Your changes ====== Other branch changes >>>>>> branch-name
21. What is a detached HEAD state?
A detached HEAD state happens when your HEAD pointer (which normally points to the latest commit of a branch) points directly to a specific commit, tag, or old revision instead of a branch.
In this state, you are not on any branch.
You are simply viewing or modifying code at a past commit.
Example of entering detached HEAD:
git checkout a7c9d12
22. What is git checkout used for?
git checkout is used to switch branches or restore files, making it one of the most essential commands in Git.- git switch → for switching branches
- git restore → for restoring files
23. What is a feature branch workflow?
A Feature Branch Workflow is a Git development strategy where each new feature, enhancement, or bug fix is developed in its own separate branch instead of directly in the main or develop branch.
The goal is to isolate work so developers can build, test, and review changes without affecting the stable codebase.
24. Difference between git pull and git fetch?
git fetch
git merge origin/main
- When you want to see what others have changed without affecting your code
- When you want full control before merging
- Fetch latest changes
- Merge them into your current branch
git pull
- When you want the latest code quickly
- When you trust the remote changes and don’t need review
- git fetch = safely download changes without touching your work.
- git pull = download + merge changes into your current branch.
25. How do you undo the last commit?
git reset --soft HEAD~1
git reset HEAD~1
git reset --hard HEAD~1
git revert HEAD
- Use reset to undo local commits (soft/mixed/hard).
- Use revert to undo a commit that is already pushed and shared.
26. Difference between git revert and git reset.
git revert <commit-id>
git reset --hard HEAD~1
- Use git revert to safely undo a pushed commit without breaking history.
- Use git reset to rewrite or clean up local commit history before pushing.
27. What is a soft, mixed, and hard reset?
29. How to squash commits?
Squashing commits means combining multiple commits into a single commit so that your Git history looks clean and meaningful (for example, before merging a feature branch).
The most common way to squash commits is to use an interactive rebase.
Steps to squash the last N commits
Suppose you want to squash the last 3 commits into one:
git rebase -i HEAD~3
pick a1b2c3 First commit
pick d4e5f6 Second commit
pick 789abc Third commit
pick a1b2c3 First commit
squash d4e5f6 Second commit
squash 789abc Third commit
- pick → keep this commit as is
- squash (or s) → combine this commit into the one above
30. What does git stash do?
git stash temporarily saves your uncommitted changes (both staged and unstaged) and cleans your working directory, allowing you to switch branches or pull updates without losing your work.
It’s like putting your work on a temporary shelf.
You use it when you need to:
- Switch branches quickly
- Pull the latest test code
- Work on an urgent bug fix
- Pause your current work without committing half-done changes
Example scenario:
You're working on feature/login, but suddenly need to fix a production bug on main.
Your changes aren’t complete, so you can't commit them.
- git stash → saves changes and cleans the working directory
- git stash list → shows stashed items
- git stash apply → restores the most recent stash
- git stash drop → removes the stash
- git stash pop → restores + deletes the stash in one step
31. Fork vs Clone vs Branch.
32. What happens during git pull --rebase?
- git fetch: Git first fetches the latest changes from the remote repository and updates the remote-tracking branch (e.g., origin/main). These changes are downloaded but not yet integrated into your local working branch.
- git rebase: After fetching, Git then takes your local commits (those that are not yet pushed to the remote) and temporarily "puts them aside." It then "rewinds" your local branch to the point where it diverged from the remote-tracking branch. Finally, it reapplies your local commits one by one on top of the newly fetched remote changes.
Trends is an amazing magazine Blogger theme that is easy to customize and change to fit your needs.