fbpx

Top 100 GitHub Interview Questions and Answers

Top 100 GitHub Interview Questions and Answers

Contents show

1. What is Git?

Answer:

Git is a distributed version control system that allows multiple developers to collaborate on a project. It tracks changes, maintains a history of revisions, and enables users to work on different branches simultaneously.

Official Reference


2. Explain the difference between Git and GitHub.

Answer:

Git is a version control system that manages and tracks changes in a local repository. GitHub, on the other hand, is a web-based platform that provides hosting for Git repositories, enabling collaborative development and offering additional features like pull requests, issues, and more.

Official Reference


3. How do you create a new Git repository?

Answer:

To create a new Git repository, navigate to the root directory of your project in the terminal and use the command git init. This initializes a new Git repository in the current directory.

git init

Official Reference


4. What is a branch in Git?

Answer:

A branch in Git is a separate line of development that allows developers to work on different features or bug fixes independently. It keeps the main codebase unaffected until changes are ready to be merged.

# Create a new branch
git branch new-feature

# Switch to the new branch
git checkout new-feature

Official Reference


5. How do you commit changes in Git?

Answer:

To commit changes in Git, use the command git commit -m "commit message". This records the changes in the local repository along with a descriptive message.

git add .
git commit -m "Added new feature"

Official Reference


6. Explain what a pull request is in GitHub.

Answer:

A pull request is a way to propose changes to a repository on GitHub. It allows contributors to submit their code for review and collaboration before merging it into the main branch.

Official Reference


7. How do you merge branches in Git?

Answer:

To merge branches in Git, use the command git merge branch-name. This combines the changes from the specified branch into the current branch.

# Switch to the branch you want to merge into
git checkout main

# Merge the other branch into main
git merge new-feature

Official Reference


8. How do you clone a repository in Git?

Answer:

To clone a Git repository, use the command git clone <repository URL>. This creates a copy of the repository on your local machine.

git clone https://github.com/username/repository.git

Official Reference


9. What is a fork in GitHub?

Answer:

A fork in GitHub is a copy of a repository that allows you to freely experiment with changes without affecting the original repository. It’s often used for proposing changes to a project.

Official Reference


10. Explain the purpose of a pull request review in GitHub.

Answer:

A pull request review in GitHub is a process where contributors review the proposed changes made in a pull request. They can provide feedback, ask questions, and approve or request further modifications before merging.

Official Reference


11. How do you revert a commit in Git?

Answer:

To revert a commit in Git, use the command git revert <commit-hash>. This creates a new commit that undoes the changes made by the specified commit.

git revert <commit-hash>

Official Reference


12. Explain what a Git submodule is.

Answer:

A Git submodule is a way to include a separate Git repository as a subdirectory within your own repository. It allows you to manage external dependencies and keep them updated independently.

Official Reference


13. How do you ignore files in Git?

Answer:

To ignore files in Git, create a file named .gitignore in the root directory of your repository and list the files or patterns you want to ignore.

# Example .gitignore content
*.log
node_modules/

Official Reference


14. What is the purpose of a GitHub Actions workflow?

Answer:

A GitHub Actions workflow is an automated process that can be set up to build, test, and deploy your code on GitHub. It helps automate tasks and streamline development workflows.

Official Reference


15. What is a Git branch protection rule in GitHub?

Answer:

A Git branch protection rule in GitHub is a set of configurations that restricts who can make changes to a branch. It can require code reviews, status checks, and enforce certain conditions before allowing merging.

Official Reference


16. How do you create a new branch in Git and push it to GitHub?

Answer:

To create and push a new branch in Git, use the following commands:

# Create a new branch
git checkout -b new-branch

# Make changes, commit, and push
git add .
git commit -m "Added new feature"
git push origin new-branch

Official Reference


17. Explain the purpose of the .gitattributes file in Git.

Answer:

The .gitattributes file in Git is used to specify how certain files should be treated, such as setting line endings, merging strategies, and diffing behavior. It helps maintain consistency when collaborating on a project.

Official Reference


18. What is a Git stash, and how is it used?

Answer:

A Git stash is a feature that allows you to temporarily save changes without committing them. It’s useful when you need to switch branches or perform other operations without committing half-finished work.

# Stash changes
git stash

# Apply stashed changes
git stash apply

Official Reference


19. How do you squash multiple Git commits into a single commit?

Answer:

To squash multiple Git commits into a single commit, you can use an interactive rebase:

# Start an interactive rebase
git rebase -i HEAD~n

