5

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?

Hugo y
  • 1,421
  • 10
  • 20
  • See also https://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch and https://stackoverflow.com/questions/2763006/make-the-current-git-branch-a-master-branch (I won't close this as a duplicate since you specifically said "without merge" but those two contain what's probably a better way, i.e., *with* merge, but a merge that says "the other branch tip was right after all".) – torek Jan 08 '19 at 09:56

2 Answers2

6

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

Tim's answer works great, let's just add this variant for the record :

# from any branch
git branch -f master dev

And the warning about losing the contents of the old dropped branch is the same, a backup is cheap, so maybe consider this :

git branch backup_old_master master
git branch -f master dev

Then master will point to the exact same commit (and history) dev currently points to, and you'll have backup_old_master just in case.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • 1
    I like this answer better, because the working copy does not change twice. First the switches might take some time (in big repos) and - most important to me - automatic builders and IDEs don't start to do unnecessary work. – A.H. Jan 08 '19 at 18:36