简体   繁体   中英

How do I change the git branch from which I branched?

I was working on a feature branch and I did a git checkout -b feature/other-feature , did some work, committed it and then pushed it to origin feature/other-feature on github.

When I created a pull request from the other-feature branch on github I realised that it showed a load of commits from the original feature branch - commits that should rightfully be merged to develop from the original branch.

I think what I did wrong was to omit git checkout develop before I did git checkout -b feature/other-feature and so what I think I want to do to correct this situation is rebase other-feature onto develop.

But given that I've pushed my mistake to origin and other people have clones of this repo, do I need to rebase or do something else?

Yes, you need to rebase your copy of feature/other-feature . However, in this case a simple git rebase develop will probably not work, because you branched off another feature branch, instead of directly off develop . You need to use rebase --onto :

git rebase --onto develop feature/earlier-feature feature/other-feature

Here, feature/earlier-feature is the feature branch you had checked out when you die the original git checkout -b feature/other-feature .

This rebase will:

  1. Take the commits in feature/other-feature which are not in feature/earlier-feature .
  2. Re-apply these commits to develop .

You should end up with a changed branch feature/other-feature which is now based on develop .


Notes

  • Because of the rebasing you will have to force-push your branch to your GitHub repository. This is not a problem, as long as you have the only clone of your repository . If you use the repo with others, things are more difficult (see below).
  • The rebase may cause conflicts. You will have to resolve these manually.
  • Further reading: The concepts behind rebasing, and the meaning of rebase --onto are well explained in the book "Pro Git". See chapter 3.6, Git Branching - Rebasing .

If others have cloned your repository

You write

But given that I've pushed my mistake to origin and other people have clones of this repo, do I need to rebase or do something else?

You can still rebase as described above. However, you must not force-push the rebased branch under the old name. Instead, create a copy of the branch:

git checkout -b feature/other-feature-2 feature/other-feature

This will create a new branch feature/other-feature-2 that is an exact copy of feature/other-feature . You can then rebase the new branch, and push it under the new name. Then tell everyone that feature/other-feature has been superseded by feature/other-feature-2 , and delete feature/other-feature on GitHub. Having to tell everyone is the price you pay for rebasing :-).

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