30 Essential Git and GitHub Interview Questions: From Beginner to Advanced

Git and GitHub Interview Questions:

  1. 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.
  2. 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.
  3. 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.
  4. 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).
  5. How do you create a new branch in Git?
    • You can create a new branch using the command git branch <branch-name>.
  6. 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 by git commit.
  7. 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.
  8. How can you undo a commit in Git?
    • You can undo a commit using git reset --soft HEAD~1 to keep changes staged or git reset --hard HEAD~1 to discard changes.
  9. 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.
  10. How do you clone a Git repository?
    • You can clone a Git repository using the command git clone <repository-url>.

Intermediate Level:

  1. What is the difference between git fetch and git pull?
    • git fetch retrieves updates from the remote repository without merging, while git pull fetches updates and merges them into the current branch.
  2. How do you view the commit history in Git?
    • You can view the commit history using the command git log.
  3. 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.
  4. 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".
  5. 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.
  6. 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.
  7. 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.
  8. How do you squash commits in Git?
    • You can squash commits using the command git rebase -i HEAD~<number-of-commits> and then selecting squash for the commits you want to combine.
  9. What is the difference between git rebase and git merge?
    • git merge combines changes from one branch into another, creating a merge commit, while git rebase applies changes on top of another branch, rewriting the commit history.
  10. What is GitHub Actions?
    • GitHub Actions is a CI/CD feature that allows you to automate workflows directly in your GitHub repository.

Advanced Level:

  1. 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.
  2. 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>.
  3. 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.
  4. 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 or git push origin --delete <branch-name> for a remote branch.
  5. 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.
  6. How can you view changes between two commits?
    • You can view changes between two commits using the command git diff <commit1> <commit2>.
  7. 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.
  8. 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.
  9. What is a Git submodule?
    • A Git submodule is a repository nested inside another Git repository, allowing you to include and manage external repositories.
  10. 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.