50 Advanced Git Questions with answers:

50 Advanced Git Questions with answers:

What is Git and how does it differ from other version control systems?

  • Answer: Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Unlike centralized version control systems, Git allows every developer to have a full copy of the repository, enabling them to work offline.

Explain the difference between Git and GitHub.

  • Answer: Git is a version control system, while GitHub is a web-based platform for hosting and collaborating on Git repositories. GitHub provides additional features such as pull requests, issue tracking, and collaborative tools.

What is a Git repository?

  • Answer: A Git repository is a collection of files and their history, stored in a .git directory at the root of the project. It contains the complete history of the project, along with metadata and configuration settings.

What is a commit in Git?

  • Answer: A commit is a snapshot of changes made to the repository at a specific point in time. It includes a unique identifier (SHA-1 hash), author information, timestamp, and a reference to the previous commit.

Explain the Git staging area.

  • Answer: The staging area (or index) is a crucial part of Git that acts as a middle-ground between the working directory and the repository. Changes are first staged using git add before being committed to the repository.

What is a branch in Git?

  • Answer: A branch in Git is a lightweight movable pointer to a commit. It allows developers to work on separate features or fixes without affecting the main codebase. Branches can be easily created, merged, or deleted.

How do you create a new branch in Git?

  • Answer: Use the command git branch <branch_name> to create a new branch. To switch to the new branch, use git checkout <branch_name> or git switch <branch_name>.

Explain the concept of Git merging.

  • Answer: Merging in Git combines changes from different branches. It can be done using git merge to integrate changes from a source branch into a target branch.

What is a Git merge conflict, and how can it be resolved?

  • Answer: A merge conflict occurs when Git cannot automatically reconcile changes between branches. To resolve conflicts, manually edit the conflicting files, mark them as resolved using git add, and complete the merge with git merge --continue.

Describe the rebase operation in Git.

  • Answer: Git rebase is the process of moving or combining a sequence of commits to a new base commit. It can be used to maintain a cleaner and linear project history.

What is a detached HEAD state in Git?

  • Answer: A detached HEAD state occurs when the HEAD points directly to a specific commit instead of a branch. It is a common state during operations like checking out a specific commit.

Explain the difference between git pull and git fetch.

  • Answer: git pull fetches changes from a remote repository and automatically merges them into the current branch. git fetch only retrieves changes but does not automatically merge them, allowing the user to review and decide how to integrate the changes.

How do you revert a commit in Git?

  • Answer: To revert a commit, use git revert <commit_hash> to create a new commit that undoes the changes introduced by the specified commit.

What is a Git submodule?

  • Answer: A Git submodule is a repository embedded within another repository. It allows you to include external repositories as a subdirectory of your project.

Explain the purpose of Git hooks.

  • Answer: Git hooks are scripts that can be triggered at specific points in the Git workflow. They enable custom actions such as pre-commit validation or post-receive deployment.

How do you squash commits in Git?

  • Answer: Use git rebase -i HEAD~n (replace n with the number of commits) and mark commits as "squash" or "s" to combine them into a single commit.

Describe the .gitignore file.

  • Answer: The .gitignore file specifies files and directories that should be ignored by Git. It helps exclude unnecessary files (e.g., build artifacts, logs) from version control.

What is Git bisect, and how is it used?

  • Answer: Git bisect is a binary search tool for finding the commit that introduced a bug. It involves marking known good and bad commits, allowing Git to narrow down the faulty commit.

Explain the purpose of Git cherry-pick.

  • Answer: Git cherry-pick is used to apply a specific commit from one branch to another. It allows you to selectively choose and apply changes.

What is Git reflog, and how is it useful?

  • Answer: Git reflog is a log of reference updates in the repository. It helps recover lost commits or branches and provides a history of recent operations.

How do you sign commits in Git?

  • Answer: Sign commits using git commit -S to add a GPG signature. Configure Git with your GPG key, and use git log --show-signature to verify signatures.

What is Git cherry and when would you use it?

  • Answer: Git cherry is used to find commits that are in one branch but not in another. It is helpful when you want to identify changes that have not been merged between branches.

Describe Git rebase --onto.

  • Answer: git rebase --onto is used to reapply commits onto a new base. It allows you to move a branch or a series of commits from one branch to another.

Explain the purpose of Git subcommands like git clean and git reset.

  • Answer: git clean removes untracked files, and git reset allows you to reset the current branch to a specific commit or undo changes.

What is the purpose of the git worktree command?

  • Answer: The git worktree command allows you to maintain multiple working directories (worktrees) associated with a single Git repository. Each worktree can have its own branch and changes.

Describe Git shallow cloning.

  • Answer: Shallow cloning involves cloning a Git repository with a limited history. It can be done using --depth option with git clone to specify the number of commits to retrieve.

