How to overwrite a branch in Git with master

Solution 1:

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

Solution 2:

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.