0

I am working on a project using git for version control. One contributor for the project, is new to using git and accidentally pushed directly to master instead of an existing feature branch.

I would like to move these changes from master to the feature branch. I know how to undo the changes but I was wondering if there was a way for me to take the commits and move them directly to another branch.

kaineub
  • 162
  • 10
  • 1
    Possible duplicate of [How to fix committing to the wrong Git branch?](https://stackoverflow.com/questions/2941517/how-to-fix-committing-to-the-wrong-git-branch) – Obsidian Age Feb 22 '18 at 03:15
  • @ObsidianAge I don't think so. I did not make the changes that were committed and push to master. Someone else did and I want to fix their mistake. Would this still follow the same process? I want to make sure they still get credit for their contributions. – kaineub Feb 22 '18 at 03:19
  • @kaineub Branch is just a pointer to commit, nothing else. Commits does not need to move between branches. So, you need to update feature branch to point to desired commit (can be achieved by pushing the changes to feature branch), and reverting the master branch to where it were before wrong push. – user4003407 Feb 22 '18 at 03:48

1 Answers1

0

I'd use the following pattern:

git checkout feature/branch
git cherry-pick [sha1]

Sha1 is the hash of a specific commit on the master branch. Cherry pick each of the associated commits and once everything is moved over, you can revert master in the usual way and you'll retain the changes on your feature branch.

If the commits are sequential, you can also do this

git cherry-pick sha1x^...sha1n

This will cherry pick all of the commits between sha1x and sha1n, inclusive of sha1x. Note the ^ is what makes this inclusive.

Anthony L
  • 2,159
  • 13
  • 25