Back to Git & Dev Tools
2026-02-217 min read

TRANSPLANTING A TOPIC BRANCH WITH --ONTO (Git & Dev Tools)

Learn TRANSPLANTING A TOPIC BRANCH WITH --ONTO (Git & Dev Tools) step by step with clear examples and exercises.

Title: Transplanting a Topic Branch with --onto (Git & Dev Tools)

Why This Matters

When collaborating on complex projects in Git, it is essential to understand how to effectively manage multiple branches. The --onto option provides a powerful tool for transplanting changes from one branch to another without creating conflicts or unnecessary merge commits. By mastering this technique, you can maintain a clean and organized Git repository, making it easier to track changes, collaborate with team members, and roll out new features more efficiently.

In addition, using --onto can help reduce the number of merge commits in your project history, which can make it simpler to visualize and understand the evolution of your codebase over time. This is particularly important for projects with a large number of contributors or complex workflows.

Prerequisites

  • Basic understanding of Git commands such as git branch, git checkout, git merge, git rebase, and git cherry-pick
  • Familiarity with creating, switching, and deleting branches in Git
  • A working project with multiple branches
  • Experience resolving Git conflicts when they arise
  • Understanding of Git's branching strategies such as feature branches, pull requests, and merge strategies (e.g., fast-forward merges, merge commits)

Core Concept

The --onto option is a versatile feature in Git that allows you to transplant a topic branch onto another branch. This operation copies all commits from the source branch (also known as the donor branch) and applies them onto the destination branch (also known as the recipient branch). The key difference between using --onto and regular merging is that it does not create merge commits, which can help keep your repository cleaner and easier to manage.

Here's a step-by-step guide on how to use the --onto option:

  1. Ensure you have both branches checked out locally. For example:
$ git checkout source-branch # Switch to the source branch
$ git checkout destination-branch # Switch to the destination branch
  1. Use the git merge --onto command to transplant the topic branch onto the current one:
$ git merge --onto destination-branch source-branch
  1. Git will now apply all commits from source-branch onto destination-branch, preserving the commit history and creating a linear sequence of changes.
  2. If there are any conflicts during this process, you'll need to resolve them manually using standard Git conflict resolution techniques.
  3. After resolving any conflicts, you can use git add . to stage all changes, followed by git commit -m "Resolved conflicts and transplanted feature branch onto master" to create a new commit that represents the combined changes from both branches.
  4. If necessary, you may want to perform additional cleanup or rebase operations using git rebase to further refine your repository's history.
  5. After resolving any conflicts and committing the changes, you can use git push to update the remote repository with your changes.

Worked Example

Let's consider a simple example where we have two branches: master and feature. The master branch contains the latest stable version of our project, while the feature branch includes new features or bug fixes that are not yet ready for production. To transplant the changes from feature onto master, follow these steps:

  1. Checkout the feature branch:
$ git checkout feature
  1. Checkout the master branch:
$ git checkout master
  1. Use the git merge --onto master feature command to transplant the changes from feature onto master:
$ git merge --onto master feature
  1. If there are any conflicts, resolve them manually and commit the changes:
$ git add . # Stage all changes
$ git commit -m "Resolved conflicts and transplanted feature branch onto master"
  1. Perform additional cleanup or rebase operations if necessary:
$ git rebase -i HEAD~n # Replace 'n' with the number of commits you want to squash or reword
  1. Push the updated master branch to the remote repository:
$ git push origin master

