Question

We have a project where one of the developers made a few commits into the master branch -- instead of creating a separate branch off of master -- and broke things. How do I:

  1. Undo those commits in master
  2. Make a branch from master
  3. Re-commit (or whatever the right git term for this is) those commits into the new branch?
Was it helpful?

Solution

You could make 1 & 2, but you could simple take care about 2&3, and then make 1.

  1. Make a branch from master
  2. Re-commit (or whatever the right git term for this is) those commits into the new branch?

Assuming you have those bad commits on your master branch in your working copy:

Simply create and switch to new branch:

git checkout -b name_of_new_branch

and then push it to repo:

git push -u origin name_of_new_branch 

your colleague now should be able to do:

git fetch origin
git checkout name_of_new_branch

  1. Undo those commits in master
git checkout master
git reset --hard HEAD~N

where N is number of bad commits of your colleague.

then, if you could override branches in your repo (default setting in github), then you could:

git push origin master --force

Please be very careful with this options. if I were in your shoes I will wait till someone confirm, that those commands are correct ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top