50+ Git/GitHub Interview Questions

date
May 22, 2024
slug
50+Git-Github-Interview-Questions
status
Published
tags
git
github
Interview
questions
summary
A comprehensive understanding of the essential concepts and advanced features of Git and GitHub.
type
Post

Basic Questions

  1. What is Git?
      • Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
  1. What is GitHub?
      • GitHub is a web-based platform that uses Git for version control and provides features like bug tracking, task management, and collaboration.
  1. What is a repository in Git?
      • A repository (or repo) is a storage location for software packages, where the project’s files and the history of changes to these files are kept.
  1. What are the basic Git commands?
      • git init: Initialize a new Git repository
      • git clone: Clone a repository
      • git add: Add files to the staging area
      • git commit: Commit changes to the repository
      • git status: Show the status of the working directory and the staging area
      • git push: Push changes to a remote repository
      • git pull: Fetch and integrate changes from a remote repository
  1. What is a commit in Git?
      • A commit is a snapshot of the changes in the repository. It is used to save your work and can include a message describing what has been changed.
  1. What is a branch in Git?
      • A branch is a parallel version of a repository. It allows you to work on different tasks without affecting the main codebase.
  1. How do you create a new branch in Git?
      • git branch <branch-name>: Creates a new branch
      • git checkout -b <branch-name>: Creates a new branch and switches to it
  1. How do you switch branches in Git?
      • git checkout <branch-name>: Switches to the specified branch
  1. What is a merge in Git?
      • A merge is a way to combine the changes from one branch into another. The git merge command is used to merge branches.
  1. What is a pull request?
      • A pull request is a way to submit changes to a project. It is a request for the repository owner to review and merge your changes into the main branch.

Intermediate Questions

  1. What is the difference between git fetch and git pull?
      • git fetch downloads changes from a remote repository but does not integrate them into the current branch. git pull fetches changes and then merges them into the current branch.
  1. What is a conflict in Git and how do you resolve it?
      • A conflict occurs when changes from different branches clash. To resolve it, you need to manually edit the conflicting files, mark them as resolved using git add, and then commit the changes.
  1. How do you discard local changes to a file?
      • git checkout -- <file>: Discards changes in the working directory
      • git reset HEAD <file>: Unstages the file but keeps the changes in the working directory
  1. What is git rebase and how is it different from git merge?
      • git rebase moves or combines a series of commits to a new base commit. Unlike git merge, which creates a new merge commit, rebase rewrites the commit history.
  1. What are Git hooks?
      • Git hooks are scripts that run automatically at specific points in the Git workflow, such as before a commit or after a merge.
  1. What is the staging area in Git?
      • The staging area is where you place changes you want to commit. It allows you to prepare and review changes before they are committed.
  1. How do you revert a commit in Git?
      • git revert <commit>: Creates a new commit that undoes the changes of a previous commit
      • git reset --hard <commit>: Resets the current branch to a specified commit and discards all changes in the working directory and staging area (use with caution)
  1. What is .gitignore and how does it work?
      • .gitignore is a file that specifies which files and directories to ignore in a repository. Git will not track changes to files listed in .gitignore.
  1. How do you tag a commit in Git?
      • git tag <tagname> <commit>: Creates a tag for a specific commit
      • git push origin <tagname>: Pushes the tag to the remote repository
  1. What is git stash?
      • git stash temporarily saves changes in the working directory that are not ready to be committed, allowing you to switch branches or work on something else.

Advanced Questions

  1. What is a detached HEAD in Git?
      • A detached HEAD state occurs when you checkout a commit that is not an actual branch. You can still make changes, but they won't belong to any branch.
  1. How do you handle large binary files in Git?
      • Git LFS (Large File Storage) is used to handle large binary files by storing them outside the regular Git repository and replacing them with text pointers inside Git.
  1. What is a submodule in Git?
      • A submodule is a repository embedded inside another repository. It allows you to keep a Git repository as a subdirectory of another Git repository.
  1. What is a Git workflow?
      • A Git workflow is a set of guidelines for using Git in a particular way to streamline collaboration and manage code effectively. Examples include Git Flow, GitHub Flow, and GitLab Flow.
  1. How do you change the commit message of a previous commit?
      • git commit --amend: Allows you to change the message of the last commit
      • For older commits, you can use git rebase -i <commit> and then edit the commit message during the interactive rebase.
  1. What is the difference between git rebase and git cherry-pick?
      • git rebase re-applies commits from one branch onto another, whereas git cherry-pick applies a specific commit from one branch to another.
  1. What are the different types of Git objects?
      • Commit, tree, blob, and tag are the four types of objects in Git, representing the various elements in a Git repository.
  1. How do you configure Git to handle line endings?
      • git config --global core.autocrlf true: Converts LF to CRLF on checkout and CRLF to LF on commit
      • git config --global core.autocrlf input: Converts CRLF to LF on commit, but not on checkout
  1. How do you squash commits in Git?
      • Use git rebase -i <commit> to interactively rebase and squash commits together into a single commit.
  1. What is Git bisect?
      • git bisect is a binary search algorithm to find the commit that introduced a bug by automatically testing commits between a known good and a bad commit.

