15

I am trying to create a patch file to be used via reviewboard.
It will be the initial patch.

diff -ruN --exclude=.git empty_dir working_dir > mypatch_00.patch works but I am getting a "The selected file does not appear to be a diff." error.

Is there any way to get it working using git-diff or diff? Thank you

rxdazn
  • 1,380
  • 1
  • 14
  • 30
  • 1
    What's the content of the diff? I think the command you're running will just result in a list of file names and not actual diffs. This is also what you get with `git log -p` which would otherwise work for you. – asm Jan 28 '13 at 14:41
  • @AndrewMyers I'm getting this kind of entries https://gist.github.com/4e98d97a21990c657ac3 – rxdazn Jan 28 '13 at 15:03

3 Answers3

23

If you want to get diff for your initial commit (maybe I misunderstood your question), you can display it using

git show COMMIT

Alternatively to get it as file:

git format-patch -1 COMMIT

Where COMMIT is revision of this commit. If this is your current commit as well, you don't have to specify it at all.

However if your commit is not initial and you want to get full diff for your history, you need to create empty branch first:

git checkout --orphan empty         # Create orphaned branch
git read-tree --empty               # Remove all files from index
git commit --allow-empty -m 'Empty' # Initial commit

Now you can do full diff against empty branch:

git diff empty..master
Michal Čihař
  • 9,799
  • 6
  • 49
  • 87
  • I know about `git show` and `git format-patch`. I want to create a diff against nothing, meaning it would be only added lines and my file would be completely green. It is what I got from `diff -ruN --exclude=.git empty_dir working_dir > mypatch_00.patch` but reviewboard does not recognize it as a correct diff. – rxdazn Jan 31 '13 at 10:29
  • Ah, I though what you want to get is diff of first revision in Git, sorry for misunderstanding... I've updated the answer to cover this as well. – Michal Čihař Jan 31 '13 at 12:27
  • Now this is exactly what I was looking for. Thank you. edit: seems like I can only award my bounty in 20 hours. You will get it tomorrow then. – rxdazn Jan 31 '13 at 12:41
  • I recommend using `git read-tree --empty` over deleting the index file. – Richard Hansen Nov 25 '14 at 01:21
  • @RichardHansen Indeed it's cleaner approach, I did not know this command. Updated the answer. – Michal Čihař Nov 25 '14 at 08:55
  • The result is exactly what I am looking for, but is there a solution involving not manipulating the repo? I would like not to tamper with it. I have looked everywhere and this is the closed. – jannej May 25 '16 at 14:49
21

If you want the full diff from nothing to a particular commit, there is an easier way than in the accepted answer:

empty_tree=$(git hash-object -t tree /dev/null) 
git diff-tree -p ${empty_tree} $MY_COMMIT [${files}...]

The value of ${empty_tree} is always 4b825dc642cb6eb9a060e54bf8d69288fbee4904 but I prefer not to have the magic number in there.

Matthew Gilliard
  • 9,298
  • 3
  • 33
  • 48
0

I prefer "empty" as a tag. Simplest way is:

git tag empty $(git hash-object -t tree /dev/null)

Because tag can point to tree-ish directly, without commit. Now to get all files in the working tree:

git diff --name-only empty

Or the same with stat:

git diff --stat empty

All files as diff:

git diff empty

Check whitespaces in all files:

git diff --check empty
Olleg
  • 794
  • 7
  • 7