How to return a commit and create a new branch from these changes?

Last Sunday, and I made a bad mistake. I made a mistake and pressed right into the main branch, where I had to create a branch and instead transfer the changes to a new branch.

Therefore, I could do git revert SHAto return the last commit with a new commit.

However, what about my changes, I do not want to lose them.

Should I create a branch from an already modified master, for example git checkout -b feature, and then return the master branch?

But what happens as soon as I merge the function into master, does it recognize that this commit was returned to Master earlier and eliminate it? git merge feature

Btw, there is no problem rewriting the story, as I am the only developer working on this project. Therefore, I would consider a hard reset if its a better choice.

thanks for the advice

+4
source share
1 answer

If you are the only developer, you can do a hard reset. If abc123SHA1 is a random commit, then do:

git checkout master
git branch feature-branch
git reset --hard abc123^

This will save your new commit in a new branch feature-branch(without switching to this new branch yet), and then rewind masterto the previous commit. Then you can push the corrected branch masterusing git push -f origin master.

+5
source

All Articles