21

This Git question is very similar to another one about SVN.

I have a repo full of big files and I need to add a single file to it. This used to be very easy in SVN.

svn import -m "Adding just a file" file_name http://path/to/svn/repo/file_name

How to achieve this simple task in Git?

Community
  • 1
  • 1
sscarduzio
  • 5,938
  • 5
  • 42
  • 54
  • AFAIK you can't easily. The way to do it would be to fetch just the HEAD commit and all of its tree objects which is enough information to generate the new trees with your new file and commit, and then you can push those back - but I'm not aware of built-in git client support to do it. Your best bet would be to just clone the repository I think - it'll be quicker than worrying about finding a better way. – Rup Oct 27 '13 at 14:58
  • Probably I will end up doing this, but slows me down. The repo includes a collection of rather fatty binary files. So even with --depth=0 it's going to be a pain. – sscarduzio Oct 27 '13 at 15:04
  • @sscarduzio If it's just adding a single file, you can use the Github interface for that. – heinrich5991 Oct 27 '13 at 15:24
  • 1
    Just cloning isn't an option for those of us in the world deprived of bandwidth and storage space. – Andrew May 18 '17 at 12:34

3 Answers3

6

For GitHub (not git itself), the GitHub API provides a way to create a file without cloning:

https://developer.github.com/v3/repos/contents/#create-a-file

PUT /repos/:owner/:repo/contents/:path

# Parameters #
----------------------------------------------------------------
Name     Type    Description
------   ------  -----------------------------------------------
path     string  Required. The content path.
message  string  Required. The commit message.
content  string  Required. The new file content, Base64-encoded.
branch   string  Branch name. Default: repo's default branch.

Minimal example JSON input:

{
  "message": "my commit message",
  "content": "bXkgbmV3IGZpbGUgY29udGVudHM="
}

So you can write a script to base64-encode the file contents, then have it use curl or such to send some JSON as above to https://api.github.com/repos/:owner/:repo/contents/:path

If it succeeds, you get a JSON response back including the GitHub URL(s) for the created file.

Can also update without cloning: https://developer.github.com/v3/repos/contents/#update-a-file

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
3

The repo includes a collection of rather fatty binary files

That is not what a git repo is designed to do, as I explained here.

You can't push something without having fetched first, so unless your repo is hosted in a service which supports adding files through its web interface (like GitHub, as commented by heinrich5991), you won't be able to import just one file.

One alternative would be to have a dedicated server hosting a full copy of your repo at all time, to which you could add one file, and from which you could then push to the target repo.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    I worked around this issue as suggested: cloned to a server, scp new file to server, git add, commit, push to Github. My 'fatty binary files' are dependency jars. And since this is the repo I use for deployment, I cannot use maven to pull them in place. I think this repo can safely be migrated to SVN as soon as possible. – sscarduzio Oct 28 '13 at 09:39
3

There is no way to do that. I just made a test following:

  1. mkdir hello_independent (note:"hello" is my previous repository's name)
  2. git init (note:create a repository with blank here)
  3. git remote set-url origin https://github.com/solio/GitPractice.git (note:set the local repository point to the remote repository.)
  4. vim newfilefromindependentfolder.md (note:add a new file)
  5. git add --a
  6. git commit -m "test commit from independent folder."
  7. git push origin master (note:this operation is the key)

it will show this error:(sorry I can't upload the image of my operation and hint message without 10 reputation)

here is my comprehension of the message:when I do this there are two different independent workflow,they have nothing relationship.So when you want to commit the independent files to the remote repository you must merge the local repository and the remote.So the best way to solve this is git clone the remote repository.

Rup
  • 33,765
  • 9
  • 83
  • 112
solio
  • 31
  • 1
  • 1
    as an impoverished third-worlder, this is a major drawback that I wish i had known before committing to git. How can I raise an issue with the git community? – Andrew May 18 '17 at 12:37