2

I am deploying application to a production server by running git pull on that production server.

As part of a backup, I am thinking of an idea to just store the current commit hash instead of copying all the files. Later on, during the restore, I would like to restore the local version of the repository from a remote origin to the saved commit hash. Ideally so it behaves like it would if I have restored whole tree including the .git folder.

To save the current commit hash, I am thinking of git log -1 --format=%H

In order to restore to a specific revision, I am aware of the git checkout hash command. Unfortunately, this command detaches the working copy of the repository from a branch.

Is there a command or set of commands to restore the local git repository to the state it was before (aka in a transparent way with respect to further updates)? So the git pull will update the working copy to the most recent commit in the respective branch and update the HEAD pointer etc.

alik
  • 2,244
  • 3
  • 31
  • 44

2 Answers2

1

You can checkout any commit, but that won't restore the .git directory to it's former state, and "attached to the same branch" makes no sense at all. In git, a branch is just a reference to a commit, which starts a sequence of commits it comes from. The commit you check out has its own history, but checking it out puts you in the special "detached head" state (you aren't at the tip of any branch, so it makes no sense to e.g. add new commits on top).

vonbrand
  • 11,412
  • 8
  • 32
  • 52
  • My vision is that the routines to update the site on the production server should not care about whether there has been a restore in the past or not. So from the view of further updates, it is quite ok to have the local repository restored to a state similar to how it would have been between `git fetch` and `git merge` if there has been no restore. – alik Mar 14 '14 at 10:52
  • @alik Not only "attached to the same branch" doesn't make sense as @vonbrand pointed out, but "detaches repo working copy from a branch" isn't correct either, the branch remains pointed to the respective commit. Further, you intend to restore from a remote repo, therefore there is no state "between" `git fetch` and anything else, the state is the state exactly after the fetch, and is described by the set of your remote tracking branches. – mockinterface Mar 14 '14 at 11:14
  • I apologize for not being correct as I am not really keen on git. I am trying to explain my goal as clearly as my understanding allows. "Between" referred to `git pull` which does the same as `git fetch` followed by `git merge` with right arguments. – alik Mar 14 '14 at 11:23
  • @alik, if you aren't interested in the repository as such (i.e., history, being able to commit, ...), all you require is package up the files in the workspace, and perhaps include a tag (or a hash of the specific commit) to be able to restore it. If needed, given, say, release `r10` followed by `r11`, you can go `git diff r10 r11` and get a patch to update the workspace without repository you have in production. – vonbrand Mar 14 '14 at 13:30
0

I am aware of the git checkout hash command. Unfortunately, this command detaches the working copy of the repository from a branch

You can take the branch and point it back to the commit, admitting the “branch attachment” that you ask about in the question subject:

$ git update-ref refs/heads/<branch> <hash>
mockinterface
  • 14,452
  • 5
  • 28
  • 49
  • How will the update-ref affects the origin copy and/or pulling further updates from origin? Please check my comment after vonbrad suggestion what I am willing to achieve. – alik Mar 14 '14 at 11:00
  • @alik I've quoted the part I intended to address in my reply, it was my intention to give you an additional tool to work with, strictly that and nothing else. Now you can restore a specific revision in your backup repo, and dub it a branch. That aside, there should be no effect on the remote branches, as long of course, as is a local (tracking) branch. – mockinterface Mar 14 '14 at 11:09
  • Thank you. I need to check the update-ref command more deeply. First run through the manual was not enough. – alik Mar 17 '14 at 21:11