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://email@example.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://firstname.lastname@example.org': 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.