3

I have a branch called as masterdev.

I made some commit to that branch, so it is maybe 10 commits ahead from the masterdev that is on the server. But in fact those commits are not good anymore, so I don't want to push them.

I could revert or rebase may be to get the opposite of what I have done in those commit. But that looks like not clear at all I think.

So what I wanted to do is getting the same behavior than git pull origin masterdev if masterdev was behind.

I want to wipe out my local masterdev to get the same as on the server.

Whymarrh
  • 13,139
  • 14
  • 57
  • 108
  • 1
    You can simply do git reset. – ShellZero Jul 20 '17 at 21:49
  • Possible duplicate of [Reset local repository branch to be just like remote repository HEAD](https://stackoverflow.com/questions/1628088/reset-local-repository-branch-to-be-just-like-remote-repository-head) – ob1 Jul 20 '17 at 22:04

4 Answers4

4

I think what you want is - this will reset your local version of masterdev, removing your commits. Then you pull to make sure you're up to date.

git reset --hard origin/masterdev

git pull origin masterdev
  • 1
    This answer is also correct, and the O/P should feel reasonably comfortable with it. – torek Jul 20 '17 at 21:56
  • I just made a test, and that was exactly what i was looking for. Thank you for your help. But it seems the git pull is useless after that. If there is nothing more to pull of course. –  Jul 22 '17 at 22:30
  • "If there is nothing more to pull of course" yes of course. That line isn't strictly necessary to solve your problem but it's probably a good habit to get into since it helps prevent merge conflicts. – katniss.everbean Jul 25 '17 at 16:40
3

If you have nothing worth saving in the index and work-tree, a simple git reset --hard suffices:

$ git status
On branch masterdev
Your branch is ahead of 'origin/masterdev' by 10 commits.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean
$ git fetch origin
... [snip]
$ git reset --hard origin/masterdev

If you do have some work to save, you probably should commit it and do something more complex than just a simple git reset --hard.

(The intermediate git fetch step can be run before or after the git status, though the number of commits ahead and/or behind may change as a result. I recommend avoiding git pull in general, and you won't need it for this case.)

torek
  • 448,244
  • 59
  • 642
  • 775
1

git reset --hard origin/masterdev should do the trick. Then you can simply do git pull origin masterdev

ShellZero
  • 4,415
  • 12
  • 38
  • 56
  • 1
    `HEAD^` assumes there is just one commit to discard. (And `HEAD` alone discards no commits - you'd want `HEAD~10` if that were the right number of commits, but it's simpler to just run `git fetch` to get `origin/masterdev` updated, and then use that directly.) – torek Jul 20 '17 at 21:52
  • Right. My bad. Thanks. Updated it. :) – ShellZero Jul 20 '17 at 21:54
0

Easy, just git checkout --track origin/master. Will create the branch for you locally and be exactly as the remote version

RuNpiXelruN
  • 1,850
  • 2
  • 17
  • 23