How to clone gist

Gist is one of the most efficient way to share code snippets, single files and full applications with other people. However one disadvantage of gist is that you can’t share directories, but this is not a major issue considering gist is primarily used to share code snippets.

If you want to make local changes to a gist and push them up to the web, you can clone a gist, make changes and then make commits. It is exactly same process as you would with any Git repository.

Let us look at how to clone gist repository using https

Go to gist repository and get https link. Please find below image to see howto get the https link.

Using following command to clone repository

$ git clone https://gist.github.com/820c117b75d52514b2e58008be07a6eb.git
Cloning into '820c117b75d52514b2e58008be07a6eb'...
remote: Enumerating objects: 44, done.
remote: Total 44 (delta 0), reused 0 (delta 0), pack-reused 44
Unpacking objects: 100% (44/44), done.
Checking connectivity... done.

That is it. You are done. You can cd into the folder and check the files.

How to publish code to gist repo along with github repo

Previously I was keeping code in the same post and I was using code formatting add-ons but then I realised this is very inefficient. While searching for best way to add code to post, I discovered Github gist. It is very useful to add code snippets to blog.

However, while using gist, I stumble across a problem. I had to maintain same code at two places, one on github repository and second on gist. Many times, I was updating my old code, now this lead to problem of updating same two at two places. Github repository was getting updated by git push however, I had to manually updated gist. I did not spend much time to find solution as it was not taking much time, nonetheless this was awkward and I made a mental note to find a workaround sometime later.

Today I did a bit research and found couple of methods. Following is the most efficient method that I found.

Step#1 Create your Github repo as usual.

Please check the post Publish local repository to Github using https for creating local repository for your Github repository

Step#2 Create a Gist repository and add this as another remote

Gist are nothing but repository only. Create gist repository from https://gist.github.com/

Gist name will be first file name that you will create. Keep the first file name same as that of Github repo whose copy you want to maintain as Gist.

Now we need to add this repository as remote with name gistrepo on your local repository. I chose to add gistrepo using https as this is easiest, you can get this link as below

Use following command to add remote gistrepo

git remote add gistrepo https://gist.github.com/43XXXXXXXXXXXXXXXXXXX2.git

You can check remote by using following command

$ git remote -v
gistrepo https://gist.github.com/43e731ef9297ecfe4d727831a5e75a22.git (fetch)
gistrepo https://gist.github.com/43e731ef9297ecfe4d727831a5e75a22.git (push)
origin https://github.com/conquistadorjd/python-07-ml.git (fetch)
origin https://github.com/conquistadorjd/python-07-ml.git (push)

Step#3 Push changes to gistrepo after pushing changes to origin i.e. github repo

First push changes to your Github repository.

$ git add .
$ git commit -m "test changes"
$ git push -u origin master
Username for 'https://github.com': conquistadorjd
Password for 'https://conquistadorjd@github.com': 
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 1.49 KiB | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/conquistadorjd/python-07-ml.git
c813187..5c0472f master -> master
Branch master set up to track remote branch master from origin.

Once done, push these changes to your gistrepo repository. While pushing the changes, please make sure you use -f instead of -u.

$ git push -f gistrepo master
Username for 'https://gist.github.com': conquistadorjd
Password for 'https://conquistadorjd@gist.github.com': 
Counting objects: 12, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (12/12), 2.59 KiB | 0 bytes/s, done.
Total 12 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To https://gist.github.com/43e731ef9297ecfe4d727831a5e75a22.git
+ cecff3f...5c0472f master -> master (forced update)

Now go and check your gist and Github, both will be in sync and wherever you have used the code, it will be get updated.