Я недавно сделал то же самое, когда я случайно совершил изменение в master, когда я должен был совершить в другой ветке. Но я ничего не толкал.
Если вы только что приняли на себя неверную ветку, и с тех пор ничего не изменили и не продвинулись в репо, вы можете сделать следующее:
// rewind master to point to the commit just before your most recent commit.
// this takes all changes in your most recent commit, and turns them into unstaged changes.
git reset HEAD~1
// temporarily save your unstaged changes as a commit that's not attached to any branch using git stash
// all temporary commits created with git stash are put into a stack of temporary commits.
git stash
// create other-branch (if the other branch doesn't already exist)
git branch other-branch
// checkout the other branch you should have committed to.
git checkout other-branch
// take the temporary commit you created, and apply all of those changes to the new branch.
//This also deletes the temporary commit from the stack of temp commits.
git stash pop
// add the changes you want with git add...
// re-commit your changes onto other-branch
git commit -m "some message..."
ПРИМЕЧАНИЕ: в приведенном выше примере я перематывал 1 коммит с git reset HEAD ~ 1. Но если вы хотите перемотать n коммитов, вы можете выполнить git reset HEAD ~ n.
Кроме того, если вы в конечном итоге зафиксировали неправильную ветвь, а также написали еще немного кода, прежде чем поняли, что зафиксировали неправильную ветку, вы можете использовать git stash для сохранения текущей работы:
// save the not-ready-to-commit work you're in the middle of
git stash
// rewind n commits
git reset HEAD~n
// stash the committed changes as a single temp commit onto the stack.
git stash
// create other-branch (if it doesn't already exist)
git branch other-branch
// checkout the other branch you should have committed to.
git checkout other-branch
// apply all the committed changes to the new branch
git stash pop
// add the changes you want with git add...
// re-commit your changes onto the new branch as a single commit.
git commit -m "some message..."
// pop the changes you were in the middle of and continue coding
git stash pop
ПРИМЕЧАНИЕ: я использовал этот сайт в качестве ссылки
https://www.clearvision -cm.com / блог / что-к-делать-когда-вы фиксации-к-неправильности мерзавца-ветви /