2

History looks like follows:

master branch     1-2-3-4
                         \
feature branch            5-6-7

I realized changes 2, 3 and 4 should have also gone into my feature branch and would like to move them as shown below.

master branch     1
                   \
feature branch      2-3-4-5-6-7

How should I go about? Nobody else is using my repo, I haven't pushed the commits since creating the branch and all changes are committed.

0

1 Answer 1

4

Just delete the offending commits from master. They'll still be in your feature branch:

git checkout master
git reset --hard 1          # Replace '1' with the actual SHA for the commit 
git push -f origin master   # Sync to remote, if applicable
Sign up to request clarification or add additional context in comments.

6 Comments

Or git reset --hard HEAD~3
@TimBiegeleisen: Yes, of course. If you know you need to remove exactly 3 commits. I prefer the sha, because off-by-one errors are less likely... but in any case, be careful :)
Good solution, upvoted. Just to mention it, you might even skip the need to checkout master if you instead move the ref with git branch -f master 1 then push as you did.
@RomainValeri: Also valid. Although the OP says nothing's been pushed, so I added that final command mainly for the benefit of any future readers.
"Branches" are nothing more than pointers to a spot on the tree. So you don't actually delete the commits--you just move the master pointer from one commit to another.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.