# In the editor, change "pick" to "squash" for commits to squash
# Save and exit the editor

# Follow the prompts to edit the commit message

Official Reference


20. Explain what Git blame is and how it’s used.

Answer:

Git blame is a command that shows who last modified each line of a file and when. It’s used to track the history of changes in a file, helping identify the author of each line or code block.

git blame file-name

Official Reference


21. What is a GitHub Gist?

Answer:

A GitHub Gist is a simple way to share code snippets, text, or markdown files. It’s a quick and easy way to share content with others, similar to a pastebin service.

Official Reference


22. How do you create a Git tag?

Answer:

To create a Git tag, you can use the following command:

git tag <tag-name>

Optionally, you can also add a commit hash after the tag name to create an annotated tag.

Official Reference


23. Explain the purpose of a .gitkeep file.

Answer:

A .gitkeep file is a convention used to ensure that an otherwise empty directory is tracked by Git. It’s often used to include directories in version control even if they don’t have any files.

Official Reference


24. What is a GitHub repository template?

Answer:

A GitHub repository template is a way to provide a pre-configured repository structure as a starting point for new projects. It includes files, directories, and even initial code, making it easier to set up similar projects.

Official Reference


25. How do you remove a file from Git version control without deleting it?

Answer:

To remove a file from Git version control without deleting it, you can use the following command:

git rm --cached <file-name>

This keeps the file in your local directory but removes it from version control.

Official Reference


26. Explain what a GitHub webhook is.

Answer:

A GitHub webhook is a feature that allows external services to be notified of events happening in a repository. It enables automation and integration with external tools whenever certain actions occur in a GitHub repository.

Official Reference


27. What is a GitHub Actions workflow?

Answer:

A GitHub Actions workflow is an automated process defined in a YAML file that specifies a series of steps to be executed when certain events occur in a GitHub repository. It enables continuous integration and continuous deployment (CI/CD) workflows.

Official Reference


28. How do you revert a commit in Git?

Answer:

To revert a commit in Git, you can use the following command:

git revert <commit-hash>

This creates a new commit that undoes the changes introduced by the specified commit.

Official Reference


29. Explain the purpose of .gitignore file.

Answer:

The .gitignore file is used to specify intentionally untracked files that Git should ignore. It’s commonly used to exclude build files, temporary files, and other artifacts from being tracked by Git.

Official Reference


30. What is a GitHub issue template?

Answer:

A GitHub issue template is a markdown file that provides a pre-defined structure for creating new issues in a repository. It helps contributors provide necessary information and context when reporting bugs or suggesting new features.

Official Reference


31. How do you rename a Git branch?

Answer:

To rename a Git branch, you can use the following commands:

# Rename the current branch
git branch -m <new-branch-name>

# Rename a different branch
git branch -m <old-branch-name> <new-branch-name>

Official Reference


32. What is a GitHub repository fork?

Answer:

A GitHub repository fork is a copy of a repository that allows you to freely experiment with changes without affecting the original repository. It’s commonly used for contributing to open-source projects.

Official Reference


33. What is a GitHub pull request?

Answer:

A GitHub pull request (PR) is a way to propose changes to a repository. It allows contributors to submit their changes for review and eventual integration into the main branch of the repository.

Official Reference


34. How do you squash commits in Git?

Answer:

To squash commits in Git, you can use an interactive rebase:

git rebase -i HEAD~<n>

In the interactive rebase editor, replace “pick” with “squash” or “s” for the commits you want to squash.

Official Reference


35. Explain the purpose of Git submodules.

Answer:

Git submodules allow you to include a separate Git repository as a subdirectory within your own repository. This is useful for managing dependencies or including external code while keeping them isolated.

Official Reference


36. What is a GitHub Pages site?

Answer:

A GitHub Pages site is a web page hosted directly from a GitHub repository. It’s often used for documentation, project websites, blogs, or any other type of static content.

Official Reference


37. How do you revert a commit with keeping the changes staged?

Answer:

To revert a commit in Git while keeping the changes staged, you can use the following command:

git reset --soft HEAD^

This resets the commit but leaves the changes in the staging area.

Official Reference


38. What is Git LFS?

Answer:

Git LFS (Large File Storage) is an extension for Git that allows for better handling of large files. It replaces them with text pointers while storing the actual files on a remote server.

Official Reference


39. What is the difference between Git and GitHub?

Answer:

Git is a distributed version control system for tracking changes in source code, while GitHub is a web-based platform for hosting Git repositories and collaborative development.

Official Reference


40. How do you create a Git tag?

