Git is a distributed version control system that allows teams to work on the same projects without stepping on each other’s toes. It’s one of the most popular tools for developers worldwide.
In this blog post, we will dive deep into the essential concepts of Git through a set of 25 multiple-choice questions. Each question will have an answer and an explanation to further your understanding.
1. What is Git?
Answer:
Explanation:
Git is a distributed version control system that allows multiple users to track and manage changes in software projects.
2. Which command initializes a new Git repository?
Answer:
Explanation:
The git init command is used to initialize a new Git repository and begin tracking an existing directory.
3. How can you view the commit history in Git?
Answer:
Explanation:
The git log command displays the commit history, showing various details about each commit.
4. Which command adds changes to the staging area in Git?
Answer:
Explanation:
The git add command stages the changes for commit, which means it tracks the new and modified files to be committed.
5. Which command shows the status of changes in the repository?
Answer:
Explanation:
The git status command displays the list of changed files that are staged, unstaged, and untracked.
6. How do you commit the staged changes?
Answer:
Explanation:
The git commit command captures a snapshot of the changes made and saves it to the version history with a unique ID.
7. What does the .git directory store?
Answer:
Explanation:
The .git directory contains all the metadata and the object database for the repository. It’s the heart of Git, and the repository itself.
8. Which command creates a new branch in Git?
Answer:
Explanation:
Using git branch <branch-name>, you can create a new branch. This doesn’t switch to the new branch; you’d use git checkout or git switch for that.
9. How do you switch to a different branch in Git?
Answer:
Explanation:
The git switch <branch-name> command allows you to switch to a different branch. Before Git version 2.23, the common approach was git checkout <branch-name>.
10. Which command merges one branch into another?
Answer:
Explanation:
The git merge command integrates changes from one branch into another. This is commonly used when features or bug fixes from one branch need to be brought into the main branch.
11. What is a merge conflict?
Answer:
Explanation:
A merge conflict occurs when there are changes in the same part of a file in both the current branch and the branch to be merged. Git cannot decide which change should take precedence, so it asks the user to resolve the conflict.
12. Which command is used to clone a remote repository?
Answer:
Explanation:
The git clone command is used to clone (or copy) a remote repository onto your local machine.
13. Which command connects a local repository to a remote server?
Answer:
Explanation:
The git remote add command connects a local repository to a remote server. This is often followed by a URL representing the location of the remote repository.
14. How do you fetch the latest updates from a remote repository without merging them?
Answer:
Explanation:
The git fetch command fetches updates from a remote repository but doesn’t merge them. It allows you to review changes before integrating them.
15. How do you push changes from a local branch to a remote repository?
Answer:
Explanation:
The git push command pushes changes from your local branch to a remote repository. This updates the remote branch with your local changes.
16. What does the HEAD in Git represent?
Answer:
Explanation:
In Git, HEAD is a special pointer or reference that points to the currently checked-out commit in the repository.
17. How can you undo the most recent commit?
Answer:
Explanation:
The git reset HEAD~1 command moves the current branch pointer back to the previous commit, effectively undoing the most recent commit. Note that there are different modes (like –soft, –hard) which decide the fate of changes during this operation.
18. Which command is used to stash changes in Git?
Answer:
Explanation:
The git stash command temporarily saves changes that are not yet ready for a commit, allowing you to switch to another branch without committing the current changes.
19. How do you create a tag in Git?
Answer:
Explanation:
In Git, the git tag command creates a reference point (or marker) for specific points in your project history. It’s often used to mark release points.
20. Which of the following is not a valid merge strategy in Git?
Answer:
Explanation:
Git supports various merge strategies like fast-forward, recursive, and octopus. “squid” is not a merge strategy in Git.
21. Which command lists all the branches in a Git repository?
Answer:
Explanation:
The git branch command, when used without any arguments, lists all the branches in a Git repository.
22. What does the git cherry-pick command do?
Answer:
Explanation:
The git cherry-pick command allows you to take a commit from another branch and apply it to your current branch.
23. How do you remove untracked files from your working directory?
Answer:
Explanation:
The git clean command removes untracked files from your working directory, ensuring your workspace is tidy.
24. What is a bare repository in Git?
Answer:
Explanation:
A bare repository in Git is a repository that contains only the .git directory and no working directory. It’s typically used for shared repositories and remotes.
25. How do you view the differences between the working directory and the last commit?
Answer:
Explanation:
The git diff command shows the differences between the working directory and the last commit. It’s a useful tool to see the changes made before committing them.