Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How replace a branch in Git with another branch without merge

Tags:

git

merge

My repo structure is like this.

  • branch 'dev' - Created from 'master' and is current.
  • branch 'master' - No commits for last 2 years. But few commits (unwanted) ahead of 'dev'.

Now my need is to make the 'master' code exactly same to 'dev'. I guess, a merge would cause the unwanted commits on 'master' to be retained. Any help?

like image 941
Toxic Brain Avatar asked Jan 08 '19 07:01

Toxic Brain


1 Answers

You may do a hard reset on master to the current dev branch:

# from master
git checkout master
git reset --hard dev

But keep in mind that this may potentially discard any commits on master which were unique to that branch. If this be a concern of yours, then consider branching off from master as a safety precaution.

The next time you push master to the remote you will probably have to force push:

git push --force origin master

The reason for force pushing is that you have rewritten the base of the master branch and Git won't accept a regular push.

like image 62
Tim Biegeleisen Avatar answered Sep 19 '22 08:09

Tim Biegeleisen