Answer:

To create a Git tag, you can use the following command:

git tag <tag-name>

To create an annotated tag with a message, use:

git tag -a <tag-name> -m "Tag message"

Official Reference


41. Explain the purpose of Git bisect.

Answer:

Git bisect is a command used for binary searching through commits to find the exact commit where a bug was introduced. It helps identify the commit that caused a regression or bug.

Official Reference


42. What is Git cherry-pick used for?

Answer:

Git cherry-pick is used to apply specific commits from one branch to another. It allows you to select and apply individual commits without merging entire branches.

Official Reference


43. How do you remove a remote branch in Git?

Answer:

To remove a remote branch in Git, you can use the following command:

git push origin --delete <branch-name>

This deletes the specified branch from the remote repository.

Official Reference


44. What is a Git hook?

Answer:

A Git hook is a script or program that can be executed automatically at certain points in the Git workflow. It allows you to customize and enforce actions before or after Git commands.

Official Reference


45. Explain the purpose of .gitattributes file.

Answer:

The .gitattributes file is used to control how Git treats files, especially with regards to line endings and binary files. It helps ensure consistent handling of files across different platforms.

Official Reference


46. How do you revert a commit in Git?

Answer:

To revert a commit in Git, you can use the following command:

git revert <commit-hash>

This creates a new commit that undoes the changes made by the specified commit.

Official Reference


47. Explain the difference between a fork and a clone in Git.

Answer:

  • A fork is a copy of a repository in a user’s GitHub account. It allows them to freely experiment with changes without affecting the original repository.
  • A clone is a copy of a repository on a local machine. It enables direct interaction with the repository’s files.

Official Reference – Forking
Official Reference – Cloning


48. How do you stash changes in Git?

Answer:

To stash changes in Git, you can use the following command:

git stash save "stash message"

This temporarily saves your local changes, allowing you to switch branches or perform other operations.

Official Reference


49. What is a Git submodule?

Answer:

A Git submodule is a separate Git repository embedded within another Git repository. It allows you to include external projects as dependencies in your main project.

Official Reference


50. How do you rename a Git branch?

Answer:

To rename a Git branch, you can use the following steps:

  1. Switch to a different branch (git checkout <old-branch>).
  2. Rename the branch using the following command:
git branch -m <new-branch>

Official Reference


51. What is Git rebase and when would you use it?

Answer:

Git rebase is used to combine multiple branches together by incorporating the changes of one branch onto another. It’s typically used to maintain a clean, linear commit history.

Official Reference


52. What is a pull request in GitHub?

Answer:

A pull request is a proposed change to a repository on GitHub. It allows contributors to suggest and discuss changes before merging them into the main branch.

Official Reference


53. How do you resolve a merge conflict in Git?

Answer:

To resolve a merge conflict in Git, follow these steps:

  1. Use git status to identify the conflicted files.
  2. Open the conflicted file(s) and manually resolve the differences.
  3. Add the resolved files using git add <filename>.
  4. Commit the changes using git commit.

Official Reference


54. Explain the purpose of the .gitignore file.

Answer:

The .gitignore file specifies intentionally untracked files that Git should ignore. It’s used to exclude files like build artifacts, logs, and configuration files from being tracked.

Official Reference


55. What is Git cherry-pick and when would you use it?

Answer:

Git cherry-pick is used to apply a specific commit from one branch to another. It’s helpful when you want to selectively include a single commit without merging entire branches.

Official Reference


56. How do you set up a remote repository in Git?

Answer:

To set up a remote repository in Git, you can use the following steps:

  1. Create a new repository on a Git hosting service like GitHub.
  2. In your local repository, add the remote using git remote add <name> <url>.
  3. Push your local repository to the remote using git push -u <name> <branch>.

Official Reference


57. What is Git bisect and how is it used?

Answer:

Git bisect is a tool for finding the commit that introduced a specific bug. It works by performing a binary search through the commit history to identify the faulty commit.

Official Reference


58. What is Git rebase and how does it differ from merging?

Answer:

Git rebase is a way to integrate changes from one branch into another by applying each commit in order. It differs from merging in that it creates a linear history, which can result in a cleaner, more readable commit history.

Official Reference


59. Explain the difference between a fork and a clone in Git.

Answer:

  • Fork: A fork is a copy of a repository on GitHub. It allows you to freely experiment with changes without affecting the original repository. Forked repositories can be used to propose changes via pull requests.
  • Clone: Cloning a repository creates a local copy on your machine. This allows you to work on the code locally, make changes, and then push them back to the remote repository.

