I made a number of changes that could break the code. I thought I was in a branch but was in master when I committed. Is there any way to move the 4 local commits to a branch locally and then push?
Going by the details you said (4 commits), you could do this:
git branch new-branch-name-here
# new commits on branch
git checkout master
git reset HEAD~4
# move HEAD (master) 4 commits back, commits are no longer on master
# note: that's a ~ (tilde, above your Tab), not a - (dash).
git push origin new-branch-name-here
# push new branch with correct commits to remote (assumed origin)
git push -f origin master
# if you already pushed master before, clear commits from remote
# otherwise, this can be skipped if master wasn't yet pushed remotely
The -f
for pushing master
is required, otherwise your push will get rejected by the server.
In general, changing commits on a branch in Git can be done in three easy steps:
-f
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With