Git is one of the most widely used version control systems, and knowing how to use it efficiently is essential for developers. If you’re preparing for a job interview, expect Git-related questions to come up, whether you’re a beginner or an experienced professional. In this detailed guide, we’ll explore the most commonly asked Git interview questions along with in-depth explanations and accurate answers.
Introduction to Git
What is Git?
Git is a distributed version control system (DVCS) that allows multiple developers to collaborate on a project by tracking changes in files. Unlike traditional version control systems, Git enables decentralized development, making it possible to work offline and merge changes seamlessly.
What is the Difference Between Git and GitHub?
Many beginners confuse Git with GitHub, but they serve different purposes:
- Git: A version control system that runs locally on a developer’s computer.
- GitHub: A cloud-based hosting platform that allows developers to store, share, and collaborate on Git repositories.
Git Basics
How to Check the Status of a Git Repository?
To check the status of files in your repository, use:
git status
// Note - Selected File format is Bash, not ideal but it's ok
This command provides information on modified, staged, and untracked files.
How to Initialize a New Git Repository?
To create a new Git repository in a project directory, use:
git init
This creates a .git
directory where Git stores version control information.
How to Clone a Remote Repository?
To copy a remote repository to your local machine, use:
git clone <repository-url>
This command downloads the entire repository and history.
Branching and Merging
How to Create and Switch to a New Branch?
To create a new branch and switch to it, use:
git checkout -b <branch-name>
Alternatively, you can create and switch separately:
git branch <branch-name>
git checkout <branch-name>
How to Merge Branches?
Switch to the branch you want to merge changes into, then run:
git commit -m "Descriptive commit message"
If there are conflicts, Git will prompt you to resolve them before completing the merge.
What is a Merge Conflict and How to Resolve It?
A merge conflict occurs when two branches modify the same part of a file differently. To resolve:
- Open the conflicted file.
- Manually edit the conflicting sections.
- Run:
git add <file> git commit -m "Resolved merge conflict"
Committing Changes
How to Add Files to the Staging Area?
To stage specific files:
git add <file-name>
To stage all changes:
git add .
How to Commit Changes?
To save staged changes:
git commit -m "Descriptive commit message"
How to Undo the Last Commit?
- Keep changes but remove commit:
git reset --soft HEAD~1
- Remove commit and changes:
git reset --hard HEAD~1
Remote Collaboration
How to Push Changes to a Remote Repository?
git push origin <branch-name>
How to Pull the Latest Changes from a Remote Repository?
git pull origin <branch-name>
How to Create a Pull Request?
- Push changes to GitHub:
git push origin <branch-name>
- Go to GitHub, open the repository, and click “New Pull Request.”
- Select branches and submit the PR.
How to Delete a Branch?
- Delete a local branch:
git branch -d <branch-name>
- Delete a remote branch:
git push origin --delete <branch-name>
Advanced Git
Difference Between git merge
and git rebase
git merge
: Combines two branches and keeps the commit history.git rebase
: Moves commits from one branch on top of another, creating a linear history.
How to Stash Changes in Git?
To temporarily save changes without committing:
git stash
To reapply stashed changes:
git stash apply
To remove a stash:
git stash drop
How to Revert a Commit Without Deleting History?
git revert <commit-hash>
This creates a new commit that undoes the previous changes.
How to View Commit History?
git log
Bonus
What is .gitignore
?
A .gitignore
file specifies files and directories that should be ignored by Git.
node_modules/
*.log
.env
What is git cherry-pick
?
This command applies a specific commit from one branch to another:
git cherry-pick <commit-hash>
What is git bisect
?
A tool to find a commit that introduced a bug:
git bisect start
git bisect bad
git bisect good <commit-hash>
How to Change the Last Commit Message?
git commit --amend -m "New commit message"
What is git reflog
Used For?
Tracks all changes in HEAD, even undone ones:
git reflog
Conclusion
Mastering Git is essential for every developer. Whether you’re preparing for an interview or improving your workflow, understanding Git commands and best practices will make you a more efficient and reliable team member. The best way to solidify your Git knowledge is through hands-on practice, so try these commands in a real repository to gain confidence..!
By following this guide, you’ll be well-prepared to answer Git interview questions and showcase your version control expertise.