45

I want to overrite master with a particular branch after making changes to it, what I done to do it is:

Step 1: Checkout brranch from Git, using command :

git checkout branch_name

Step 2: I done some changes in code, now I want to make this branch as master, for that I first run the command:

git status

Above command list me all the modified files.

Now my question, what all I need to do overrite master with this particular branch "my_branch"?

Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • 2
    http://stackoverflow.com/questions/29870328/overwrite-everything-in-master-with-another-branch-in-git/ ? – Tobia Tesan May 07 '15 at 15:17
  • http://stackoverflow.com/questions/29870328/overwrite-everything-in-master-with-another-branch-in-git/ solved half a problem, but when I performing git pull from another system, I am still getting the previous version. – Arpit Aggarwal May 07 '15 at 15:30
  • 1
    @Arpit, well, if you don't push it to the remote system that's the way it will stay. Be aware that this requires a force push, which your teammates may or may not appreciate. – Tobia Tesan May 07 '15 at 15:39
  • Possible duplicate of [overwrite everything in master with another branch in git](https://stackoverflow.com/questions/29870328/overwrite-everything-in-master-with-another-branch-in-git) – Tomas Kubes Jun 03 '17 at 07:58

2 Answers2

109

git branch -f master dev_branch will rewrite local master branch.

git push remote +dev_branch:master will rewrite remote branch.

  • NOTE: If the above doesn't work, the name remote could be origin for you (i.e. git push origin +dev_branch:master)
Robbie Capps
  • 417
  • 1
  • 5
  • 16
aragaer
  • 17,238
  • 6
  • 47
  • 49
49

To completely replace the master branch with the contents of any other feature_branch you can also use:

git checkout feature_branch
git merge -s ours --no-commit master
git commit      # Add a message regarding the replacement that you just did
git checkout master
git merge feature_branch

See: http://git.tutorialhorizon.com/2014/10/05/replace-the-master-branch-with-another-branch-in-git/

ice_chrysler
  • 2,633
  • 1
  • 21
  • 27