14

Having the full Git revision history has a lot of benefits as part of the development process.

But our product is the source code, we are using scripted languages that don't need compilation or processing, and then the Git history becomes a burden on deployment- in our example we deploy a clean virtual environment following every change, having several deployments on a single machine.

There are some ways to reduce the amount of history, for example shallow clones whose efficiency depends on how deep the revision is in the branch, doing a fetch instead of clone but then you still get history from the revision and back, or getting the full repo once then pull when needed but this is wasteful in terms of disk space and tends to be less reliable.

Is there a way to get a single revision from Git without it's history ?

1

3 Answers 3

16

Shallow clone

You can indeed get a shallow clone out of Git using:

git clone --depth=1 <url>

This will still clone the repo and create a .git folder with the objects, only smaller in size (difference depending on your total file size vs. history size).

Git archive

You can also use git-archive to extract an archive of the repo:

Creates an archive of the specified format containing the tree structure for the named tree, and writes it out to the standard output. If is specified it is prepended to the filenames in the archive.

In the examples it shows for instance:

git archive --format=tar --prefix=git-1.4.0/ v1.4.0 | gzip >git-1.4.0.tar.gz

Create a compressed tarball for release.

Hosted Git, archive API

If you are hosting your repo on GitHub, then you can use their archive API:

https://api.github.com/repos/<username>/<repository>/zipball/<commit_hash>

Bitbucket.org has a same functionality for this:

https://bitbucket.org/<username>/<repository>/get/<branch_name|commit_hash|tag>.zip

1
  • 7ochem, maybe gzip command need to be gzip -c .... Commented Mar 7, 2017 at 18:16
11

Don't deploy your git repo. Develop a real deployment methodology. Even if it's as simple as tarring up an archive (= building an artifact with just the necessary files in it to be deployed) of deployed scripts.

Even if you shallow-clone the tip of your source repository, you likely don't need unit tests, documentation, linting profiles, and other supporting ephemera in your deployed environment.

Note: for repositories of scripted languages that don't really have a "build" step, a trivial way to release an artifact would be to package them in an archive, such as tar or rpm. Then, to "deploy", you untar the archive or install the rpm. this removes the need for git tooling in your deploy chain (not all prod servers will have those dev tools).

10
  • it's okay to question (or even challenge!) an answer -- that's what makes Stack Exchange great :) for repositories of scripted languages that don't really have a "build" step, a trivial way to release an artifact would be to package them in an archive, such as tar or rpm. then, to "deploy", you untar the archive or install the rpm. this removes the need for git tooling in your deploy chain (not all prod servers will have those dev tools) Commented Mar 7, 2017 at 19:30
  • 1
    @Pierre.Vriens yes, you're missing what's is suggested as tarring an archive, I.e building an artifact with just the necessary files in it to be deployed. That said I agree that's not a quality answer and this point should be extended. We are in private beta and answers should née explicit
    – Tensibai
    Commented Mar 7, 2017 at 19:32
  • Then I don't see what this brings more than the accepted answer, if it's to point toward fit archive... it's just redundant, you should edit to extend on this way iMHo
    – Tensibai
    Commented Mar 7, 2017 at 19:34
  • 1
    Please check my edit of your answer (just integrated your interesting comment). Feel free to improve/rework of course, or just rollback if you don't like my edit at all. BTW: your comment (= note I added) made me think like "really, that simple? Again a sample of how we Get Things Done in zOS ... with z for zero downtime ...". I think it's time to start to question way more questions/answers via similar comments ... Don't challenge me too much ...
    – Pierre.Vriens
    Commented Mar 7, 2017 at 19:38
  • i don't purport that this is a complete or model answer, but i didn't see anyone else addressing the elephant in the room: if you use git to "deploy" your project, you're "gonna have a bad time" ;) Commented Mar 7, 2017 at 19:40
6

Question is there a way to get a single revision from Git without it's history ?

To get a repository, no there's no way, mainly because there's no 'revision'. Git store commits, which are changes from previous state.
If you want your repository at a specific point in time you have to pull the commit at this time and all it's ancestors or you'll get only the changes made in the commit.

To avoid confusion: Shallow cloning is getting the needed history and then truncating it to free space, the tree is still made from history.

For the solutions, @7ochem answer does cover them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.