How do you configure Git to use an HTTP proxy?

  • Answer: Set the HTTP proxy for Git using the http.proxy configuration. For example, use git config --global http.proxy http://proxy.example.com:8080.

Explain the purpose of Git LFS (Large File Storage).

  • Answer: Git LFS is an extension to Git that allows for the storage of large files outside the Git repository, reducing the repository size. It is useful for managing binary files and large assets.

What is the Git "refspec"?

  • Answer: A refspec in Git is a string that defines the mapping between remote and local branches during fetch or push operations. It specifies the source and destination references.

Describe Git worktrees and their advantages.

  • Answer: Git worktrees allow you to work on multiple branches simultaneously without switching back and forth. They provide a clean and isolated environment for each branch.

How do you handle confidential information, such as API keys, in a Git repository?

  • Answer: Confidential information should be stored in environment variables or configuration files outside the repository. Use tools like .gitignore and git-crypt to avoid accidentally committing sensitive data.

What is Git rebase -i used for, and how does it work?

  • Answer: git rebase -i opens an interactive rebase session, allowing you to modify, reorder, or squash commits. It provides a text editor interface where you can choose actions for each commit.

Explain the purpose of Git sparse-checkout.

  • Answer: Git sparse-checkout is used to limit the working directory to specific directories or files, enabling users to check out only the necessary parts of a repository.

How do you recover from a detached HEAD state in Git?

  • Answer: To recover from a detached HEAD state, create a new branch using git checkout -b new_branch_name or switch to an existing branch using git checkout branch_name.

What is the Git "rerere" (Reuse Recorded Resolution) feature?

  • Answer: The rerere feature records previous conflict resolutions, allowing Git to automatically apply them to future conflicts. It stands for "Reuse Recorded Resolution."

Explain Git's "worktree add" command.

  • Answer: The git worktree add command creates a new linked working directory associated with the current repository. It enables you to work on multiple branches simultaneously.

How do you amend the last commit message in Git?

  • Answer: Use git commit --amend to modify the last commit message. This opens the default text editor for you to make changes.

Describe the purpose of Git "revert" vs. "reset."

  • Answer: git revert creates a new commit that undoes changes from a specific commit. git reset is used to reset the current branch to a specified commit, potentially discarding changes.

What is the Git "worktree move" command?

  • Answer: The git worktree move command allows you to move or rename an existing linked working directory created with git worktree add.

Explain the concept of Git "subversion" (svn) integration.

  • Answer: Git can integrate with Subversion repositories using git svn commands, providing a bridge between Git and Subversion version control systems.

How do you use Git to show the changes introduced by a specific commit?

  • Answer: Use git show <commit_hash> to display the changes introduced by a specific commit, including the commit message and diff.

What is Git "cherry-pick range"?

  • Answer: Git cherry-pick range involves picking a range of commits from one branch and applying them to another branch. It can be achieved using git cherry-pick <start_commit>^..<end_commit>.

Explain the purpose of the "git bisect" command.

  • Answer: The git bisect command helps in finding a specific commit that introduced a bug by using a binary search algorithm. It requires marking known good and bad commits to narrow down the problematic commit.

How does Git handle line endings, and what are autocrlf and core.autocrlf?

  • Answer: Git handles line endings based on platform conventions. The core.autocrlf setting controls automatic conversion between Unix and Windows line endings. autocrlf is used for automatic line ending conversion.

What is the purpose of "git blame"?

  • Answer: git blame shows the commit and author information for each line of a file, helping to identify who made changes and when.

How can you rename a Git branch?

  • Answer: To rename a Git branch, use git branch -m <new_branch_name> to rename the current branch, or git branch -m <old_branch_name> <new_branch_name> to rename a different branch.

Explain the difference between "merge" and "rebase" workflows.

  • Answer: The merge workflow combines changes from different branches, creating a new merge commit. The rebase workflow moves or combines a sequence of commits onto a new base, creating a linear history.

What is Git "filter-branch" used for?

  • Answer: git filter-branch is used to rewrite Git repository history by applying filters. It can be used for tasks like renaming files, removing sensitive data, or restructuring the repository.

Describe Git "force push" and its implications.

  • Answer: A force push (git push --force) is used to overwrite remote branch history with local changes. It should be used with caution, as it can lead to loss of commits and disrupt collaboration.

How do you squash multiple commits into a single commit during an interactive rebase?

  • Answer: During an interactive rebase, mark commits as "squash" or "s" to combine them into a single commit. The rebase process will prompt you to edit the commit message before finalizing the changes.

These questions cover various advanced Git topics, and the answers provide detailed explanations. Make sure to understand these concepts thoroughly for a successful Git interview.