1

I created a branch off the master to do some major changes to the project and now I want to make this branch the master, i.e overwrite what's in the master (which has had some changes since) with what's in my branch. What's the best way of doing this in git?

Update: I should have explained that I'm the only user right now. I'm using GitHub for my repository.

parsley72
  • 8,449
  • 8
  • 65
  • 98
  • It sounds like you want to change the current branch to master. Check http://stackoverflow.com/questions/2763006/change-the-current-branch-to-master-in-git or http://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch – Michael Ver Mar 16 '14 at 04:57

2 Answers2

1

While being on the tip of your new branch:

git branch -f master
git checkout master

Branches are just moving labels in Git. Just create the master branch on the checked out commit. Normally git would complain, as master already exists. Hence the force -f option.

If you want to preserve the old master for whatever unforeseen reason, just put another branch on it before:

git branch old_master master
git branch -f master
git checkout master
SzG
  • 12,333
  • 4
  • 28
  • 41
1

You can reset any branch to any commit using git reset. In your case you probably want to do:

git checkout master
git tag old-master
git reset --hard otherbranch
gitk --all

The actual work is don by git reset the tag is optional for reference in case you later need some change from your old master branch. gitk is a nice way to check if everything looks the way you planned it to be.

Be aware that this works only locally. If you need to update a remote repository you probably need a forced push and might upset any other users of your repository.

michas
  • 25,361
  • 15
  • 76
  • 121