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.

Publish local repository to Github using https

Follow below steps to push local repository to github

Step#1.

Create new empty repository on github https://github.com/new
Please make sure you do not select Initialize this repository with a README checkbox

Step#2.

If you want to create new local repository

echo "# any comment you want to add" >> README.md
git init
git add .
#git add README.md  # this works as well
git commit -m "first commit"
git remote add origin https://github.com/XXX-your-username-XXX/XXX-your-repo-name-XXX.git
git push -u origin master 

Last step will ask you for  Github userid and password.
It will take some time to upload your changes to github depending on size of your changes. Once this is completed, go yo github and verify.

Git Quick Reference

Git is the most useful tool for developers but if someone does not use it for a while, it all seems confusing. Git is tricky. Here are some of the most commonly used command.

  • Copy remote repository to your local folder.
git clone ssh://abc...xyz.git/  
  • Create new git repositiory
git init
  • To get list of files changed in directory
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: MySQL/MySQL_Select.py

Untracked files:
(use "git add <file>..." to include in what will be committed)

MS Excels/xlsx_read_into_pandas.py
MySQL/MySQL_update.py
image/

no changes added to commit (use "git add" and/or "git commit -a")
  • View remote repository name
$ git remote -v
origin https://github.com/conquistadorjd/python-01-utilities.git (fetch)
origin https://github.com/conquistadorjd/python-01-utilities.git (push)
  • Once you have made your changes and ready for deployment, you following set of commands for deployment.
git add .  
git commit -m "commit comment"  
git push origin master  

For heroku application use following

git push heroku master  
  • What is the difference between git push origin and git push origin master

Following command only pushes your master branch to origin

git push origin master

Following command pushes all your branches to origin

git push origin
  • Get commit history
$git log