67

I have an old dev branch that is completely outdated. I don't want to remove it but I do want to replace its contents entirely with the master branch. Meaning, I want dev_branch = master

I tried:

git merge -s ours dev_branch

Didn't work though...

Giacomo1968
  • 55,001
Cornwell
  • 906

2 Answers2

97

If you want all changes from master in dev_branch, then:

git checkout dev_branch
git reset --hard master

This only works if other people haven't cloned the repository.

If you have dev_branch pushed to a remote already, you have to do:

git push --force

To force-push to the remote. Warning: This will break the history of the branch for people who cloned it before! Then, other people will have to do a git pull --rebase on the dev_branch to get the changes.


You can also rename the dev branch to something old and then make a new branch from master with the same name:

git branch -m dev_branch old_dev_branch
git branch -m master dev_branch

Or, use the ours strategy — not sure why it wouldn't work for you:

git checkout master
git merge -s ours dev_branch
git checkout dev_branch
git merge master
slhck
  • 228,104
  • 1
    With the renaming solution, I think you will find that you now can't push dev_branch to origin (if it had one), and you will also no longer have a master branch locally. – aidan Oct 28 '15 at 01:48
  • 1
    Thanks @slhck! For some reason, my stage branch was not being updated after doing a normal merge. It kept on saying that my stage branch was up-to-date. Using the "ours" strategy solved this for me. – Slick Shinobi Jun 22 '16 at 08:48
  • ours would only affect conflicted chunks, but he wants to completely renew the branch, not only the conflicts !? (this is at least what I need to do and why I found this Q) - I will go for the rename! – Martin Meeser Sep 27 '19 at 09:44
1

Why do that? You can delete the branch if it isn't needed anymore (But why? Branches cost next to nothing.). Or you can rename it:

git branch -m dev_branch obsolete_dev

Or you could do this to delete it:

git branch -D dev_branch

Now create a new branch off master (assuming you are on it):

git branch dev_branch

See git branch --help for further options (setting up remotes and all that jazz).

If you now have new branches, you'll have to synchronize with any peer repositories.

Best way to avoid hassle: Have an "active" development branch, if it goes stale, abandon it and create a new one. No history lost that way (could prove crucial sometime).

Have e.g. a branch for each major version, develop on branches off those to fix version bugs, master forges ahead. Use cherry-pick and perhaps merges to port fixes to older versions.

Giacomo1968
  • 55,001
vonbrand
  • 2,459
  • 3
  • 22
  • 21
  • Q: > Why do that? You can delete the branch if it isn't needed anymore

    A: A possible reason for why one might not want to delete the branch is that CI tooling might deploy the dev server or a testing server based on that branch, and removing that branch might remove the server, or break it.

    – apotek Feb 27 '24 at 16:29