Challenge Your GIT Version Control IQ with this Quiz

CHAIRI Chaimae
6 min readFeb 19, 2024

--

oldwyomingpizza.blogspot.com

Git is an essential part of a developer’s life. Mastering it makes us a valuable asset. I remember when I wanted to test and verify it as a skill in the LinkedIn assessment; I failed it twice, but that is not a barrier but a motivation to learn more about it. To join me on this challenge, I’ve found some questions to test if you’re a Git Maestro or a Learner Luminary. So, please, when you arrive at a question, give yourself some time to think about it for a moment before looking at the answer.

Let’s get started!

Part 1: Branches

What is a git branch?

digitalvarys.com

A branch is a separate line of development that allows you to work on a specific set of changes independently from the main codebase. It enables parallel development and isolation of features or bug fixes before merging them back into the main branch.

How do you create a new branch in Git?

To create a new branch in Git, you can use the following command:

git branch branch_name

Replace "branch_name" with the desired name for your new branch. After creating the branch, you can switch to it using:

git checkout branch_name

Alternatively, you can combine branch creation and checkout into a single command:

git checkout -b branch_name

This will both create and switch to the new branch in one step.

How do you delete a branch in Git?

To delete a branch in Git, you can use the following command:

git branch -d branch_name

Replace branch_name with the name of the branch you want to delete. The `-d` option stands for "delete." If the branch contains changes that haven't been merged, Git will prevent deletion to avoid potential data loss.

git branch -D branch_name

The `-D` option forces the deletion of the branch, even if it contains unmerged changes. Use it with caution, as unrecoverable data loss may occur.

How do you switch between Git branches?

To switch between Git branches, you can use the following command:

git checkout branch_name

Replace branch_name with the name of the branch you want to switch to. Alternatively, you can use the git switch command:

git switch branch_name

What happens to a branch commits after you delete the branch?

When you delete a branch in Git, the branch itself is removed, but the commits associated with that branch are not immediately deleted. Git retains the commits in the repository. The deletion of the branch simply means you are no longer working within that branch or able to reference it by name.

If the deleted branch was merged into another branch before deletion, those commits are still part of the branch where the merge occurred. If the branch wasn't merged and you used `git branch -D` to forcefully delete it, the commits may become unreachable over time as they are no longer part of any branch.

However, Git has a garbage collection mechanism that cleans up unreferenced commits periodically. So, eventually, if there are no references pointing to those commits, they will be removed during Git's garbage collection process, making the space available for reuse.

Part 2: Merging

What is git merge?

In Git, the git merge command is used to combine changes from one branch into another. It essentially integrates the changes made in a source branch into a target branch. This can be particularly useful when working on features or bug fixes in separate branches and wanting to bring those changes into the main development branch.

The basic syntax for merging is:

git checkout target_branch
git merge source_branch

These commands checks out the target branch and then merges the changes from the source branch into it. Git automatically tries to merge the changes, but conflicts may arise if changes in both branches affect the same lines of code. In such cases, manual intervention is needed to resolve the conflicts.

What are 3 most useful git merge strategies?

Git provides several merge strategies to handle combining changes from different branches. Here are three commonly used and useful merge strategies:

1.Merge (default):
- Command: `git merge branch_name`
This is the default strategy. It performs a three-way merge, combining changes from the specified branch into the current branch. If there are no conflicts, Git automatically creates a new merge commit. If conflicts arise, manual resolution is needed.

2. Rebase:
- Command: `git rebase branch_name`
Rebase is an alternative to merging that rewrites the commit history. It moves the entire branch to begin on the tip of the specified branch. This results in a linear history. However, it should be used with caution as it rewrites commit history, which can cause issues if the branch is already shared with others.

3. Squash and Merge:
This strategy is often used in pull requests or code reviews. It combines multiple commits into a single commit before merging. It helps maintain a clean and concise history by reducing the number of commits.

Each strategy has its use case, and the choice depends on the development workflow, collaboration practices, and the desired structure of the commit history.

What is the difference between a merge commit and a regular commit?

A merge commit and a regular commit are conceptually similar in that they both represent changes to a Git repository. However, there are key differences between the two:

1. Merge Commit:
- Description: A merge commit is a special type of commit that results from merging two or more branches in Git. It has two or more parent commits, representing the history of the merged branches.
-Creation: Generated automatically when you run `git merge` to combine changes from one branch into another.
- Purpose: Captures the integration of changes from one branch into another, maintaining a clear record of the merge point and the branches involved.

2. Regular Commit:
- Description: A regular commit is a standalone record of changes made to the repository. It has a single parent commit, representing the commit from which the changes originated.
- Creation: Generated when you use `git commit` to save changes to the repository.
- Purpose: Represents individual changes, bug fixes, or new features introduced to the codebase. Regular commits form the linear history of a branch.

Part 3 : Garbage collection

What is Git garbage collector?

In Git, the garbage collector is a mechanism responsible for cleaning up and optimizing the repository by removing unreferenced or dangling objects. Git uses a structure called the "object database" to store data, including commits, trees, and blobs. When you create or modify objects (like making commits), Git retains references to these objects.

However, over time, as branches are created, merged, or deleted, some objects may become unreferenced or dangling, meaning they are no longer part of any branch or tag. The garbage collector identifies and removes these unreferenced objects, freeing up space and improving the efficiency of the repository.

The garbage collection process is automatic and is usually triggered in the background by various Git commands. Additionally, you can manually run the garbage collector using this command:

git gc

This command performs garbage collection, optimizing the repository by compacting the object database and removing unreferenced objects.

What is the difference between git gc and git gc --auto?

The git gc command is used to perform garbage collection in Git, optimizing the repository by compacting the object database and removing unreferenced objects.

On the other hand, git gc --auto is used to enable automatic garbage collection. This means that Git will periodically run garbage collection in the background as needed, without requiring explicit user intervention. It allows Git to manage the cleanup process automatically.

So, the main difference is in the invocation:

- `git gc` performs a one-time garbage collection when explicitly called.
- `git gc --auto` enables automatic garbage collection, allowing Git to manage cleanup in the background as required.

In most cases, users do not need to run `git gc` manually, as Git takes care of garbage collection automatically during certain operations. The `--auto` option is often used to ensure that Git performs periodic cleanup without requiring manual intervention.

How did you do? How many questions did you respond to?

--

--

CHAIRI Chaimae
CHAIRI Chaimae

Written by CHAIRI Chaimae

This space's to document my learning and track my progress. Hope it helps someone somewhere. You're welcomed!

No responses yet