Git and GitHub Interview Questions:
- What is Git, and how does it differ from other version control systems?
- Git is a distributed version control system that allows multiple developers to work on the same project without interfering with each other’s changes.
- What is a Git repository?
- A Git repository is a directory that contains all the project files and the history of changes made to those files.
- How do you initialize a new Git repository?
- You can initialize a new Git repository by running the command
git init
in the project directory.
- You can initialize a new Git repository by running the command
- What is the difference between a Git branch and a Git tag?
- A branch is a movable pointer to a commit, allowing for parallel development, while a tag is a fixed pointer used to mark specific commits (e.g., releases).
- How do you create a new branch in Git?
- You can create a new branch using the command
git branch <branch-name>
.
- You can create a new branch using the command
- What is a merge conflict in Git, and how can you resolve it?
- A merge conflict occurs when two branches have competing changes. You can resolve it by manually editing the conflicting files and using
git add
followed bygit commit
.
- A merge conflict occurs when two branches have competing changes. You can resolve it by manually editing the conflicting files and using
- What does the command
git status
do?- The
git status
command shows the current state of the working directory and staging area, including changes that are staged, unstaged, or untracked.
- The
- How can you undo a commit in Git?
- You can undo a commit using
git reset --soft HEAD~1
to keep changes staged orgit reset --hard HEAD~1
to discard changes.
- You can undo a commit using
- What is the purpose of the
.gitignore
file?- The
.gitignore
file specifies which files or directories should be ignored by Git and not tracked in the repository.
- The
- How do you clone a Git repository?
- You can clone a Git repository using the command
git clone <repository-url>
.
- You can clone a Git repository using the command
Intermediate Level:
- What is the difference between
git fetch
andgit pull
?git fetch
retrieves updates from the remote repository without merging, whilegit pull
fetches updates and merges them into the current branch.
- How do you view the commit history in Git?
- You can view the commit history using the command
git log
.
- You can view the commit history using the command
- What is the purpose of Git tags?
- Git tags are used to mark specific points in the commit history, typically for releases, to easily reference them in the future.
- How can you change the last commit message?
- You can change the last commit message using the command
git commit --amend -m "New commit message"
.
- You can change the last commit message using the command
- What is the purpose of the
git stash
command?- The
git stash
command temporarily saves changes that are not ready to be committed, allowing you to switch branches without losing your work.
- The
- How do you resolve a merge conflict?
- You can resolve a merge conflict by editing the conflicting files to select the desired changes, then staging and committing the resolved files.
- What is a pull request (PR) on GitHub?
- A pull request is a request to merge changes from one branch into another, allowing for code review and discussion before the merge.
- How do you squash commits in Git?
- You can squash commits using the command
git rebase -i HEAD~<number-of-commits>
and then selectingsquash
for the commits you want to combine.
- You can squash commits using the command
- What is the difference between
git rebase
andgit merge
?git merge
combines changes from one branch into another, creating a merge commit, whilegit rebase
applies changes on top of another branch, rewriting the commit history.
- What is GitHub Actions?
- GitHub Actions is a CI/CD feature that allows you to automate workflows directly in your GitHub repository.
Advanced Level:
- What is the purpose of the Git reflog?
- The Git reflog tracks updates to the tips of branches, allowing you to recover lost commits or branches.
- How do you create and switch to a new branch in one command?
- You can create and switch to a new branch using the command
git checkout -b <branch-name>
.
- You can create and switch to a new branch using the command
- What is the significance of the
HEAD
pointer in Git?- The
HEAD
pointer represents the current commit in the checked-out branch, indicating where you are in the repository’s history.
- The
- How do you delete a branch in Git?
- You can delete a branch using the command
git branch -d <branch-name>
for a local branch orgit push origin --delete <branch-name>
for a remote branch.
- You can delete a branch using the command
- What is the GitHub flow?
- GitHub flow is a branching model that encourages developers to create feature branches for new features or bug fixes, use pull requests for merging, and deploy from the main branch.
- How can you view changes between two commits?
- You can view changes between two commits using the command
git diff <commit1> <commit2>
.
- You can view changes between two commits using the command
- What is the purpose of the
git cherry-pick
command?- The
git cherry-pick
command allows you to apply the changes from a specific commit onto your current branch.
- The
- How do you manage multiple remotes in Git?
- You can manage multiple remotes by adding them with
git remote add <name> <url>
and using the remote name with Git commands.
- You can manage multiple remotes by adding them with
- What is a Git submodule?
- A Git submodule is a repository nested inside another Git repository, allowing you to include and manage external repositories.
- How do you revert a pushed commit in Git?
- You can revert a pushed commit using the command
git revert <commit-hash>
, which creates a new commit that undoes the changes.
- You can revert a pushed commit using the command
![Sarath Tamminana](https://i0.wp.com/enminto.com/wp-content/uploads/2021/05/Profile-Image-e1621598995164.jpg?resize=100%2C100&ssl=1)
The founder of TacoBIG.com.He is a Cloud Architect from Bangalore interested in contributing guidance to Cloud related communities. He loves to read books and share knowledge with others. He is keen on understanding Financial wisdom and sharing thoughts on how to achieve financial freedom.