0

I found some questions about how to revert with Git, but those questions always needs human interaction or to need to which revision are you going to revert. In this case there is no human interaction and you are not telling the machine to which revision must revert.

Given a remote and automated machine which haves a cloned git repo which does pull every time you press a "pull" button on it's interface, is it possible to implement the functionality of reverting to the previous commit?

I can easily add the button, but I'm not sure about how to do the functionality. I mean, i need to know if git has a command which gives you the previous commit of it's branch and then, a command which changes the local repo status to that previous commit.

This machine doesn't make any commits, it only has the clone code and it only does some pull for code updating. Now with this new button it will be capable of reverting to the previous revision.

Is this possible?

halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • 2
    Possible duplicate of [How to undo a git pull?](http://stackoverflow.com/questions/5815448/how-to-undo-a-git-pull) – Josh Lee Jan 24 '17 at 18:17

2 Answers2

0

The following will revert the master branch to what it was before the last command that changed the master branch, no matter what it was, commit, merge, rebase, filter-branch, ...

git reset --hard master@{1}
Vampire
  • 35,631
  • 4
  • 76
  • 102
  • can't find any info about this command with it's parameters you told me on google – NullPointerException Jan 25 '17 at 08:21
  • I don't know why someone downvoted it. The command does exactly what I described. Maybe the downvoter read "Revert" in your question and I didn't give you a command to revert but to reset, but a reset is actually what you meant. – Vampire Jan 25 '17 at 09:41
  • You can read about `reset` at https://git-scm.com/docs/git-reset and about the format of the last parameter at https://git-scm.com/docs/git-rev-parse. `reset --hard` does move the current branch pointer to the given commit-ish and also updates the worktree with this state. `master@{1}` is the next to last entry in the reflog of branch `master`, so you would have to change this to the branch you are talking about or requesting it dynamically if for different branches. Each action you do that changes a branch pointer like the ones I listed exemplary leaves an entry in the reflog. – Vampire Jan 25 '17 at 09:44
  • thank you, but your command doesn't works, it gives this error: fatal: Log for 'master' only has 1 entries. Finally i disscovered the solution and posted it in a new answer, but i given you a upvote for your help – NullPointerException Jan 25 '17 at 16:01
  • If you have the SHA, your solution works of course. Mine only works if the reflog for the branch is enabled. But usually this should be the case. You can check the config for reflogs, following https://git-scm.com/docs/git-config. – Vampire Jan 25 '17 at 16:07
  • After a little research I'm not sure about exactly what is reflog and how to use it safely – NullPointerException Jan 26 '17 at 07:10
  • As i already explained, the reflog saves for some period of time, each step a ref did. Each command that changes a ref (like commit, merge, rebase, filter-branch, ...) will leave an entry in the reflog. – Vampire Jan 26 '17 at 07:48
0

Finally i solved it using this:

git reset --hard $SHA1

$SHA1 is the id of the revision you want to get.

Later, when you want to be updated again with all the commits of the repo, you just need to do:

git pull
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • Since you seem to think `HEAD@{1}` doesn't meet your needs, but specifying some `$SHA1` does, would you care to explain how you know the id of the previous commit and how that's different from using the reflog? – Josh Lee Jan 25 '17 at 19:04
  • i will pass it with a field, because HEAD@{1} gives an error: "fatal: Log for 'master' only has 1 entries." – NullPointerException Jan 26 '17 at 07:09
  • After a little research I'm not sure about exactly what is reflog and how to use it safely – NullPointerException Jan 26 '17 at 07:10