1

I know git is for version control, so it will keep history commit always. But for some usecase, I wish to keep history in my local git repository, but only push snapshot to remote git server, such as github.

In order to save space in github, I wish the git in github only save the lastest snapshot but no any history commits.I can use "git commit --amend" to use one commit only everytime, but I am not sure if it really just like a snapshot without any other overhead from space perspective.

In below diagram, I wish to make both client and server back to 1M but not 11M (red notes box). Of course, I don't care about any history before!

enter image description here

lucky1928
  • 8,708
  • 10
  • 43
  • 92
  • This is an unusual request; why do you want to do this? Normally you'd want the full history of your repo on GitHub so that collaborators could have access to it. – Tim Biegeleisen Jul 26 '17 at 15:24
  • Right, it's unusual request, I want to do that since server space is expensive,I can keep history in local repository but use as less space as possible in server space. but the server is designed to use git, – lucky1928 Jul 26 '17 at 15:37
  • 2
    Amending your commit will not release the storage used for the previous version immediately, but it will be garbage-collected at some point. Is youe repo really so big that this is a significant cost? Each commit should only be a diff, and typically compresses well. – Useless Jul 26 '17 at 16:44
  • `git checkout --orphan` comes to mind... Re-create your master branch with no parent before each commit. You still have the situation where the old stuff won't be garbage collected until later, though. – twalberg Jul 26 '17 at 17:55

2 Answers2

1

At this post, using git archive (no history) or git bundle (full history), to create only one file, and storing that file elsewhere is the easiest solution.

No need to push: you only have one file (archive or bundle) to store.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0
git update-ref refs/heads/latest $(
        git commit-tree -m 'your message here' $(git write-tree)
)
git push -f origin latest

will push a commit of your current checkout+any adds and set the latest branch here and in the remote to it. You could even

git push -f origin $(git commit-tree -m 'your message here' @:):refs/heads/latest

if you want to just slice off your currently-checked-out aka HEAD aka @ commit's content as the remote's latest commit.

By the way, you don't have to use remote names or urls for repos, just plain pathnames will work too. Even . works. The shortest way to locally slice the tip off the master branch is a bit unnerving, but it's lovely:

git push -f . $(git commit-tree -m 'your message here' master:):refs/heads/latest

and you could then git push -f origin latest after examining it for correctness.

To see what refs the remote has,

git ls-remote origin   # as everywhere, a path or url will work too
jthill
  • 55,082
  • 5
  • 77
  • 137
  • Sounds like still not as expected, Just update my question with a diagram. – lucky1928 Jul 27 '17 at 15:29
  • See [@Useless's comment](https://stackoverflow.com/questions/45331124/git-snapshot-only-push-to-save-space/45342463?noredirect=1#comment77627755_45331124). Git's not built to treat keeping 10MB around for a while in case you decide you really didn't want to delete it as any kind of problem. Googling "reduce git repository size" will get you lots of [good](https://stackoverflow.com/questions/2116778/reduce-git-repository-size) [answers](https://stackoverflow.com/questions/3797907/how-to-remove-unused-objects-from-a-git-repository/14729486#14729486). – jthill Jul 27 '17 at 16:27