Official Reference (Forking)
Official Reference (Cloning)


60. What is Git submodules?

Answer:

Git submodules allow you to include other Git repositories as subdirectories within a parent repository. This is useful for including external libraries or projects as part of a larger project.

Official Reference


61. How do you revert a commit in Git?

Answer:

To revert a commit in Git, you can use git revert <commit_id>. This creates a new commit that undoes the changes introduced by the specified commit.

Official Reference


62. Explain the purpose of Git hooks.

Answer:

Git hooks are scripts that can be executed at various points in the Git workflow, such as before or after a commit. They allow for custom actions or validations to be performed automatically.

Official Reference


63. What is Git stash and when would you use it?

Answer:

Git stash is used to save changes that are not ready to be committed, allowing you to switch branches or perform other operations. It’s helpful when you need to temporarily store your changes.

Official Reference


64. How do you resolve a merge conflict in Git?

Answer:

To resolve a merge conflict in Git, follow these steps:

  1. Use git status to identify conflicting files.
  2. Open each conflicting file and manually resolve the conflicting sections.
  3. Add the resolved files using git add <file> for each resolved file.
  4. Complete the merge with git commit -m "Merge conflict resolution".

Official Reference


65. What is a pull request in GitHub?

Answer:

A pull request is a way to propose changes from a forked repository to the original repository. It allows contributors to notify maintainers about changes they’ve made and request that these changes be reviewed and merged.

Official Reference


66. Explain the purpose of Gitignore files.

Answer:

Gitignore files specify intentionally untracked files that Git should ignore. This is useful for excluding files like logs, compiled binaries, and configuration files from being tracked by Git.

Official Reference


67. What is the difference between Git and GitHub?

Answer:

  • Git: Git is a version control system that manages and tracks changes in source code during software development.
  • GitHub: GitHub is a web-based platform that provides hosting for Git repositories along with features like issue tracking, pull requests, and collaboration tools.

Official Reference (Git)
Official Reference (GitHub)


68. How do you create a new branch in Git?

Answer:

To create a new branch in Git, you can use the command git checkout -b <branch_name>. This command creates a new branch and switches to it.

Official Reference


69. What is a Git remote?

Answer:

A Git remote is a reference to a repository hosted on a server, typically on platforms like GitHub, GitLab, or Bitbucket. It allows users to interact with and push to or pull from repositories on that server.

Official Reference


70. Explain the concept of Git branching.

Answer:

Git branching allows multiple independent lines of development within a single repository. Each branch represents a different state of the codebase, enabling parallel work on features, bug fixes, and experiments.

Official Reference


71. How do you revert a commit in Git?

Answer:

To revert a commit in Git, you can use the command git revert <commit_hash>. This creates a new commit that undoes the changes introduced by the specified commit.

Official Reference


72. What is a Git submodule?

Answer:

A Git submodule is a repository that is nested inside another repository. It allows you to include external repositories as dependencies in your project, while keeping them independent.

Official Reference


73. Explain the purpose of Git tags.

Answer:

Git tags are references to specific points in Git history, usually used to mark release points. They allow for easy reference to specific commits, making it simpler to track and manage versions.

Official Reference


74. How do you delete a branch in Git?

Answer:

To delete a branch in Git, you can use the command git branch -d <branch_name>. This command will delete the specified branch, but only if its changes are already merged into another branch.

Official Reference


75. What is a Git stash?

Answer:

A Git stash is a way to save changes that are not ready to be committed yet. It allows you to switch branches or perform other operations without committing half-done work.

Official Reference


76. What is a Git hook?

Answer:

A Git hook is a script that Git runs before or after specific events, such as committing, merging, or receiving a push. They allow you to customize and automate Git workflows.

Official Reference


77. How do you show the commit history in Git?

Answer:

You can view the commit history in Git using the command git log. This displays a list of commits along with their details, such as author, date, and commit message.

Official Reference


78. What is a pull request review in GitHub?

Answer:

A pull request review in GitHub is a way for collaborators to discuss proposed changes. Reviewers can comment on specific lines, approve, request changes, or merge the pull request.

Official Reference


79. What is a Git rebase?

Answer:

Git rebase is a command used to integrate changes from one branch into another. It applies the changes of the current branch on top of the target branch, resulting in a linear history.

Official Reference


80. Explain the difference between Git fetch and Git pull.

Answer:

  • git fetch downloads objects and refs from another repository, allowing you to see what changes have been made. It doesn’t merge or modify your working directory.
  • git pull does a git fetch followed by a git merge. It downloads and integrates changes from a remote repository into your current branch.

