99

My friend's local master branch is apparently a disaster (through accidental merges and commits, I guess). However, his dev branches are fine but contain changes he's not ready to push to remote.

What's the best way of overriding his local master branch with the remote master branch and get a fresh copy (without overriding his other branches)?

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Brian
  • 2,499
  • 3
  • 24
  • 26

2 Answers2

160

As Jefromi commented,

git checkout master
git reset --hard origin/master

does the right thing: setting the master to its origin state. (If you are already on the master branch, you can omit the first command.) It also leaves the branch's reflog intact.


Old inferior answer:

git checkout dev
git branch -D master
git checkout master

This switches to another branch ("dev" in this case – choose any other branch you might have), deletes the local master branch, and then recreates it from remotes/origin/master (which might not work depending on your settings and Git version). The last command is often equivalent to

git checkout -b master remotes/origin/master

Compared to the new answer above this has the disadvantage that the reflog is destroyed and recreated (i.e. you can't as easy undo this if needed), and it is less clear what happens here. Also, you need to have another branch existing to which you can switch during deletion and recreation (but that was the case in the original question).

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • 7
    Why not just `git reset --hard origin/master`? Also... does that last step even work? Doesn't seem to for me. (`git checkout -b master origin/master` would though.) – Cascabel Apr 14 '11 at 03:27
  • @Jefromi: You are right, I didn't see the obvious solution. The last seems to work for me (checkout has some default if the branch does not exist, but a fitting remote one does). This may depend on the version, though. – Paŭlo Ebermann Apr 14 '11 at 11:24
  • 1
    I tried the first solution and it didn't work. Would be nice to edit it out. – lulalala Mar 10 '14 at 05:24
  • @lulalala With "first" solution you mean the one mentioned as "old" one? For it to work you need to have a local branch named "dev", of course. – Paŭlo Ebermann Jun 16 '16 at 18:27
  • yeah the `checkout dev` one – lulalala Jun 17 '16 at 03:20
  • I don't see what's wrong with actually deleting the master and re-creating it from origin. Should be totally fresh and gets rid of the untracked files as well. You may prefer to refresh your reflog too. – NeilG Mar 22 '20 at 22:43
  • will `git reset --hard origin/master` pull changes from `origin/master` and make your `master` branch an exact replica of `origin/master`? – Sudhir Singh Khanger Apr 25 '22 at 11:58
  • @SudhirSinghKhanger if you are currently on master, yes. (It won't fetch changes from the remote repository, you'll need to do this first with `git fetch origin` if wanted.) – Paŭlo Ebermann Apr 25 '22 at 23:21
13

Paŭlo Ebermann's answer is correct:

git checkout master
git reset --hard origin/master

And add that if you also wish to remove untracked files and ignored files:

git clean -xfn # dry run with -n

Source with further details: How to remove local (untracked) files from the current Git working tree?

Community
  • 1
  • 1
Travis D
  • 356
  • 2
  • 7