{"id":23077862,"url":"https://github.com/bikkimahato/git-interview-questions","last_synced_at":"2025-04-03T12:25:51.278Z","repository":{"id":263653345,"uuid":"890509936","full_name":"bikkimahato/git-interview-questions","owner":"bikkimahato","description":"Welcome to the Git Repository Guide! This repository is designed to provide comprehensive guidance and best practices for using Git effectively. Whether you're a beginner or an experienced developer, you'll find useful information to enhance your Git skills.","archived":false,"fork":false,"pushed_at":"2024-11-23T15:51:22.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-09T01:46:24.665Z","etag":null,"topics":["git","gitscm","interview","interview-questions","version-control"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bikkimahato.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-18T17:34:59.000Z","updated_at":"2024-12-01T11:57:32.000Z","dependencies_parsed_at":"2024-11-19T18:35:46.755Z","dependency_job_id":"c269b20b-ecd5-454b-873d-0ba92c93b7d0","html_url":"https://github.com/bikkimahato/git-interview-questions","commit_stats":null,"previous_names":["bikkimahato/git-interview-questions"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bikkimahato%2Fgit-interview-questions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bikkimahato%2Fgit-interview-questions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bikkimahato%2Fgit-interview-questions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bikkimahato%2Fgit-interview-questions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bikkimahato","download_url":"https://codeload.github.com/bikkimahato/git-interview-questions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247000140,"owners_count":20867034,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["git","gitscm","interview","interview-questions","version-control"],"created_at":"2024-12-16T10:03:33.619Z","updated_at":"2025-04-03T12:25:51.256Z","avatar_url":"https://github.com/bikkimahato.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Git Interview Questions\nWelcome to the Git Repository Guide! This repository is designed to provide comprehensive guidance and best practices for using Git effectively. Whether you're a beginner or an experienced developer, you'll find useful information to enhance your Git skills.\n\n## Introduction\nGit is a powerful version control system used to manage code changes across projects of all sizes. This repository aims to provide clear and concise information to help you navigate and master Git.\n\n## Getting Started\nTo get started with Git, follow these steps:\n1. Install Git: [Git Downloads](https://git-scm.com/downloads)\n2. Configure Git: \n    ```sh\n    git config --global user.name \"Your Name\"\n    git config --global user.email \"your.email@example.com\"\n    ```\n3. Initialize a repository:\n    ```sh\n    git init\n    ```\n\n## Basic Commands\nHere are some fundamental Git commands:\n- `git clone \u003crepository-url\u003e`: Clone an existing repository\n- `git add \u003cfile\u003e`: Add files to staging area\n- `git commit -m \"commit message\"`: Commit changes\n- `git push`: Push changes to the remote repository\n- `git pull`: Fetch and merge changes from the remote repository\n\n## Branching and Merging\nLearn how to manage branches and merge changes:\n- Create a new branch: `git checkout -b \u003cbranch-name\u003e`\n- Switch branches: `git checkout \u003cbranch-name\u003e`\n- Merge branches: `git merge \u003cbranch-name\u003e`\n\n## Advanced Techniques\nExplore advanced Git techniques:\n- Rebase: `git rebase \u003cbranch-name\u003e`\n- Stash changes: `git stash`\n- Cherry-pick: `git cherry-pick \u003ccommit-hash\u003e`\n\n## Best Practices\nFollow these best practices to ensure smooth collaboration:\n- Commit frequently with meaningful messages\n- Keep branches small and focused\n- Use pull requests for code reviews\n\n## Contributing\n\nWe welcome contributions from the community! If you have a question or improvement that you think should be included, please follow these steps:\n\n1. Fork the repository.\n2. Create a new branch for your contribution.\n3. Add your question or improvement in the appropriate category folder (`easy`, `medium`, `hard`).\n4. Submit a pull request with a detailed description of your changes.\n\n---\n\nHappy coding! If you find this repository helpful, please give it a star ⭐ and share it with others.\n\n---\n\n### Table of Contents\n### Level : Easy\n| No. | Questions |\n| --- | --------- |\n| 1   | [What is Git?](#1-what-is-git) |\n| 2   | [How do you initialize a new Git repository?](#2-how-do-you-initialize-a-new-git-repository) |\n| 3   | [How do you clone a repository?](#3-how-do-you-clone-a-repository) |\n| 4   | [What is the command to check the status of your working directory?](#4-what-is-the-command-to-check-the-status-of-your-working-directory) |\n| 5   | [How do you stage a file for commit?](#5-how-do-you-stage-a-file-for-commit) |\n| 6   | [How do you commit changes to a repository?](#6-how-do-you-commit-changes-to-a-repository) |\n| 7   | [What command do you use to view the commit history?](#7-what-command-do-you-use-to-view-the-commit-history) |\n| 8   | [How do you create a new branch?](#8-how-do-you-create-a-new-branch) |\n| 9   | [How do you switch to an existing branch?](#9-how-do-you-switch-to-an-existing-branch) |\n| 10  | [How do you merge a branch into the current branch?](#10-how-do-you-merge-a-branch-into-the-current-branch) |\n| 11  | [What is the difference between `git pull` and `git fetch`?](#11-what-is-the-difference-between-git-pull-and-git-fetch) |\n| 12  | [How do you delete a branch locally?](#12-how-do-you-delete-a-branch-locally) |\n| 13  | [How do you delete a branch remotely?](#13-how-do-you-delete-a-branch-remotely) |\n| 14  | [How do you undo the last commit?](#14-how-do-you-undo-the-last-commit) |\n| 15  | [How do you discard changes in a file?](#15-how-do-you-discard-changes-in-a-file) |\n| 16  | [How do you list all branches in a repository?](#16-how-do-you-list-all-branches-in-a-repository) |\n| 17  | [How do you create a new tag?](#17-how-do-you-create-a-new-tag) |\n| 18  | [How do you push changes to a remote repository?](#18-how-do-you-push-changes-to-a-remote-repository) |\n| 19  | [How do you pull changes from a remote repository?](#19-how-do-you-pull-changes-from-a-remote-repository) |\n| 20  | [How do you set your Git username and email?](#20-how-do-you-set-your-git-username-and-email) |\n| 21  | [What is a commit message and why is it important?](#21-what-is-a-commit-message-and-why-is-it-important) |\n| 22  | [How do you view the changes made to a file?](#22-how-do-you-view-the-changes-made-to-a-file) |\n| 23  | [How do you rename a branch?](#23-how-do-you-rename-a-branch) |\n| 24  | [How do you move or rename a file?](#24-how-do-you-move-or-rename-a-file) |\n| 25  | [What is the difference between `git reset` and `git revert`?](#25-what-is-the-difference-between-git-reset-and-git-revert) |\n\n### Table of Contents\n### Level : Medium\n| No. | Questions |\n| --- | --------- |\n| 1   | [What is a Git repository?](#1-what-is-a-git-repository) |\n| 2   | [Explain the difference between a local and remote repository.](#2-explain-the-difference-between-a-local-and-remote-repository) |\n| 3   | [What is a commit hash?](#3-what-is-a-commit-hash) |\n| 4   | [How do you resolve merge conflicts?](#4-how-do-you-resolve-merge-conflicts) |\n| 5   | [Explain the difference between `git merge` and `git rebase`.](#5-explain-the-difference-between-git-merge-and-git-rebase) |\n| 6   | [How do you stash changes in Git?](#6-how-do-you-stash-changes-in-git) |\n| 7   | [How do you apply stashed changes?](#7-how-do-you-apply-stashed-changes) |\n| 8   | [What is the purpose of the `.gitignore` file?](#8-what-is-the-purpose-of-the-gitignore-file) |\n| 9   | [How do you revert a commit that has already been pushed to the remote repository?](#9-how-do-you-revert-a-commit-that-has-already-been-pushed-to-the-remote-repository) |\n| 10  | [How do you cherry-pick a commit?](#10-how-do-you-cherry-pick-a-commit) |\n| 11  | [What is the difference between `git diff` and `git log`?](#11-what-is-the-difference-between-git-diff-and-git-log) |\n| 12  | [How do you create a new branch and push it to the remote repository in one command?](#12-how-do-you-create-a-new-branch-and-push-it-to-the-remote-repository-in-one-command) |\n| 13  | [What is `git bisect` and how is it used?](#13-what-is-git-bisect-and-how-is-it-used) |\n| 14  | [How do you squash commits?](#14-how-do-you-squash-commits) |\n| 15  | [How do you create a bare repository?](#15-how-do-you-create-a-bare-repository) |\n| 16  | [What is a detached HEAD state?](#16-what-is-a-detached-head-state) |\n| 17  | [How do you configure a remote upstream branch?](#17-how-do-you-configure-a-remote-upstream-branch) |\n| 18  | [How do you set up a Git hook?](#18-how-do-you-set-up-a-git-hook) |\n| 19  | [How do you force push to a remote repository?](#19-how-do-you-force-push-to-a-remote-repository) |\n| 20  | [What are submodules in Git?](#20-what-are-submodules-in-git) |\n| 21  | [How do you clone a repository with submodules?](#21-how-do-you-clone-a-repository-with-submodules) |\n| 22  | [Explain the difference between `git reset --soft`, `--mixed`, and `--hard`.](#22-explain-the-difference-between-git-reset-soft-mixed-and-hard) |\n| 23  | [How do you find a specific commit in the history?](#23-how-do-you-find-a-specific-commit-in-the-history) |\n| 24  | [How do you change the last commit message?](#24-how-do-you-change-the-last-commit-message) |\n| 25  | [What is the difference between an annotated and a lightweight tag?](#25-what-is-the-difference-between-an-annotated-and-a-lightweight-tag) |\n\n### Table of Contents\n### Level : Hard\n| No. | Questions |\n| --- | --------- |\n| 1   | [What is Git rebasing and what are its advantages and disadvantages?](#1-what-is-git-rebasing-and-what-are-its-advantages-and-disadvantages) |\n| 2   | [How do you perform an interactive rebase?](#2-how-do-you-perform-an-interactive-rebase) |\n| 3   | [How do you handle a situation where you accidentally pushed sensitive information to a public repository?](#3-how-do-you-handle-a-situation-where-you-accidentally-pushed-sensitive-information-to-a-public-repository) |\n| 4   | [How do you optimize a repository for performance?](#4-how-do-you-optimize-a-repository-for-performance) |\n| 5   | [Explain the internal structure of a Git repository.](#5-explain-the-internal-structure-of-a-git-repository) |\n| 6   | [How do you handle large binary files in Git?](#6-how-do-you-handle-large-binary-files-in-git) |\n| 7   | [What is the purpose of the `git fsck` command?](#7-what-is-the-purpose-of-the-git-fsck-command) |\n| 8   | [How do you clean up a repository with a large history?](#8-how-do-you-clean-up-a-repository-with-a-large-history) |\n| 9   | [Explain the concept of Git object model (blobs, trees, commits, etc.).](#9-explain-the-concept-of-git-object-model-blobs-trees-commits-etc) |\n| 10  | [How do you recover from a corrupted repository?](#10-how-do-you-recover-from-a-corrupted-repository) |\n| 11  | [How do you set up a Git server?](#11-how-do-you-set-up-a-git-server) |\n| 12  | [What is the difference between `git archive` and `git bundle`?](#12-what-is-the-difference-between-git-archive-and-git-bundle) |\n| 13  | [How do you manage multiple repositories in a single project?](#13-how-do-you-manage-multiple-repositories-in-a-single-project) |\n| 14  | [How do you track changes to a specific directory?](#14-how-do-you-track-changes-to-a-specific-directory) |\n| 15  | [What is the purpose of the `reflog`?](#15-what-is-the-purpose-of-the-reflog) |\n| 16  | [How do you bisect a repository with many branches?](#16-how-do-you-bisect-a-repository-with-many-branches) |\n| 17  | [How do you configure Git to use a different merge tool?](#17-how-do-you-configure-git-to-use-a-different-merge-tool) |\n| 18  | [How do you use `git filter-branch` to rewrite history?](#18-how-do-you-use-git-filter-branch-to-rewrite-history) |\n| 19  | [What are the risks of force-pushing to a shared repository?](#19-what-are-the-risks-of-force-pushing-to-a-shared-repository) |\n| 20  | [How do you manage Git credentials securely?](#20-how-do-you-manage-git-credentials-securely) |\n| 21  | [Explain what a Git worktree is and how to use it.](#21-explain-what-a-git-worktree-is-and-how-to-use-it) |\n| 22  | [How do you enforce commit message guidelines?](#22-how-do-you-enforce-commit-message-guidelines) |\n| 23  | [How do you use `git blame` to find the author of a specific line of code?](#23-how-do-you-use-git-blame-to-find-the-author-of-a-specific-line-of-code) |\n| 24  | [What is the difference between shallow cloning and a full clone?](#24-what-is-the-difference-between-shallow-cloning-and-a-full-clone) |\n| 25  | [How do you handle a situation where a merge introduces a bug that needs to be fixed without reverting the merge?](#25-how-do-you-handle-a-situation-where-a-merge-introduces-a-bug-that-needs-to-be-fixed-without-reverting-the-merge) |\n\n# Easy Git Interview Questions and Answers\n### 1. What is Git?\n\nGit is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It allows multiple people to collaborate on a project, track changes, and revert to previous versions if needed.\n\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 2. How do you initialize a new Git repository?\n\nTo initialize a new Git repository, navigate to your project directory and use the following command:\n\n```sh\ngit init\n```\n\nThis command creates a new `.git` subdirectory in your project directory, which contains all the necessary metadata for the repository.\n\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 3. How do you clone a repository?\n\nTo clone an existing repository, use the following command:\n\n```sh\ngit clone \u003crepository_url\u003e\n```\n\nFor example:\n\n```sh\ngit clone https://github.com/user/repository.git\n```\n\nThis command creates a new directory with the name of the repository and copies all the repository data into it.\n\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 4. What is the command to check the status of your working directory?\n\nTo check the status of your working directory, use:\n\n```sh\ngit status\n```\n\nThis command shows which files have been modified, which files are staged for commit, and which files are not being tracked by Git.\n\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 5. How do you stage a file for commit?\n\nTo stage a file for commit, use:\n\n```sh\ngit add \u003cfile_name\u003e\n```\n\nFor example:\n\n```sh\ngit add README.md\n```\n\nYou can also stage all modified files at once using:\n\n```sh\ngit add .\n```\n\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 6. How do you commit changes to a repository?\n\nTo commit changes, use:\n\n```sh\ngit commit -m \"Your commit message\"\n```\n\nFor example:\n\n```sh\ngit commit -m \"Add initial project files\"\n```\n\nThe `-m` flag allows you to add a commit message directly from the command line.\n\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 7. What command do you use to view the commit history?\n\nTo view the commit history, use:\n\n```sh\ngit log\n```\n\nThis command lists all the commits along with their messages, authors, and timestamps.\n\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 8. How do you create a new branch?\n\nTo create a new branch, use:\n\n```sh\ngit branch \u003cbranch_name\u003e\n```\n\nFor example:\n\n```sh\ngit branch feature-xyz\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 9. How do you switch to an existing branch?\n\nTo switch to an existing branch, use:\n\n```sh\ngit checkout \u003cbranch_name\u003e\n```\n\nFor example:\n\n```sh\ngit checkout feature-xyz\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 10. How do you merge a branch into the current branch?\n\nTo merge a branch into the current branch, use:\n\n```sh\ngit merge \u003cbranch_name\u003e\n```\n\nFor example, if you are on the `main` branch and want to merge `feature-xyz`:\n\n```sh\ngit merge feature-xyz\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 11. What is the difference between `git pull` and `git fetch`?\n\n- `git pull` fetches changes from a remote repository and merges them into your current branch.\n- `git fetch` only downloads changes from a remote repository but does not merge them into your current branch.\n\nExample:\n\n```sh\ngit fetch origin\ngit merge origin/main\n```\n\nis equivalent to:\n\n```sh\ngit pull origin main\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 12. How do you delete a branch locally?\n\nTo delete a branch locally, use:\n\n```sh\ngit branch -d \u003cbranch_name\u003e\n```\n\nFor example:\n\n```sh\ngit branch -d feature-xyz\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 13. How do you delete a branch remotely?\n\nTo delete a branch remotely, use:\n\n```sh\ngit push origin --delete \u003cbranch_name\u003e\n```\n\nFor example:\n\n```sh\ngit push origin --delete feature-xyz\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 14. How do you undo the last commit?\n\nTo undo the last commit, use:\n\n```sh\ngit revert HEAD\n```\n\nOr, if you want to remove the commit and the changes:\n\n```sh\ngit reset --hard HEAD~1\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 15. How do you discard changes in a file?\n\nTo discard changes in a file, use:\n\n```sh\ngit checkout -- \u003cfile_name\u003e\n```\n\nFor example:\n\n```sh\ngit checkout -- README.md\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 16. How do you list all branches in a repository?\n\nTo list all branches, use:\n\n```sh\ngit branch\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 17. How do you create a new tag?\n\nTo create a new tag, use:\n\n```sh\ngit tag \u003ctag_name\u003e\n```\n\nFor example:\n\n```sh\ngit tag v1.0\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 18. How do you push changes to a remote repository?\n\nTo push changes, use:\n\n```sh\ngit push \u003cremote\u003e \u003cbranch\u003e\n```\n\nFor example:\n\n```sh\ngit push origin main\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 19. How do you pull changes from a remote repository?\n\nTo pull changes, use:\n\n```sh\ngit pull \u003cremote\u003e \u003cbranch\u003e\n```\n\nFor example:\n\n```sh\ngit pull origin main\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 20. How do you set your Git username and email?\n\nTo set your Git username and email, use:\n\n```sh\ngit config --global user.name \"Your Name\"\ngit config --global user.email \"your.email@example.com\"\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 21. What is a commit message and why is it important?\n\nA commit message is a brief description of the changes made in a commit. It is important because it provides context and reasoning for the changes, making it easier for others (and yourself) to understand the history and purpose of changes in the project.\n\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 22. How do you view the changes made to a file?\n\nTo view changes made to a file, use:\n\n```sh\ngit diff \u003cfile_name\u003e\n```\n\nFor example:\n\n```sh\ngit diff README.md\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 23. How do you rename a branch?\n\nTo rename a branch, use:\n\n```sh\ngit branch -m \u003cold_name\u003e \u003cnew_name\u003e\n```\n\nFor example:\n\n```sh\ngit branch -m old-branch-name new-branch-name\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 24. How do you move or rename a file?\n\nTo move or rename a file, use:\n\n```sh\ngit mv \u003cold_path\u003e \u003cnew_path\u003e\n```\n\nFor example:\n\n```sh\ngit mv old_file.txt new_file.txt\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### 25. What is the difference between `git reset` and `git revert`?\n\n- `git reset` moves the current branch pointer to a specified commit and can also modify the staging area and working directory.\n- `git revert` creates a new commit that undoes the changes of a specified commit, preserving the commit history.\n\nExample of `git reset`:\n\n```sh\ngit reset --hard HEAD~1\n```\n\nExample of `git revert`:\n\n```sh\ngit revert HEAD\n```\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n### Summary Table\n\n| Command | Description |\n|---------|-------------|\n| `git init` | Initialize a new Git repository |\n| `git clone \u003crepository_url\u003e` | Clone an existing repository |\n| `git status` | Check the status of the working directory |\n| `git add \u003cfile_name\u003e` | Stage a file for commit |\n| `git commit -m \"message\"` | Commit changes to the repository |\n| `git log` | View the commit history |\n| `git branch \u003cbranch_name\u003e` | Create a new branch |\n| `git checkout \u003cbranch_name\u003e` | Switch to an existing branch |\n| `git merge \u003cbranch_name\u003e` | Merge a branch into the current branch |\n| `git pull` | Fetch and merge changes from a remote repository |\n| `git fetch` | Fetch changes from a remote repository |\n| `git branch -d \u003cbranch_name\u003e` | Delete a branch locally |\n| `git push origin --delete \u003cbranch_name\u003e` | Delete a branch remotely |\n| `git revert HEAD` | Undo the last commit |\n| `git checkout -- \u003cfile_name\u003e` | Discard changes in a file |\n| `git branch` | List all branches |\n| `git tag \u003ctag_name\u003e` | Create a new tag |\n| `git push \u003cremote\u003e \u003cbranch\u003e` | Push changes to a remote repository |\n| `git pull \u003cremote\u003e \u003cbranch\u003e` | Pull changes from a remote repository |\n| `git config --global user.name \"name\"` | Set Git username |\n| `git config --global user.email \"email\"` | Set Git email |\n| `git diff \u003cfile_name\u003e` | View changes made to a file |\n| `git branch -m \u003cold_name\u003e \u003cnew_name\u003e` | Rename a branch |\n| `git mv \u003cold_path\u003e \u003cnew_path\u003e` | Move or rename a file |\n| `git reset` | Move the current branch pointer |\n| `git revert` | Create a new commit that undoes changes |\n\nThis comprehensive guide should provide you with a solid understanding of Git commands and their usage.\n\n#### **[⬆ Back to Top](#level--easy)**\n---\n\n# Medium Git Interview Questions and Answers\n### 1. What is a Git repository?\n\nA Git repository is a storage space where your project files and their history of changes are stored. It includes all the project’s files and the entire history of changes made to those files, which allows you to track versions, revert to previous states, and collaborate with others.\n\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 2. Explain the difference between a local and remote repository.\n\n- **Local Repository**: A repository stored on your local machine. You interact with this repository using Git commands.\n- **Remote Repository**: A repository hosted on a remote server. It is often used for collaboration where multiple users push and pull changes.\n\nFor example, you clone a remote repository to create a local copy on your machine.\n\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 3. What is a commit hash?\n\nA commit hash is a unique identifier for a specific commit in the Git history. It's a 40-character SHA-1 hash that ensures each commit can be uniquely identified.\n\nExample of a commit hash:\n\n```\ne1a1d3e8b8e3a1b5a1f3c1c8d8e8d8c8c8c8d8d8\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 4. How do you resolve merge conflicts?\n\nTo resolve merge conflicts:\n1. Identify the files with conflicts using `git status`.\n2. Open the conflicting files and manually resolve conflicts.\n3. Stage the resolved files using `git add \u003cfile_name\u003e`.\n4. Commit the resolved changes using `git commit`.\n\nExample:\n\n```sh\ngit add resolved_file.txt\ngit commit -m \"Resolve merge conflict in resolved_file.txt\"\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 5. Explain the difference between `git merge` and `git rebase`.\n\n- **`git merge`**: Combines the changes from one branch into another, creating a merge commit.\n  \n  ```sh\n  git merge feature-branch\n  ```\n\n- **`git rebase`**: Moves or combines a sequence of commits to a new base commit. It creates a linear history.\n\n  ```sh\n  git rebase main\n  ```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n\n### 6. How do you stash changes in Git?\n\nTo stash changes, use:\n\n```sh\ngit stash\n```\n\nThis command saves your local modifications away and reverts the working directory to match the HEAD commit.\n\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 7. How do you apply stashed changes?\n\nTo apply stashed changes, use:\n\n```sh\ngit stash apply\n```\n\nOr to both apply and remove the stash:\n\n```sh\ngit stash pop\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 8. What is the purpose of the `.gitignore` file?\n\nThe `.gitignore` file specifies which files and directories Git should ignore and not track. It helps prevent committing unnecessary files like temporary files, build artifacts, and sensitive information.\n\nExample `.gitignore`:\n\n```\n*.log\nnode_modules/\n.env\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 9. How do you revert a commit that has already been pushed to the remote repository?\n\nTo revert a pushed commit, use:\n\n```sh\ngit revert \u003ccommit_hash\u003e\n```\n\nThis creates a new commit that undoes the changes of the specified commit.\n\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 10. How do you cherry-pick a commit?\n\nTo cherry-pick a commit, use:\n\n```sh\ngit cherry-pick \u003ccommit_hash\u003e\n```\n\nThis command copies the changes from the specified commit and applies them to your current branch.\n\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 11. What is the difference between `git diff` and `git log`?\n\n- **`git diff`**: Shows the changes between commits, branches, files, etc.\n\n  ```sh\n  git diff\n  ```\n\n- **`git log`**: Shows the commit history.\n\n  ```sh\n  git log\n  ```\n\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 12. How do you create a new branch and push it to the remote repository in one command?\n\nTo create a new branch and push it to the remote repository:\n\n```sh\ngit checkout -b \u003cbranch_name\u003e \u0026\u0026 git push -u origin \u003cbranch_name\u003e\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 13. What is `git bisect` and how is it used?\n\n`git bisect` helps you find the commit that introduced a bug by performing a binary search through your commit history.\n\nExample usage:\n\n```sh\ngit bisect start\ngit bisect bad\ngit bisect good \u003ccommit_hash\u003e\n```\n\nMark each commit as good or bad until you find the problematic commit.\n\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 14. How do you squash commits?\n\nTo squash commits, use interactive rebase:\n\n```sh\ngit rebase -i HEAD~n\n```\n\nReplace `pick` with `squash` (or `s`) for the commits you want to squash, then save and close the editor.\n\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 15. How do you create a bare repository?\n\nTo create a bare repository, use:\n\n```sh\ngit init --bare \u003crepository_name\u003e.git\n```\n\nA bare repository is typically used as a central repository for collaboration.\n\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 16. What is a detached HEAD state?\n\nA detached HEAD state occurs when your HEAD is pointing to a commit instead of a branch. This means you are not on any branch.\n\nExample:\n\n```sh\ngit checkout \u003ccommit_hash\u003e\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 17. How do you configure a remote upstream branch?\n\nTo configure a remote upstream branch, use:\n\n```sh\ngit branch --set-upstream-to=\u003cremote\u003e/\u003cbranch\u003e\n```\n\nFor example:\n\n```sh\ngit branch --set-upstream-to=origin/main\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 18. How do you set up a Git hook?\n\nCreate a file in the `.git/hooks` directory with the appropriate hook name (e.g., `pre-commit`, `post-commit`). Write your script in this file.\n\nExample `pre-commit` hook:\n\n```sh\n#!/bin/sh\necho \"Running pre-commit hook\"\n```\n\nMake the script executable:\n\n```sh\nchmod +x .git/hooks/pre-commit\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 19. How do you force push to a remote repository?\n\nTo force push, use:\n\n```sh\ngit push --force\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 20. What are submodules in Git?\n\nSubmodules allow you to keep a Git repository as a subdirectory of another Git repository. This is useful for managing dependencies.\n\nAdding a submodule:\n\n```sh\ngit submodule add \u003crepository_url\u003e \u003cpath\u003e\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 21. How do you clone a repository with submodules?\n\nTo clone a repository with submodules:\n\n```sh\ngit clone --recurse-submodules \u003crepository_url\u003e\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 22. Explain the difference between `git reset --soft`, `--mixed`, and `--hard`.\n\n- **`--soft`**: Moves HEAD to the specified commit, but leaves the staging area and working directory unchanged.\n\n  ```sh\n  git reset --soft HEAD~1\n  ```\n\n- **`--mixed`** (default): Moves HEAD to the specified commit and resets the staging area, but leaves the working directory unchanged.\n\n  ```sh\n  git reset --mixed HEAD~1\n  ```\n\n- **`--hard`**: Moves HEAD to the specified commit and resets both the staging area and working directory.\n\n  ```sh\n  git reset --hard HEAD~1\n  ```\n\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 23. How do you find a specific commit in the history?\n\nTo find a specific commit, use:\n\n```sh\ngit log --grep=\"\u003csearch_term\u003e\"\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 24. How do you change the last commit message?\n\nTo change the last commit message, use:\n\n```sh\ngit commit --amend -m \"New commit message\"\n```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### 25. What is the difference between an annotated and a lightweight tag?\n\n- **Annotated Tag**: Stores extra metadata such as the tagger name, email, date, and a tagging message. It's stored as a full object in the Git database.\n\n  ```sh\n  git tag -a \u003ctag_name\u003e -m \"Tag message\"\n  ```\n\n- **Lightweight Tag**: A simple pointer to a specific commit. It does not store any extra metadata.\n\n  ```sh\n  git tag \u003ctag_name\u003e\n  ```\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n### Summary Table\n\n| Command | Description |\n|---------|-------------|\n| `git init` | Initialize a Git repository |\n| `git clone \u003crepository_url\u003e` | Clone a repository |\n| `git commit -m \"message\"` | Commit changes |\n| `git status` | Check the status of the working directory |\n| `git add \u003cfile_name\u003e` | Stage changes |\n| `git log` | View commit history |\n| `git diff` | View changes |\n| `git branch \u003cbranch_name\u003e` | Create a new branch |\n| `git checkout \u003cbranch_name\u003e` | Switch branches |\n| `git merge \u003cbranch_name\u003e` | Merge branches |\n| `git rebase \u003cbranch_name\u003e` | Rebase branches |\n| `git stash` | Stash changes |\n| `git stash apply` | Apply stashed changes |\n| `git revert \u003ccommit_hash\u003e` | Revert a commit |\n| `git cherry-pick \u003ccommit_hash\u003e` | Cherry-pick a commit |\n| `git bisect` | Perform binary search to find a commit |\n| `git rebase -i HEAD~n` | Squash commits |\n| `git init --bare` | Create a bare repository |\n| `git branch --set-upstream-to=\u003cremote\u003e/\u003cbranch\u003e` | Set upstream branch |\n| `git push --force` | Force push to a remote repository |\n| `git submodule add \u003crepository_url\u003e` | Add a submodule |\n| `git clone --recurse-submodules \u003crepository_url\u003e` | Clone repository with submodules |\n| `git reset --soft` | Reset with soft option |\n| `git reset --mixed` | Reset with mixed option |\n| `git reset --hard` | Reset with hard option |\n| `git log --grep=\"\u003csearch_term\u003e\"` | Search commit history |\n| `git commit --amend -m \"New message\"` | Amend last commit message |\n| `git tag -a \u003ctag_name\u003e -m \"message\"` | Create an annotated tag |\n| `git tag \u003ctag_name\u003e` | Create a lightweight tag |\n\nThis guide should provide a comprehensive understanding of advanced Git commands and concepts.\n\n#### **[⬆ Back to Top](#level--medium)**\n---\n\n# Hard Git Interview Questions and Answers\n### 1. What is Git rebasing and what are its advantages and disadvantages?\n\n**Git Rebasing** is the process of moving or combining a sequence of commits to a new base commit. It allows you to maintain a linear project history.\n\n#### Advantages:\n- **Clean History**: Rebasing results in a cleaner, more linear commit history.\n- **Easier Merging**: Reduces the complexity of the merge process by avoiding merge commits.\n\n#### Disadvantages:\n- **History Rewriting**: Can be dangerous if not used carefully, especially with shared branches, as it rewrites commit history.\n- **Loss of Context**: Merge commits provide a context of when branches were combined, which is lost with rebasing.\n\n**Example:**\n\n```sh\ngit checkout feature-branch\ngit rebase main\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 2. How do you perform an interactive rebase?\n\nInteractive rebase allows you to edit, reorder, squash, or drop commits.\n\n**Example:**\n\n```sh\ngit rebase -i HEAD~3\n```\n\nThis command opens an editor where you can specify actions for the last three commits.\n\n```sh\npick 1234567 Commit message 1\nsquash 89abcdef Commit message 2\npick fedcba9 Commit message 3\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 3. How do you handle a situation where you accidentally pushed sensitive information to a public repository?\n\n1. **Remove the sensitive data from history**:\n\n```sh\ngit filter-branch --force --index-filter \\\n  'git rm --cached --ignore-unmatch \u003cfile_path\u003e' \\\n  --prune-empty --tag-name-filter cat -- --all\n```\n\n2. **Force-push the changes**:\n\n```sh\ngit push origin --force --all\n```\n\n3. **Invalidate old references**:\n\n```sh\ngit push origin --force --tags\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n4. **Inform collaborators**: Notify all collaborators to re-clone the repository.\n\n### 4. How do you optimize a repository for performance?\n\n- **Garbage Collection**: Run `git gc` to optimize the repository by compressing file history.\n- **Pack Files**: Use `git repack` to create more efficient pack files.\n- **Prune Unreachable Objects**: Remove unreachable objects with `git prune`.\n\n**Example:**\n\n```sh\ngit gc --aggressive --prune=now\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 5. Explain the internal structure of a Git repository.\n\nA Git repository consists of several components:\n\n- **Objects**: Blobs, Trees, Commits, and Tags.\n- **Refs**: Branches and Tags.\n- **Index**: The staging area.\n- **HEAD**: A pointer to the current branch.\n\n**Example:**\n\n```sh\n.git/\n  ├── objects/\n  ├── refs/\n  ├── index\n  └── HEAD\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 6. How do you handle large binary files in Git?\n\nUse **Git LFS (Large File Storage)** to manage large binary files.\n\n**Example:**\n\n1. **Install Git LFS**:\n\n```sh\ngit lfs install\n```\n\n2. **Track large files**:\n\n```sh\ngit lfs track \"*.bin\"\n```\n\n3. **Add and commit**:\n\n```sh\ngit add .gitattributes\ngit add largefile.bin\ngit commit -m \"Add large file using LFS\"\ngit push\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 7. What is the purpose of the `git fsck` command?\n\n`git fsck` checks the integrity of the Git repository, identifying any corrupted objects.\n\n**Example:**\n\n```sh\ngit fsck\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 8. How do you clean up a repository with a large history?\n\nUse `git filter-branch` to rewrite history and remove unwanted data.\n\n**Example:**\n\n```sh\ngit filter-branch --prune-empty --tree-filter 'rm -rf unnecessary_directory' -- --all\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 9. Explain the concept of Git object model (blobs, trees, commits, etc.).\n\n- **Blobs**: Store file data.\n- **Trees**: Represent directories and contain pointers to blobs and other trees.\n- **Commits**: Hold metadata and pointers to tree objects and parent commits.\n- **Tags**: Annotated tags provide metadata for a specific commit.\n\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 10. How do you recover from a corrupted repository?\n\n1. **Check repository**:\n\n```sh\ngit fsck\n```\n\n2. **Recover lost commits**:\n\n```sh\ngit reflog\n```\n\n3. **Restore from backup**: If available, restore from a backup.\n\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 11. How do you set up a Git server?\n\n**Example using SSH:**\n\n1. **Initialize a bare repository**:\n\n```sh\ngit init --bare /path/to/repo.git\n```\n\n2. **Set up SSH access**: Ensure SSH is configured on the server.\n\n3. **Push to the remote repository**:\n\n```sh\ngit remote add origin user@server:/path/to/repo.git\ngit push -u origin main\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 12. What is the difference between `git archive` and `git bundle`?\n\n- **`git archive`**: Creates a tar or zip archive of the repository.\n\n  ```sh\n  git archive --format=tar HEAD | gzip \u003e repository.tar.gz\n  ```\n\n- **`git bundle`**: Creates a single file containing the repository data.\n\n  ```sh\n  git bundle create repository.bundle main\n  ```\n\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 13. How do you manage multiple repositories in a single project?\n\nUse **submodules** or **monorepos**:\n\n**Submodules Example:**\n\n```sh\ngit submodule add \u003crepository_url\u003e \u003cpath\u003e\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 14. How do you track changes to a specific directory?\n\nUse `git log` with the directory path:\n\n```sh\ngit log -- \u003cdirectory_path\u003e\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 15. What is the purpose of the `reflog`?\n\n`reflog` records changes to the tips of branches and other references, allowing you to recover lost commits.\n\n**Example:**\n\n```sh\ngit reflog\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 16. How do you bisect a repository with many branches?\n\n1. **Start bisect**:\n\n```sh\ngit bisect start\n```\n\n2. **Mark the bad commit**:\n\n```sh\ngit bisect bad\n```\n\n3. **Mark a good commit**:\n\n```sh\ngit bisect good \u003ccommit_hash\u003e\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n4. **Continue until the problematic commit is found**.\n\n### 17. How do you configure Git to use a different merge tool?\n\nSet the merge tool in your Git configuration:\n\n```sh\ngit config --global merge.tool \u003ctool_name\u003e\ngit config --global mergetool.\u003ctool_name\u003e.path \u003cpath_to_tool\u003e\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 18. How do you use `git filter-branch` to rewrite history?\n\nRewriting history to remove a file:\n\n```sh\ngit filter-branch --index-filter 'git rm --cached --ignore-unmatch \u003cfile_path\u003e' -- --all\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 19. What are the risks of force-pushing to a shared repository?\n\n- **Overwriting Changes**: You might overwrite others' changes.\n- **History Rewriting**: Force-push rewrites history, which can lead to inconsistencies.\n\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 20. How do you manage Git credentials securely?\n\n- **Use SSH keys** for authentication.\n- **Use credential helpers** to securely store credentials.\n\n**Example:**\n\n```sh\ngit config --global credential.helper cache\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 21. Explain what a Git worktree is and how to use it.\n\nA **Git worktree** allows you to have multiple working directories attached to a single repository.\n\n**Example:**\n\n```sh\ngit worktree add \u003cpath\u003e \u003cbranch\u003e\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 22. How do you enforce commit message guidelines?\n\nUse a **commit-msg hook**:\n\n1. **Create the hook**:\n\n```sh\necho -e '#!/bin/sh\\nexec \u003cscript_path\u003e' \u003e .git/hooks/commit-msg\nchmod +x .git/hooks/commit-msg\n```\n\n2. **Write the script** to enforce guidelines.\n\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 23. How do you use `git blame` to find the author of a specific line of code?\n\nUse `git blame` with the file path:\n\n```sh\ngit blame \u003cfile_path\u003e\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 24. What is the difference between shallow cloning and a full clone?\n\n- **Shallow Cloning**: Clones the repository with limited history, reducing download size.\n\n  ```sh\n  git clone --depth=1 \u003crepository_url\u003e\n  ```\n\n- **Full Clone**: Clones the entire repository history.\n\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### 25. How do you handle a situation where a merge introduces a bug that needs to be fixed without reverting the merge?\n\n1. **Identify the bug** and create a new branch:\n\n```sh\ngit checkout -b fix-branch\n```\n\n2. **Make necessary changes and commit**:\n\n```sh\ngit commit -m \"Fix bug introduced in merge\"\n```\n\n3. **Merge the fix branch back**:\n\n```sh\ngit checkout main\ngit merge fix-branch\n```\n#### **[⬆ Back to Top](#level--hard)**\n---\n\n### Summary Table\n\n| Command | Description |\n|---------|-------------|\n| `git rebase -i` | Perform an interactive rebase |\n| `git filter-branch` | Rewrite history |\n| `git fsck` | Check repository integrity |\n| `git gc` | Optimize repository |\n| `git lfs` | Manage large files |\n| `git reflog` | Record changes to refs |\n| `git bisect` | Find a commit introducing a bug |\n| `git worktree` | Manage multiple worktrees |\n| `git blame` | Find the author of a line |\n| `git clone --depth=1` | Shallow clone repository |\n\nThis guide provides an in-depth understanding of advanced Git concepts and commands with examples.\n\n#### **[⬆ Back to Top](#level--hard)**\n---","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbikkimahato%2Fgit-interview-questions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbikkimahato%2Fgit-interview-questions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbikkimahato%2Fgit-interview-questions/lists"}