65

I am working on a small project with gist and since it is growing I would like to put it on github.

Let's suppose that:

The ideal solution would be one that pushes my changes on both the gist and the github repository.

Christopher Chiche
  • 15,075
  • 9
  • 59
  • 98

6 Answers6

49

You can add the github repository as a remote to your checked out gist repository.

git clone git@gist.github.com:1234.git
git remote add github git@github.com:ChrisJamesC/myNewProject.git

Push it to initialize the git on github

git push -u github master

If your github repo wasn't quite empty (you created it with a README, license, etc. which you don't mind losing) you will have to do a force overwrite on your push

git push -f -u github master

If you don't want to lose the exiting commits and files, see https://stackoverflow.com/a/40408059/117471

This will also change the upstream of the branch, so github will be default.

You now can rename the remote of gist:

git remote rename origin gist

Each time you make changes (or pull changes from github/gist), you can do:

git push                 # To github
git push gist master     # To gist

This will also push back your changes to the gist and not only the github repo.

Community
  • 1
  • 1
gzm0
  • 14,752
  • 1
  • 36
  • 64
  • Edit: When the git on github is empty, you can directly push, without reseting the branch. Makes it nicer. – gzm0 Dec 02 '12 at 17:04
  • 2
    It sets up the pushed branch as a remote-tracking branch of the remote (causes pull to work without arguments). – gzm0 Aug 06 '14 at 19:21
  • 1
    The git clone command for the gist, above did not work for me, I had to change it to the following: ```git clone git@gist.github.com:1234.git``` – Noah Sussman May 25 '16 at 19:46
  • 1
    This seems indeed be the preferred way of doing so (judging from Gist's interface). I updated the answer. Thanks for the heads-up. – gzm0 May 26 '16 at 09:37
  • This is a great Q&A, but I think it is outdated. When I tried using `git clone` with the gist, I received errors. I suspect that the solution may be to use the [GitHub CLI](https://cli.github.com/manual/gh_gist_clone) and a command like `gh gist clone `. – EJ Mak Dec 12 '22 at 00:51
44

Github now has a new feature - import from another repository. So the steps are much simplified:

  1. Use the import feature and specify the URL of the repository.
  2. Profit!

Update:

You don't have to create a repo. The + button in the top right corner now has 'Import Repository' as an option.

screenshot from github

Happypig375
  • 1,022
  • 1
  • 12
  • 32
Konrads
  • 2,206
  • 2
  • 30
  • 45
  • 1
    You don't have to create a blank repository. Just visit https://github.com/new/import and fill in the 2 boxes. There are some caveat that I cover in my answer here http://stackoverflow.com/a/40407301/117471 – Bruno Bronosky Nov 03 '16 at 17:04
5

Clone the gist (e.g. git clone git://gist.github.com/123.git) to your local harddrive, then set the new URL for origin (e.g. git remote set-url origin https://github.com/ChrisJamesC/myNewProject). Push to the new repository (git push origin master). Happy gitting!

knittl
  • 246,190
  • 53
  • 318
  • 364
  • You can add a new remote instead of overwriting `origin`. Then you could push to both repositories. But to be honest, it does not make much sense to maintain a Gist as well, when you already have a full repository. – poke Dec 02 '12 at 17:03
  • 2
    I am working on a visualisation project and the http://bl.ocks.org/ website is based on gist. So I would prefer to keep the gist but it is very optional. This question is also to see a bit beyond my current problem. Thanks for your help! – Christopher Chiche Dec 02 '12 at 17:07
4

Sorry for shaking an old question, and that I can't comment, but in the second step as given by gzm0 you may have to use --force, i.e.

git push -f -u github master

It may have been because there was a README in the Github repo, but I guess others may run into this too.

bpj
  • 159
  • 2
  • 5
  • 2
    Beware that use of `--force` is very dangerous in general since it overwrites the repository. Perhaps better to create another new one without the README (which will be disappeared anyway). See [--force considered harmful; understanding git's --force-with-lease - Atlassian Developers](https://developer.atlassian.com/blog/2015/04/force-with-lease/) – nealmcb Apr 17 '17 at 15:54
1

You can clone the gist locally.

Add the github repository as new remote.

Push your local repository to the new github remote.

Delete all files in your gist but the README.md file . in this file you can write a hint that the gist has moved to a new repository

rubo77
  • 19,527
  • 31
  • 134
  • 226
1

My edit to the accepted answer was getting lengthy so I created a separate answer to hold it.

If your repo is not empty and you don't want to lose the exiting commits and files, the accepted answer doesn't apply to you. You will have to either:

  1. If you do not care about the commit history of the gist...
    • Copy the files over, git add, git commit
  2. If you want to keep the commit history of the gist...
Community
  • 1
  • 1
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149