2

I created a git repo and I want the entire git repo with all commits as a single patch file.

I've tried many SO posts, but couldn't find any suitable solution anywhere.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Palak Arora
  • 1,013
  • 1
  • 10
  • 16
  • What exactly do you need to do with the result? What kind of system is it for? Be specific. Just cloning the repo is going to be by far the most compact representation. – jthill Aug 25 '14 at 07:14
  • So "patch" was actually the full repo history in your case? – VonC Aug 31 '14 at 06:23
  • @VonC: There wasn't any "patch" as such. I wanted my entire repo as a text file. – Palak Arora Aug 31 '14 at 06:24
  • @PalakArora excellent. did you choose bundle (to get all the commits), or just archive (to get the HEAD content in a zip file)? – VonC Aug 31 '14 at 06:25
  • @VonC: I tried out another method as I've posted it over here. I've upvoted your answer. :) Thanks. – Palak Arora Aug 31 '14 at 06:27
  • But your method is about a patch, not the full content of your repo?! – VonC Aug 31 '14 at 06:27
  • @VonC: You may rename the file as a `.txt` file also. I just wanted it in text format. It outputs the entire repo. – Palak Arora Aug 31 '14 at 06:28

3 Answers3

7

In order to create a single patch encapsulating the whole repository, you need to diff the current HEAD against the empty tree. Such a tree is available in git under the SHA1 4b825dc642cb6eb9a060e54bf8d69288fbee4904 (This is the SHA1 that you would get if you created the empty tree by emptying the index and issuing git write-tree. Every git repository has one.)

Therefore the command you are looking for is:

git diff --binary 4b825dc642cb6eb9a060e54bf8d69288fbee4904..HEAD

The --binary option makes sure that binary content will be included in the diff, and that the file names will not be abbreviated.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • Seriously? downvoted *again*? Could we at least wait for the OP to clarify the exact use case? I will delete my answer if Palak confirms that diff was the right answer. – VonC Aug 25 '14 at 08:43
  • @VonC Why insist on providing an answer that doesn't answer the question? While the OP's wish to obtain a "single patch file" is admittedly strange, I don't see how either of your interpretations fits the term. You have been on SO far longer than me, and should know better than to scoff at an occasional downvote. (I won't even comment the practice of deleting an answer and then resubmitting the exact same one.) – user4815162342 Aug 25 '14 at 12:01
2

While user4815162342's answer is technically the right solution (upvoted), Palak Arora's question about "an entire git repo with all commits as a single patch file" could be interpreted differently.

Here are two other possibilities, pending Palak's feedback:


If by patch "with all commits" you mean the full history of a git repo as one file, you could consider creating a bundle.
It is a single file which acts actually as a Git repo: you can fetch or pull or clone from that file;

cd /path/to/your/repo
git bundle create /tmp/foo-all --all

If your "patch" is actually the full content of the current repo, you can also use git archive

cd /path/to/your/repo
git archive -o /tmp/foo.tar --format=tar .

In both cases, they differ from the classic patch in that you wouldn't use a bundle or an archive to "apply" that "patch" on an existing code base, but rather to rebuild quickly said code base from scratch.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

Since, I tried out another method on my own, I thought of posting it here. It was exactly what I needed.

git log --pretty=format:"%H - %an, %ad : %s" --author "<your name>" --after="2014-05-19 00:00:00" --before="2014-08-18 00:00:00" -p --no-merges > ~/<path to some folder>/myrepo.patch
Palak Arora
  • 1,013
  • 1
  • 10
  • 16