Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - Moving Pushed Commits to a Different Branch

People also ask

Can I move commits to another branch?

If you want to move commits to an existing branch you need to merge your changes into the existing branch before executing git reset --hard HEAD~3 (see Moving to an existing branch above). If you don't merge your changes first, they will be lost.

How do I move multiple commits from one branch to another?

You can do this with multiple commits too, just cherry pick several, then reset back to the last commit you want to keep. The process is the same if you have committed to local master by mistake - just cherry-pick to a branch, then reset master. Only ever do this if you haven't pushed the commits to origin.

Can a commit be taken from one branch and move to a different branch in git?

You can move commits from one branch to another branch if you want changes to be reflected on a different branch than the one to which you pushed the changes.


Getting the branch is easy:

git branch their-branch master
git reset --hard master $SHA1_OF_C
git push --force $SHARED_REPO_REMOTE

This will rewrite history; it creates a new branch that is equivalent to the current master branch named their-branch, then resets your local master to point to a random SHA1 (...or branch, or tag, or whatever), and finally forcibly updates the remote repository branch to match your local branch.

That delivers exactly the situation you want.

Caveats: this will rewrite history, and can screw up anyone who has built off the old master. Watch out for merge problems following.

No rebasing desired or required.

(As an aside, I suggest you either give them a staging repository, or get them to use git send-email, or use GitHub pull requests, or otherwise prevent them pushing to master until they get it right. Which, based on your summary, might be some time away.)