简体   繁体   中英

How to initialize local git repository and tell it it is already in sync with server?

Hear me out!

I have a local folder with all the code that is the latest version. This same version of code is present in a master branch in a remote git repository. Now I want to initialize git locally, but I do not want to download all the code through git pull again. Even if I add a remote repository after git init and try to fetch, it seems to download everything before deciding that it is in sync.

There are also a lot of binary files in the code.

Main reason to do this is to save bandwidth. But also just to know how to do it.

How do I tell git - "this is already in sync with latest in the server (or even better - this particular revision on the server - take my word for it - and take only later revisions?)"

Edit:

To be really honest, the server has only one commit right now - now what I want is something like for eg

locally also do that commit and tell git that the server also has the same commit (because the files in the folder present when the first commit were done were exactly same) - obviously git doesn't know that yet, because different commit ids are generated locally from what was there on the server. If there was anyway I could change the commit id after checking in locally to what it is on the server, it would solve my problem.

I am looking for anything like or similar to above if possible.

I really don't want to do "git clone" (and download all files through git now, again), even a shallow clone.

Edit2:

Note that even after just one commit, my .git folder is 182 MB. This is nearly the compressed size of the entire repo - and my fear is that this entire thing is getting downloaded again (even though the same 182 MB .git file already exists locally, with probably only the commit id being different).

You cannot expect git to work based on your working-copy alone! It needs a lot of metadata in order to function properly, including the project history.

What you want is a shallow clone. This can be had with

git clone --depth=1 git://some/repo

However, there are a lot of important limitations to shallow clones.

  • You can't push from them
  • You can't pull from them
  • You can't reference project history without downloading more

I think you're misunderstanding what git is and how it works. The entire point of a distributed version control system is that you have the entire history of the repository locally.

That said, if you really want to do this, you can make a "shallow clone":

git clone --depth <depth> <repository>

Where <depth> is the number of revisions of history you want. Be warned that this clone will be pretty broken when trying to interact with other repositories, so you might not get the behaviour you want.

From the git clone man page , bold emphasis mine:

--depth <depth> Create a shallow clone with a history truncated to the specified number of revisions. A shallow repository has a number of limitations ( you cannot clone or fetch from it, nor push from nor into it ), but is adequate if you are only interested in the recent history of a large project with a long history, and would want to send in fixes as patches.

The last phrase there indicates your only remaining workflow - to generate patches and then apply them to a complete repository elsewhere if you want to push.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM