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
- What is Git?
- Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
- 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.
- 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.
- What are the basic Git commands?
git init
: Initialize a new Git repositorygit clone
: Clone a repositorygit add
: Add files to the staging areagit commit
: Commit changes to the repositorygit status
: Show the status of the working directory and the staging areagit push
: Push changes to a remote repositorygit pull
: Fetch and integrate changes from a remote repository
- 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.
- 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.
- How do you create a new branch in Git?
git branch <branch-name>
: Creates a new branchgit checkout -b <branch-name>
: Creates a new branch and switches to it
- How do you switch branches in Git?
git checkout <branch-name>
: Switches to the specified branch
- 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.
- 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
- What is the difference between
git fetch
andgit 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.
- 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.
- How do you discard local changes to a file?
git checkout -- <file>
: Discards changes in the working directorygit reset HEAD <file>
: Unstages the file but keeps the changes in the working directory
- What is
git rebase
and how is it different fromgit merge
? git rebase
moves or combines a series of commits to a new base commit. Unlikegit merge
, which creates a new merge commit, rebase rewrites the commit history.
- 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.
- 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.
- How do you revert a commit in Git?
git revert <commit>
: Creates a new commit that undoes the changes of a previous commitgit 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)
- 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
.
- How do you tag a commit in Git?
git tag <tagname> <commit>
: Creates a tag for a specific commitgit push origin <tagname>
: Pushes the tag to the remote repository
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- What is the difference between
git rebase
andgit cherry-pick
? git rebase
re-applies commits from one branch onto another, whereasgit cherry-pick
applies a specific commit from one branch to another.
- 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.
- 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 commitgit config --global core.autocrlf input
: Converts CRLF to LF on commit, but not on checkout
- How do you squash commits in Git?
- Use
git rebase -i <commit>
to interactively rebase and squash commits together into a single commit.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- What is the difference between
git stash pop
andgit stash apply
? git stash pop
applies the stashed changes and then deletes the stash, whereasgit stash apply
applies the stashed changes without deleting the stash.
- 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.
- 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.
- How do you create a patch from a commit?
git format-patch <commit>
: Creates a patch file from a commitgit apply <patch-file>
: Applies the patch to your repository
- What is
git cherry
? git cherry
shows which commits from one branch are not in another branch, helping to identify unique commits.
- 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
- How do you clean up unnecessary files in a repository?
git clean -f
: Removes untracked files from the working directorygit clean -fd
: Removes untracked files and directories
- What is
git blame
? git blame <file>
: Shows what revision and author last modified each line of a file
- How do you rebase a branch onto another branch?
git checkout <branch>
: Switch to the branch you want to rebasegit rebase <target-branch>
: Rebase the current branch onto the target branch
- What is the difference between
git log
andgit reflog
? git log
shows the commit history of the repository, whereasgit reflog
shows a log of changes to the tip of branches and references.
- How do you filter commits in
git log
? git log --author=<name>
: Filters commits by authorgit log --since=<date>
: Filters commits since a specific dategit log --grep=<pattern>
: Filters commits by a commit message pattern
- How do you change the URL of a remote repository?
git remote set-url origin <new-url>
: Changes the URL of the remote repository
- 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.
- How do you compare branches in Git?
git diff <branch1>..<branch2>
: Shows the differences between two branches
- 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.
- How do you create an annotated tag?
git tag -a <tagname> -m "message"
: Creates an annotated tag with a message
- 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>
- How do you find a specific commit by message?
git log --all --grep='<message>'
: Searches for commits with a specific message across all branches
- How do you stage partial changes in a file?
git add -p <file>
: Interactively stages parts of a file
- 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.