gitemailgithubsendmailgit-send-email

Unable to use git send-email to send source code and patches


I have a directory created locally: /home/Tegra.

I have created following Files inside /home/Tegra:

hello_world.c hello_world_1.c hello_world_2.c

Each file is incrementally modified. I have also created patches as:

diff -u hello_world.c hello_world_1.c > hello_world_1.patch
diff -u hello_world_1.c hello_world_2.c > hello_world_2.patch
  1. Now I want to first send an email using git send-email to email address abc@xyz.org. which should contain hello_world.c file

  2. Then I want to send second email with hello_world_1.patch file as attachment.

  3. Then I want to send third email with hello_world_2.patch file as attachment.

Unfortunately, I am not even able to do the step 1:

My git has been properly configured with relevant smtp server tls 587 port.

I tried following command:

git send-email --to abc@xyz.org --subject My Hello hello_world.c

I get following error:

Cannot run git format-patch from outside a repository

Where does repository come into picture. SHould I have to maintain first a repository of my code.

Edit: For step 1: As per comments below we need a repository:

  1. Created a Empty Repository on Github : "MyRepo"
  2. Cloned it on local machine. (using git clone )
  3. Then added the first file "hello_world.c" into the Directory /MyRepo".
  4. Then >>git add hello_world.c
  5. Then >>git commit -m 'My First source'
  6. Then >>git push -u origin master
  7. After that, I typed: git send-email --to=abc@xyz.org --subject="[asdasdas] assd asdasd" hello_world.c

Now I get an Error:

No subject line in hello_world.c ? at /usr/lib/git-core/git-send-email line 584

Solution

  • Then added the first file "hello_world.c" into the Directory /MyRepo".

    First make sure you have actually committed anything in your cloned empty repo.

    git add .
    git commit -m "new commit"
    git push
    

    Second, the git send-email doc does mention:

    --subject=<string>
    

    Specify the initial subject of the email thread. Only necessary if --compose is also set.

    Make sure to use --compose.

    This format expects the first line of the file to contain the "Cc:" value and the "Subject:" of the message as the second line.

    That would work with a .patch, not the source itself.
    See git format-patch, and "How to send patches with git-send-email" for a more complete example:

    For the last commit:

    git send-email -1  --to=abc@xyz.org --subject="[asdasdas] assd asdasd"
    

    Third, a simpler solution would be to use git bundle. That generates one file that you can send any way you want, and from which the receiver can pull/clone from. It acts (that one file) as a bare git repo.