Skip to content

Instantly share code, notes, and snippets.

@johan
Forked from johan/autofork.js
Last active October 17, 2022 23:14
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save johan/1306266 to your computer and use it in GitHub Desktop.
Save johan/1306266 to your computer and use it in GitHub Desktop.
(Re)fork any gist, including your own

Want to fork your own gists? No fork button? No problem! Install this user script by clicking refork.user.js' "raw" link down below: ⇓

// ==UserScript==
// @name (Re)fork any gist, including your own
// @namespace https://github.com/johan
// @version 2.0 - updated 2015-06-21
// @description Adds a "fork" button to gists missing one at gist.github.com, so you can create multiple forks
// @match https://gist.github.com/*
// @include https://gist.github.com/*
// ==/UserScript==
if (/^\/[^\/]+\/\d+/.test(location.pathname) &&
!document.querySelector('form[action$="/fork"]')) {
var actions = document.querySelector('.gisthead .pagehead-actions')
, auth = document.querySelector('input[name="authenticity_token"]').outerHTML
, fork = document.createElement('li')
, url = location.pathname + '/fork'
;
fork.innerHTML = '<form accept-charset="UTF-8" action="'+ url + '" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓">' + auth + '</div><button class="btn btn-sm" type="submit"><span class="octicon octicon-repo-forked"></span>Fork</button></form>';
actions.appendChild(fork);
}
@spivurno
Copy link

Awesome user script. Works like a charm. :)

@andrioli
Copy link

andrioli commented Oct 3, 2012

Really nice

@jlewin
Copy link

jlewin commented Jan 9, 2013

Does this still work for anyone? I haven't been on the site in a while and I don't recall what it used to look like but it appears the content on the site changed and broke the script logic.

Never mind, found the announcement: https://github.com/blog/1276-welcome-to-a-new-gist

@jlewin
Copy link

jlewin commented Jan 9, 2013

Interesting. I forked the script and updated it to account for the site changes but the userscript only runs when a full page refresh is invoked (confirmed with a quick console.log statement before any conditional logic). Some research into the problem reveals they are using pjax https://github.com/defunkt/jquery-pjax/ to improve perceived page load times and the ajax style page refreshes bypass the page load mechanism that normally would cause the userscript to be invoked.

Theoretically you could catch the pjax load/completed events and run the script logic but hooking the custom events without jQuery is too big a challenge to tackle tonight.

$(document).on('pjax:complete', function() {
// Call button inject logic here
})

@jlewin
Copy link

jlewin commented Jan 10, 2013

It seems jQuery doesn't use dom events (except in some fallback scenarios) and thus I was unable to hook 'pjax:complete' or any other jQuery event for that matter. As a workaround, a MutationObserver was registered to monitor changes to the '#js-pjax-container' element and button injection is now rerun after dom modifications are detected. Probably not the cleanest or most elegant solution but given the constraints of the extension sandbox, it seemed reasonable. I'd love to see other takes on the problem but for now this approach seems to work.

Btw, very creative solution Johan... thanks!

@eric-wieser
Copy link

URL has changed to location.pathname + '/fork'

@Noitidart
Copy link

i came across this from stack over flow when i searched on bing "github how to fork gist"

this is exactly what i needed. however has this been updated to included the location.pathname + '/fork' like eric-wieser says?

also what do you mean user script? i dont get how to use this. im having to convert it to a bookmarklet.

@Noitidart
Copy link

found updated version here:
https://gist.github.com/U-D13/7085254

@RichardBronosky
Copy link

This will never work again unless github blesses it. Do a view-source and search for authenticity_token. Game over, man. Game over.

@RichardBronosky
Copy link

RichardBronosky commented Nov 14, 2019

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:1306266.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/main
  • # Push to the new gist
    git push -f origin main

Note

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

Update on 2022/04/14

@ackvf
Copy link

ackvf commented Apr 13, 2022

@RichardBronosky I assume github does not recognize it as a fork and doesn't link the two gists together, right?

@RichardBronosky
Copy link

RichardBronosky commented Apr 15, 2022

@RichardBronosky I assume github does not recognize it as a fork and doesn't link the two gists together, right?

@ackvf I updated my comment to include a link to a fork I just made of this gist. The gist.github.com web interface doesn't indicate the fork, but the complete history is there including accurate authors and dates. I have an old fork of this repo https://gist.github.com/RichardBronosky/4d8a7aed9f8dc40eea669a0985ac8d79 and it does indicate the fork. I'll compare the 2 and see if I can find anything in .git folder that will serve as the fork indicator.

@ackvf
Copy link

ackvf commented Apr 18, 2022

Thank you. I've already been cloning my gists using Sourcetree and actually I've been using local repo to manipulate / clean the gist history.
Anyway, forking own repo does not seem to be supported at all: "message": "You cannot fork your own gist."

community/community#14882

@getify
Copy link

getify commented Oct 17, 2022

@RichardBronosky did you ever figure out what specifically makes a fork be indicated in the gist web interface?

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