GitHub-Specific Questions

  1. How do you fork a repository on GitHub?
      • Go to the repository page on GitHub and click the "Fork" button in the top right corner. This creates a copy of the repository under your GitHub account.
  1. How do you create a pull request on GitHub?
      • After pushing your changes to a branch on your forked repository, go to the original repository and click "New pull request." Choose the branch you want to merge and create the pull request.
  1. How do you resolve a merge conflict in a pull request on GitHub?
      • You can use the GitHub interface to manually resolve conflicts by editing the conflicting files, marking them as resolved, and committing the changes.
  1. What are GitHub Actions?
      • GitHub Actions is a CI/CD platform that allows you to automate workflows, build, test, and deploy your code directly from GitHub.
  1. How do you protect a branch on GitHub?
      • Go to the repository settings, click on "Branches," then "Branch protection rules," and set the rules for the branch you want to protect, such as requiring pull request reviews or passing status checks.
  1. What is GitHub Pages?
      • GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files from a repository on GitHub and publishes a website.
  1. How do you add collaborators to a repository on GitHub?
      • Go to the repository settings, click on "Manage access," and then "Invite a collaborator" to add collaborators by their GitHub username or email.
  1. What is a GitHub Gist?
      • A GitHub Gist is a simple way to share snippets or small pieces of code with others. Gists can be public or secret.
  1. How do you close an issue using a commit message on GitHub?
      • Include keywords like "fixes #issue-number" or "closes #issue-number" in the commit message to automatically close the issue when the commit is merged.
  1. What is GitHub Sponsors?
      • GitHub Sponsors is a program that allows developers to receive financial support for their work on open source projects.

Expert-Level Questions

  1. What is the difference between git stash pop and git stash apply?
      • git stash pop applies the stashed changes and then deletes the stash, whereas git stash apply applies the stashed changes without deleting the stash.
  1. How do you use git reflog?
      • git reflog shows a log of all the changes to the tip of branches and other references, allowing you to recover lost commits or changes.
  1. What is the use of git fsck?
      • git fsck verifies the connectivity and validity of objects in the database. It can detect corrupted or missing objects in the repository.
  1. How do you create a patch from a commit?
      • git format-patch <commit>: Creates a patch file from a commit
      • git apply <patch-file>: Applies the patch to your repository
  1. What is git cherry?
      • git cherry shows which commits from one branch are not in another branch, helping to identify unique commits.
  1. How do you reset a single file to a specific commit?
      • git checkout <commit> -- <file>: Resets the file to the state it was in at a specific commit
  1. How do you clean up unnecessary files in a repository?
      • git clean -f: Removes untracked files from the working directory
      • git clean -fd: Removes untracked files and directories
  1. What is git blame?
      • git blame <file>: Shows what revision and author last modified each line of a file
  1. How do you rebase a branch onto another branch?
      • git checkout <branch>: Switch to the branch you want to rebase
      • git rebase <target-branch>: Rebase the current branch onto the target branch
  1. What is the difference between git log and git reflog?
      • git log shows the commit history of the repository, whereas git reflog shows a log of changes to the tip of branches and references.
  1. How do you filter commits in git log?
      • git log --author=<name>: Filters commits by author
      • git log --since=<date>: Filters commits since a specific date
      • git log --grep=<pattern>: Filters commits by a commit message pattern
  1. How do you change the URL of a remote repository?
      • git remote set-url origin <new-url>: Changes the URL of the remote repository
  1. What is the purpose of git gc?
      • git gc (garbage collection) cleans up unnecessary files and optimizes the local repository by compressing file history and removing unreachable objects.
  1. How do you compare branches in Git?
      • git diff <branch1>..<branch2>: Shows the differences between two branches
  1. What is the use of git config?
      • git config is used to configure Git settings, such as user name, email, editor, and merge tool. It can set global or repository-specific settings.
  1. How do you create an annotated tag?
      • git tag -a <tagname> -m "message": Creates an annotated tag with a message
  1. How do you undo a git push?
      • git revert <commit>: Reverts the changes made by the commit and creates a new commit
      • Alternatively, force push to remove commits (use with caution): git push origin +<branch-name>
  1. How do you find a specific commit by message?
      • git log --all --grep='<message>': Searches for commits with a specific message across all branches
  1. How do you stage partial changes in a file?
      • git add -p <file>: Interactively stages parts of a file
  1. What is a shallow clone in Git?
      • A shallow clone is a clone with a limited commit history, created using git clone --depth <depth> <repository>. This reduces the amount of data transferred and speeds up the clone process.
If you have any questions, please contact me.