34

I have two branches. Staging and Beta. Staging has code in it ( including files ), that I do not want at all. How can I make Beta completely overwrite Staging, so that none of those files or code are merged from Staging into Beta.

I see some people recommend doing this :

git checkout staging
git merge -s ours beta

But I don't believe the pre-existing files would be a "code conflict" and therefore would not be removed. Am I wrong? If I'm right, how would I accomplish this?

Trip
  • 26,756
  • 46
  • 158
  • 277

4 Answers4

31

You can simple delete staging and re-create it based on beta:

git branch -D staging
git checkout beta
git branch staging
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
25

If you don't care about the old history of staging, you can just recreate it:

git checkout beta
git branch -f staging

If you do care about the old history of staging, then things get more fun:

git checkout staging        # First, merge beta into staging so we have
git merge -s theirs beta    # a merge commit to work with.

git checkout beta           # Then, flip back to beta's version of the files

git reset --soft staging    # Then we go back to the merge commit SHA, but keep 
                            # the actual files and index as they were in beta

git commit --amend          # Finally, update the merge commit to match the
                            # files and index as they were in beta.
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 1
    This would delete unwanted files that were recently created on Staging? – Trip Apr 23 '13 at 14:51
  • @Trip Since it resets the index back to exactly what was in `beta`, anything that wasn't in beta should get deleted as far as Git is concerned. – Amber Apr 23 '13 at 14:55
  • 2
    Unfortunately `git merge -s theirs` isn't available in newer versions of Git, so this won't work exactly as written. – James Nov 19 '15 at 16:58
  • 2
    If you have a newer version of git use 'git merge -X theirs' instead of 'git merge -s theirs'. – digitalHound Aug 02 '16 at 17:35
9

I suggest you just rename it in case you change your mind.

git branch -m staging staging_oops
git checkout beta
git branch staging

If you really can't stand having that extra branch around:

git branch -D staging_oops
Shawn Balestracci
  • 7,380
  • 1
  • 34
  • 52
0

If the history of staging is not going to be an issue, you can simply do this.

git checkout staging 
git reset --hard beta

Just remember the history of staging will be gone after the above command and staging will have the work of your beta branch.

yardstick17
  • 4,322
  • 1
  • 26
  • 33