WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Git · Expert · question 76 of 100

Can you explain the concept of "grafts" in Git and provide some use cases for them?

📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

In Git, a "graft" is a way to manually specify parent-child relationships between commits. This can be useful in situations where the repository history needs to be modified, for example, to combine two separate repositories into one or to correct mistakes in the commit history.

Here’s how grafts work:

When Git generates the commit graph for a repository, it uses the parent-child relationships between commits to build the graph. Normally, these relationships are determined automatically based on the order of commits in the repository. However, with grafts, you can manually specify parent-child relationships between commits.

To create a graft, you create a file named .git/info/grafts in the repository, and add lines to the file specifying the parent-child relationships between commits. Each line in the file specifies a parent commit and a child commit, separated by a space. For example:

    <new-parent-commit> <old-child-commit>

This tells Git that the <new-parent-commit> should be the parent of the <old-child-commit>. When Git generates the commit graph, it will use these relationships to build the graph.

Here are some use cases for grafts:

Combining repositories: If you have two separate Git repositories that you want to combine into one, you can use grafts to specify the parent-child relationships between the two repositories. This allows Git to generate a single commit graph that includes the history of both repositories.

For example, suppose you have two repositories: repo1.git and repo2.git. You want to combine them into a single repository combined.git. You can do this by creating a graft that specifies the parent-child relationship between the final commit of repo1.git and the first commit of repo2.git. This allows Git to generate a single commit graph that includes the history of both repositories.

Correcting mistakes in the commit history: If you discover a mistake in the commit history, such as a commit that was accidentally attributed to the wrong author, you can use grafts to correct the mistake. By specifying the correct parent-child relationships between the commits, you can modify the commit history without having to rewrite the entire repository.

For example, suppose you have a commit in the repository that was accidentally attributed to the wrong author. You can correct this by creating a graft that specifies the correct parent-child relationship between the commit and its parent. This allows Git to generate a new commit graph that reflects the corrected history.

Reordering commits: If you want to reorder commits in the repository, you can use grafts to specify the new parent-child relationships between the commits. This allows Git to generate a new commit graph that reflects the reordered history.

For example, suppose you have a series of commits in the repository that you want to reorder. You can do this by creating a graft that specifies the new parent-child relationships between the commits. This allows Git to generate a new commit graph that reflects the reordered history.

Converting repositories from other version control systems: If you are converting a repository from another version control system, such as Subversion, to Git, you can use grafts to specify the parent-child relationships between the commits in the new Git repository. This allows Git to generate a commit graph that reflects the history of the original repository.

In summary, grafts are a way to manually specify parent-child relationships between commits in Git. They can be useful in situations where the repository history needs to be modified, such as combining repositories or correcting mistakes in the commit history. By using grafts, you can modify the commit history without having to rewrite the entire repository.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Git interview — then scores it.
📞 Practice Git — free 15 min
📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

All 100 Git questions · All topics