Git - Interview Question Set-2



Question-11: You have accidentally deleted a file that was committed to your Git repository. What is the best way to restore the file?

Answer: The best way to restore a deleted file in Git is to use the git checkout command. 


First, identify the commit in which the file was last present using the git log command. 


Then, use the git checkout <commit> -- <file> command to restore the file from the specified commit. 


If the file was deleted in the most recent commit, you can use the git reset HEAD~1 command to reset the branch to the previous commit, use git checkout to restore the file, and then use the git add and git commit commands to commit the restored file.



Question-12: What is Git bisect?

Answer: Git bisect is a tool that helps you find the commit that introduced a bug. It works by doing a binary search of the commit history, asking you to test a commit in the middle of the range each time. 


Based on your feedback, it will narrow down the range of possible commits until it finds the one that introduced the bug.



Question-13: What is a Git stash?

Answer: A Git stash is a way to temporarily save changes that you are not ready to commit, so that you can work on a different branch or pull in changes from another repository. 


Stashing your changes creates a new "stash" object that stores your changes, and you can apply the stash later using git stash apply or git stash pop.



Question-14: You have made a commit to a branch, but you want to move it to a different branch. What is the best way to do this?

Answer: The best way to move a commit to a different branch in Git is to use the git cherry-pick command. 


First, identify the commit hash using the git log command. Then, switch to the target branch using the git checkout command. 


Finally, use the git cherry-pick <commit> command to apply the commit to the target branch. 


This will create a new commit with the changes from the original commit applied to the target branch.



Question-15: What is Git LFS?

Answer: Git LFS (Large File Storage) is an extension for Git that allows you to manage large files such as audio, video, and graphics, that Git cannot handle efficiently. 


Instead of storing the large files in the Git repository, Git LFS stores a pointer to the large file in the repository, and the actual file is stored in a separate Git LFS server.



Question-16: What is a Git submodule?

Answer: A Git submodule is a way to include a separate Git repository as a subdirectory of another repository. 


This is useful when you want to include a library or other code from a separate repository, without copying the code into your repository. 


The submodule is stored as a pointer to a specific commit in the separate repository, so that other developers can update the submodule to the latest version of the code.



Question-17: What is Git cherry-pick?

Answer: Git cherry-pick is a command that allows you to apply a specific commit from one branch to another branch. 


This is useful when you want to apply a bugfix or feature from one branch to another branch without merging the entire branch. 


The cherry-picked commit is treated as a new commit, so it will have a new commit ID.



Question-18: You have made some changes to a file, but you want to see what the file looked like before the changes. What is the best way to view the previous version of the file?

Answer: The best way to view the previous version of a file in Git is to use the git show command. 


First, use the git log command to identify the commit in which the file was last modified. 


Then, use the git show <commit>:path/to/file command to view the contents of the file at the specified commit. 


If the file has been modified multiple times, you can use the git log --oneline path/to/file command to identify the commit hash for the previous version of the file, and then use the git show <commit> command to view the contents of the file at the specified commit.



Question-19: What is Git reflog?

Answer: Git reflog is a log of all the changes to Git references (such as branches, tags, and HEAD) that have occurred in a repository. 


It can be used to recover lost commits, branches, or tags that have been deleted or overwritten. 


The reflog can be accessed using the git reflog command.



Question-20: What is Git filter-branch?

Answer: Git filter-branch is a command that allows you to rewrite the history of a Git repository. 


It can be used to remove sensitive data (such as passwords or API keys) from the commit history, or to split a large repository into smaller repositories. 


The filter-branch command works by applying a set of filters to each commit in the repository, and creating new commits with the filtered changes.

No comments

Powered by Blogger.