Learn and master the top 40 Git interview questions along with detailed answers and code snippets

  1. What is Git?
    Git is a distributed version control system designed to track changes in files and coordinate work among multiple developers.

    It allows for efficient collaboration, branching, merging, and version control of source code.

  2. How do you initialize a Git repository in a directory?
    To initialize a Git repository, navigate to the desired directory and run the following command:
git init
  1. How do you add files to the staging area in Git?
    To add files to the staging area, use the following command:
git add <file1> <file2> ...

You can also use git add . to add all the files in the current directory.

  1. How do you commit changes in Git?
    To commit changes, use the following command:
git commit -m "Commit message"

This will create a new commit with the changes and a descriptive commit message.

  1. How do you create a new branch in Git?
    To create a new branch, use the following command:
git branch <branch_name>

This creates a new branch with the given name based on the current branch.

  1. How do you switch between branches in Git?
    To switch between branches, use the following command:
git checkout <branch_name>

This will switch to the specified branch.

  1. How do you merge branches in Git?
    To merge a branch into the current branch, use the following command:
git merge <branch_name>

This merges the specified branch into the current branch.

  1. How do you resolve merge conflicts in Git?
    When a merge conflict occurs, Git marks the conflicting sections in the files. You need to manually resolve the conflicts by editing the files. After resolving the conflicts, you can use the following command to complete the merge:
git add <file1> <file2> ...
git commit -m "Merge branch_name into current_branch"
  1. How do you view the commit history in Git?
    To view the commit history, use the following command:
git log

This will display the commit history with information such as commit hash, author, date, and commit message.

  1. How do you push changes to a remote Git repository?
    To push changes to a remote repository, use the following command:
git push <remote_name> <branch_name>

Replace <remote_name> with the name of the remote repository, such as origin, and <branch_name> with the branch you want to push.

  1. How do you clone a Git repository?
    To clone a Git repository, use the following command:
git clone <repository_url>

This will create a copy of the repository on your local machine.

  1. How do you update your local repository with the latest changes from a remote repository?
    To update your local repository, use the following command:
git pull <remote_name> <branch_name>

This fetches the latest changes from the remote repository and merges them into your local branch.

  1. How do you revert a commit in Git?
    To revert a commit, use the following command:
git revert <commit_hash>

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

  1. How do you discard local changes in Git?
    To discard local changes and revert to the last committed state, use the following command:
git checkout -- <file1> <file2> ...

This discards the changes in the specified files.

  1. How do you create and apply a Git patch?
    To create a Git patch, use the following command:
git diff > patchfile.patch

This creates a patch file containing the differences between the current state and the previous commit. To apply a patch, use the following command:

git apply patchfile.patch
  1. How do you stash changes in Git?
    To stash changes and save them for later use, use the following command:
git stash

This saves the changes and reverts the working directory to the last committed state.

  1. How do you apply a stash in Git?
    To apply a stash, use the following command:
git stash apply

This applies the most recent stash and restores the changes.

  1. How do you rename a branch in Git?
    To rename a branch, use the following command:
git branch -m <old_branch_name> <new_branch_name>

This renames the specified branch.

  1. How do you delete a branch in Git?
    To delete a branch, use the following command:
git branch -d <branch_name>

This deletes the specified branch.

  1. How do you tag a specific commit in Git?
    To tag a commit, use the following command:
git tag <tag_name> <commit_hash>

This creates a new tag with the specified name at the given commit.

  1. How do you view the differences between two branches in Git?
    To view the differences between two branches, use the following command:
git diff <branch1>..<branch2>

This shows the differences between the two branches.

  1. How do you cherry-pick a commit in Git?
    To cherry-pick a commit from one branch to another, use the following command:
git cherry-pick <commit_hash>

This applies the changes made in the specified commit to the current branch.

  1. How do you rebase a branch in Git?
    To rebase a branch onto another branch, use the following command:
git rebase <branch_name>

This moves the commits of the current branch onto the specified branch.

  1. How do you configure Git username and email?
    To configure Git username and email, use the following commands:
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"

Replace "Your Name" with your name and "youremail@example.com" with your email address.

  1. How do you ignore files or directories in Git?
    To ignore files or directories, create a .gitignore file in the root of the repository and list the files or directories to be ignored. For example:
# .gitignore
build/
temp.txt

This will ignore the build/ directory and the temp.txt file.

  1. How do you create an annotated tag in Git?
    To create an annotated tag, use the following command:
git tag -a <tag_name> -m "Tag message"

This creates an annotated tag with the specified name and a descriptive message.

  1. How do you view the list of tags in Git?
    To view the list of tags, use the following command:
git tag

This displays a list of all the tags in the repository.

  1. How do you revert a merge commit in Git?
    To revert a merge commit, use the following command:
git revert -m 1 <merge_commit_hash>

This creates a new commit that undoes the changes made in the merge commit.

  1. How do you show the commit that introduced a specific line of code?
    To show the commit that introduced a specific line of code, use the following command:
git blame <file_path> -L <line_number>,<line_number>

This shows the commit hash and author for each line in the specified range.

  1. How do you configure Git to use a proxy server?
    To configure Git to use a proxy server, use the following command:
git config --global http.proxy <proxy_url>

Replace <proxy_url> with the URL of the proxy server.

See also  Top 30 Jest Interview Questions and Answers

31. How do you revert a commit and keep the changes as uncommitted modifications?
To revert a commit and keep the changes as uncommitted modifications, use the following command:

git revert --no-commit <commit_hash>
git reset

This reverts the commit but keeps the changes in the working directory as uncommitted modifications.

32. How do you rename a file in Git?
To rename a file, use the following command:

git mv <old_file_name> <new_file_name>

This renames the file and stages the change in Git.

33. How do you discard local commits in Git?
To discard local commits that have not been pushed yet, use the following command:

git reset HEAD~<number_of_commits>

Replace <number_of_commits> with the number of commits you want to discard.

34. How do you view the changes introduced by a commit in Git?
To view the changes introduced by a commit, use the following command:

git show <commit_hash>

This displays the commit information along with the diff of changes.

35. How do you move the most recent commits to a new branch in Git?
To move the most recent commits to a new branch, use the following commands:

git branch <new_branch_name>
git reset --hard HEAD~<number_of_commits>

Replace <new_branch_name> with the name of the new branch and <number_of_commits> with the number of commits you want to move.

36. How do you configure Git to use SSH instead of HTTPS for remote repositories?
To configure Git to use SSH instead of HTTPS, use the following command:

git remote set-url origin git@github.com:<username>/<repository>.git

Replace <username> with your GitHub username and <repository> with the name of the repository.

See also  Integration Testing Complete Tutorial

37. How do you squash multiple commits into a single commit in Git?
To squash multiple commits into a single commit, use the following command:

git rebase -i HEAD~<number_of_commits>

This opens an interactive rebase session where you can choose to squash or fixup commits.

38. How do you view the differences between a file in the working directory and the last commit in Git?
To view the differences between a file in the working directory and the last commit, use the following command:

git diff <file_path>

This shows the differences between the file in the working directory and the last committed version.

39. How do you restore a deleted file in Git?
To restore a deleted file, use the following command:

git checkout <commit_hash> -- <file_path>

Replace <commit_hash> with the commit that contains the deleted file and <file_path> with the path to the deleted file.

40. How do you set up Git to use a specific editor for commit messages?
To set up Git to use a specific editor for commit messages, use the following command:

git config --global core.editor <editor_command>

Replace <editor_command> with the command for your desired editor, such as vim, nano, or code.