Official Reference (Git Fetch)
Official Reference (Git Pull)


81. What is a Gitignore file?

Answer:

A .gitignore file specifies intentionally untracked files that Git should ignore. It’s useful for excluding files like build artifacts, logs, and other generated files from version control.

Official Reference


82. How do you resolve a merge conflict in Git?

Answer:

  1. Use git status to identify conflicted files.
  2. Open the conflicted file and manually resolve the conflicting lines.
  3. Add the resolved files using git add <file_name>.
  4. Commit the changes using git commit.

Official Reference


83. What is a GitHub Action?

Answer:

A GitHub Action is an automated workflow that can be set up in a repository to respond to various events, like pushes, pull requests, and more. It allows you to automate tasks and integrate with external services.

Official Reference


84. How do you squash commits in Git?

Answer:

  1. Use git rebase -i HEAD~n, where n is the number of commits you want to squash.
  2. In the interactive rebase editor, replace “pick” with “squash” for the commits you want to squash.
  3. Save and close the editor.

Official Reference


85. What is a GitHub repository fork?

Answer:

A fork is a copy of a repository in your GitHub account. It allows you to freely experiment with changes without affecting the original repository. You can also submit pull requests from a fork.

Official Reference


86. How do you revert a commit in Git?

Answer:

To revert a commit in Git, you can use the git revert command. It creates a new commit that undoes the changes introduced by the specified commit.

git revert <commit_hash>

Official Reference


87. What is Git bisect?

Answer:

git bisect is a command used for binary search through the commit history. It helps identify the specific commit that introduced a bug by systematically narrowing down the range of commits to inspect.

Official Reference


88. Explain the difference between Git and GitHub.

Answer:

  • Git is a distributed version control system that allows you to track changes in your codebase and collaborate with others.
  • GitHub is a web-based platform that provides Git repository hosting, collaborative tools, and a platform for version control and project management.

Official Git Reference
Official GitHub Reference


89. What is Git LFS?

Answer:

Git LFS (Large File Storage) is an extension to Git that allows handling large files by storing them externally. It replaces large files in your repository with small pointer files, improving performance.

Official Reference


90. How do you create a new branch in Git?

Answer:

To create a new branch in Git, you can use the following command:

git checkout -b <branch_name>

This command creates a new branch and switches to it.

Official Reference


91. Explain what a pull request is in GitHub.

Answer:

A pull request in GitHub is a way to propose changes to a repository. It allows contributors to suggest and discuss modifications before they are merged into the main codebase.

Official Reference


92. How do you merge branches in Git?

Answer:

To merge branches in Git, use the following steps:

  1. Switch to the target branch: git checkout <target_branch>.
  2. Merge the source branch into the target branch: git merge <source_branch>.

Official Reference


93. What is Git cherry-pick?

Answer:

Git cherry-pick is a command used to apply a specific commit from one branch to another. It allows you to select and include specific changes without merging entire branches.

Official Reference


94. How do you rename a remote repository in Git?

Answer:

You can rename a remote repository in Git using the following steps:

  1. Rename the local repository folder.
  2. Update the remote URL using git remote set-url origin <new_url>.

Official Reference


95. Explain Git submodules.

Answer:

Git submodules allow you to include another Git repository as a subdirectory within your own repository. It’s a way to manage external dependencies.

Official Reference


96. How do you view the commit history in Git?

Answer:

You can view the commit history in Git using the command:

git log

This command displays a list of commits along with their details.

Official Reference


97. How do you stash changes in Git?

Answer:

To stash changes in Git, you can use the following commands:

  1. Stash the changes: git stash.
  2. List stashes: git stash list.
  3. Apply the latest stash: git stash apply.
  4. Apply a specific stash: git stash apply stash@{n}.

Official Reference


98. What is Git rebase?

Answer:

Git rebase is a command used to integrate changes from one branch into another. It allows for a cleaner, linear history by applying the changes of one branch on top of another.

Official Reference


99. How do you resolve merge conflicts in Git?

Answer:

To resolve merge conflicts in Git, follow these steps:

  1. Use git status to identify conflicted files.
  2. Open the file(s) and manually resolve the conflicts.
  3. Add the resolved files: git add <file_name>.
  4. Commit the changes: git commit.

Official Reference


100. What is Git reflog?

Answer:

Git reflog is a log of all the actions performed on a repository. It keeps track of changes to the repository’s branches, allowing you to recover lost commits or branches.

Official Reference