Common Mistakes

  1. Not checking out both branches before using --onto: It's essential to have both the source and destination branches checked out locally before performing the transplant operation.
  2. Forgetting to resolve conflicts: If there are any conflicts during the transplant process, you must resolve them manually before committing the changes.
  3. Not updating the remote repository: After resolving any conflicts and committing the changes, don't forget to push the updated branch to the remote repository using git push.
  4. Using --onto instead of regular merge when it's not necessary: While --onto can help keep your repository cleaner, it may not always be the best choice. Use it judiciously and consider the impact on your project history before making a decision.
  5. Not performing additional cleanup or rebase operations: After transplanting changes using --onto, you may want to perform additional cleanup or rebase operations to ensure your repository's history is well-organized and easy to understand.
  6. Ignoring the impact on merge strategies: Using --onto can affect how Git handles merges in the future, so be mindful of this when working with branches that use different merge strategies (e.g., fast-forward merges, merge commits).
  7. Not preserving original branch history: When using --onto, Git will apply all commits from the source branch onto the destination branch, potentially overwriting its original commit history. Be cautious when using this option and consider backing up or preserving important branches before proceeding.
  8. Misunderstanding the purpose of --onto: Some developers may confuse --onto with other Git commands like git rebase, git cherry-pick, or even regular merging. Understand the unique benefits and limitations of each command to make informed decisions when managing your Git repository.

Practice Questions

  1. You have two branches, feature-1 and master. Both branches contain different changes that you want to combine into one branch called integration. How would you use Git's --onto option to achieve this?
  2. Suppose you accidentally transplanted the wrong topic branch onto your current branch using git merge --onto. What command can you use to revert the changes and return to the previous state of your repository?
  3. You are working on a feature branch called feature-2 and want to transplant it onto the master branch without creating any merge commits. How would you do this using Git's --onto option, while also ensuring that the changes from both branches are properly combined and any necessary cleanup is performed?
  4. You have a feature branch called feature-3 with multiple commits, but only want to transplant specific commits onto another branch called master. How would you use Git's --onto option to achieve this?
  5. Suppose there is a conflict during the transplant process using git merge --onto. How can you view and resolve the conflicts manually before committing the changes?
  6. You are working on a project with multiple developers, and one of your teammates has created a branch called feature-4 that includes several important bug fixes. However, you want to transplant only specific commits from this branch onto your current branch without affecting the rest of the history. How would you use Git's --onto option to achieve this?
  7. You have a project with a complex history and multiple branches, and you want to simplify the repository structure by consolidating some branches and discarding others. How can you use Git's --onto option in conjunction with other commands like git rebase or git cherry-pick to achieve this goal?

FAQ

  1. Why should I use --onto instead of regular merge?
  • Using --onto can help keep your repository cleaner by avoiding the creation of merge commits, making it easier to track changes and manage project history. It's particularly useful when you want to combine changes from a feature branch into another branch without creating unnecessary merge commits.
  1. Can I use --onto with remote branches?
  • Yes, you can use git merge --onto with both local and remote branches. However, you'll need to fetch the remote branch first using git fetch.
  1. What happens if there are conflicts during the transplant process?
  • If there are any conflicts during the transplant process, Git will automatically detect and prompt you to resolve them manually. You can use standard Git conflict resolution techniques to address any issues that arise.
  1. Can I use --onto with multiple source branches at once?
  • No, --onto can only be used with a single source branch at a time. If you need to combine changes from multiple branches, you'll have to perform separate transplant operations or consider using other Git tools like git rebase.
  1. How can I view the commit history after using --onto?
  • To view the commit history of the destination branch after using --onto, use the git log command:
$ git log --oneline
  1. Can I undo or revert changes made with --onto?
  • If you want to undo or revert changes made with --onto, you can create a new branch from the destination branch before the transplant operation, and then merge in any necessary changes manually using standard Git commands like git merge or git revert.
  1. How does --onto affect Git's merge strategies?
  • Using --onto can impact how Git handles merges in the future, particularly when working with branches that use different merge strategies (e.g., fast-forward merges, merge commits). Be mindful of this when using --onto and consider the potential implications on your project workflow.
  1. Can I preserve the original branch history when using --onto?
  • To preserve the original branch history when using --onto, you can create a backup of the source branch before transplanting its commits onto another branch. This will allow you to revert changes or access the original branch history if needed.
TRANSPLANTING A TOPIC BRANCH WITH --ONTO (Git & Dev Tools) | Git & Dev Tools | XQA Learn