Press "Enter" to skip to content

You committed files to the wrong branch (master), and now you want to apply your changes to the right one (development)

Last updated on August 28, 2017

I wanted to share a scenario and solution for a problem I am seeing arise in the dbatools project:

  • Commits are registered against the “wrong” branch
    • A pull request is submitted
    • The file comparison shows hundreds of changes instead of 1-2

There are many ways this can happen, but usually due to forgetting to create a feature branch, or accidentally creating said branch from master instead of development.

To fix this problem, you will want check the log of commits to determine the hash of the command you want, and then create a new branch (from the correct source) and apply those commits.

git cherry-pick is a command that allows you to apply a commit on a different branch than what you what you originally intended.

To fix a pull request where you have more files than you want:

  • Make sure you are in the branch you had a problem.
    • git status to see your current branch and git checkout branchname will change your context to the branch name you want.
    • Use the git log command to see a list of commits, you should see your changes and a relevant hash.
    • Record the first 10-12 characters of the commit hashes of your changes (technically you only need enough of the values to be unique.)
    • Checkout the correct source branch (development) with git checkout development
    • Create the correct feature branch with git checkout -b new-feature-name (this will also check it out for you and set it to be the current branch)
    • Cherry pick the commits with git cherry-pick hash
    • Push the new branch to your repository git push origin new-feature-name
    • You should now be able to Pull Request/Compare your code.

A note on comparisons, any time you make a new Pull Request GitHub will automatically list the files changed and the number of commits you put in place, if this seems different than what you did, give me a jingle and we can see which part of this process we want to do 🙂

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.