linux932
linux932

Reputation: 963

How to create a clean master branch from the origin project of a fork?

Alright, so I accidentally pushed commits to the master branch of my fork, and submitted the branch for a PR.

I need to create another PR now, but I don't know anyway I can create a new branch based off a unmodified master branch (because I accidentally pushed commits there). How can I create a branch based off the master branch of the forks origin project?

To clarify: I don't have a clean copy of the master branch to create another branch based off it. What are some soultions I can use to fix this?

Upvotes: 1

Views: 306

Answers (2)

Lunyx
Lunyx

Reputation: 3284

You can create a new branch off a previous commit with

git checkout -b <new_branch_name> <commit hash>

However, it seems that the bigger problem is that you now have commits that are in master that shouldn't be there. Short of a force push to remove them, the only other option you have is to revert those commits. You mention that you are on your own fork, which may make force pushing a better option if no one else is also working on your fork. If you want to do that to fix your master, you can do

git reset --hard <commit hash to revert to>

Upvotes: 1

mintychai
mintychai

Reputation: 311

You can go to a previous commit and branch from there. Go to your master branch and use git log to see your commit history and copy the hash of the commit you want to go back to (one before your latest, it looks like).

Use git checkout <hash> to get on that commit. Then git branch <new branch name> to create a new branch based off the clean master branch.

Upvotes: 1

Related Questions