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 statusto see your current branch and
git checkout branchnamewill change your context to the branch name you want.
- Use the
git logcommand 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 🙂