Skip to content

Instantly share code, notes, and snippets.

@antichris
Created October 21, 2013 14:55
Show Gist options
  • Save antichris/7085254 to your computer and use it in GitHub Desktop.
Save antichris/7085254 to your computer and use it in GitHub Desktop.
Fork your own Gist

Fork your own Gist

This is a bookmarklet that adds a fully functional Fork button to your own Gist.

If a Fork button is already present in the page, this bookmarklet will set focus to it instead of adding another one.

The change is temporary and the button will disappear as soon as you navigate away from that Gist (clicking the Fork button does this for you as well).


A Closure Compiler optimized version provided below for convenient copy'n'paste.

javascript: (function () {
var fork, present, star, button, form, forks, count;
$('.pagehead-actions li').each(function () {
var action = $(this).find('form').attr('action');
if (action) {
switch (action.split('/').pop()) {
case 'fork':
fork = $(this);
present = true;
break;
case 'star':
star = this;
break;
}
}
});
if (!fork) {
fork = $(star).clone();
}
button = fork.find('.minibutton');
if (present) {
button.focus();
return;
}
form = fork.find('form');
form.attr('action', form.attr('action').split('/').map(function (a) {
return a === 'star' ? 'fork' : a;
}).join('/'));
button.html('<span class="octicon octicon-git-branch"></span>Fork');
forks = $('.counter').filter(function () {
var href = $(this).parent().attr('href');
return href ? href.split('/').some(function (a) {
return a === 'forks';
}) : false;
});
count = fork.find('.social-count');
count.html(forks.text() || 0);
count.attr('href', forks.parent().attr('href'));
$('.pagehead-actions li:last').parent().append(fork);
}());
javascript:(function(){var b,e,f,c,d;$(".pagehead-actions li").each(function(){var a=$(this).find("form").attr("action");if(a)switch(a.split("/").pop()){case "fork":b=$(this);e=!0;break;case "star":f=this}});b||(b=$(f).clone());c=b.find(".minibutton");e?c.focus():(d=b.find("form"),d.attr("action",d.attr("action").split("/").map(function(a){return"star"===a?"fork":a}).join("/")),c.html('<span class="octicon octicon-git-branch"></span>Fork'),c=$(".counter").filter(function(){var a=$(this).parent().attr("href");return a?a.split("/").some(function(a){return"forks"===a}):!1}),d=b.find(".social-count"),d.html(c.text()||0),d.attr("href",c.parent().attr("href")),$(".pagehead-actions li:last").parent().append(b))})()
@livibetter
Copy link

This is quite interesting, because I have never once thought of forking my own Gist or Git repository on GitHub.

I wonder have you contacted GitHub support to discuss the Fork button on Gist since it seems that you can fork your own Git repositories on GtiHub? (There is a Fork button, didn't try it)

@rickcolosimo
Copy link

VERY helpful -- it's exactly what I needed.

For newbies like me: copy the closure-compiled.js into your new bookmark dialog window as the "location." You can name it anything clever, such as "ForkGist."

@gre
Copy link

gre commented Jan 24, 2014

Dear Github,

Why don't you let us fork our own gists!

Best wishes,
gre

@Noitidart
Copy link

This is so great thanks very much for it.
I forked it and added some improvements.

1) did not work when if you the page you are trying to run it on was already starred by you
2) if there were 0 stars but were fork count > 0, the fork count would not be a clickable link (note: if there are 0 forks, the link created will still not be clickable)

See here: https://gist.github.com/Noitidart/8794639

Added details for how users can use it from Scratchpad as bookmarklets don't work and also made a Firefox addon out of it: https://addons.mozilla.org/en-US/firefox/addon/ghforkable/

@JasonKleban
Copy link

You can get a little further by pasting the raw script of http://code.jquery.com/jquery-latest.min.js into the developer tools. This gets past the $ is not defined issue. That allows a second "Star" button to appear which is misnamed but when you click it, might attempt to do the right thing. It tries to post to ./fork with, as previously noted, a critical-looking authenticity_token form field and gets back 422 Unprocessable Entity.

@elhaem
Copy link

elhaem commented Jun 29, 2019

To get a “Fork” button with the right fork-count, change .minibutton to .btn-sm, and .counter to .Counter. However, this does not solve the problem with the authenticity_token.

@RichardBronosky
Copy link

This no longer works because the fork link has been replaced with a form that includes a authenticity_token field. I don't think there is any way around that from the browser.
I now use https://github.com/defunkt/gist from the CLI to:

  • # Define your original gist url (I have to use the ssh url to get it to work for my credential-helper)
    original_url="git@gist.github.com:7085254.git"
  • # Create a new gist
    url="$(date | gist -f temp)"
  • # Clone it
    git clone $url; cd $(basename $url)
  • # Add the original gist as a remote and fetch it
    git remote add alt $original_url; git fetch alt
  • # Check out the original
    git reset --hard alt/master
  • # Push to the new gist
    git push -f origin master

Note: the # are in there so you can copy-pasta the whole block into the terminal 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment