简体   繁体   中英

git: how to move committed changes from master to branch?

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?

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 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM