How to overwrite a branch in Git with master

git

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…

Best Answer

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