1

I have a git repository that I push to from development and pull from test and production.

Say I made 17 commits so far, and all the servers are in synch.
Now I want to revert to commit #15 on production, but not only a (few) file(s): I want all the files on my production server to reflect commit #15.

I also prefer not to create a new commit for this, and also to be able to roll forward to the latest commit, or other ones from the central repository.

My first guess is to run it without declaring any files, e.g.,

git checkout [commit-15-SHA1]

Is there a better/safer to do it? What is the git way?

Community
  • 1
  • 1
Marius Butuc
  • 17,781
  • 22
  • 77
  • 111
  • 2
    Your suggested command is *exactly* the way to do this. Are you encountering any problems doing that? – Greg Hewgill Oct 31 '12 at 00:12
  • I got a "_You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. [...]_" message. I guess this happened because I didn't stash beforehand? – Marius Butuc Oct 31 '12 at 00:16
  • 1
    No, the "detached HEAD" state happens because your current HEAD points to a commit that doesn't have any other name. So if you were to make a new commit based on your current location, you wouldn't have any way to refer to that new commit other than writing down the SHA1. If you're not intending to make any new commits based on your current HEAD, there's nothing to worry about. – Greg Hewgill Oct 31 '12 at 00:19
  • Also, it might be a good idea to [tag](http://git-scm.com/book/en/Git-Basics-Tagging) this commit so that you can refer to it more easily. – hammar Oct 31 '12 at 00:38

1 Answers1

4

If you want to revert, and then branch from commit #15, keeping all the revisions AFTER commit #15 intact, yours is a good approach.

git checkout [Commit-15-SHA]

If you just want to "back up" in commit history, without losing your current changes, and two most recent commits, then try a soft reset.

git reset --soft [Commit-15-SHA]

However, if you want to nuke everything you changed, including all revisions AFTER commit 15, then you do a hard reset.

git reset --hard [Commit-15-SHA]

Read more about git reset here.

4Z4T4R
  • 2,340
  • 2
  • 26
  • 45