Skip to content

Instantly share code, notes, and snippets.

@5310
Last active March 24, 2022 01:37
Show Gist options
  • Save 5310/01a41f21eb62d6ca391f to your computer and use it in GitHub Desktop.
Save 5310/01a41f21eb62d6ca391f to your computer and use it in GitHub Desktop.
Advanced Git and GitHub Cheatbook #cheatsheet

An easy way to update gh-pages with master is to simply overwrite it.

  • git push -f origin master:gh-pages

A common workflow for me is to submodule a specially-treated branch of a repository to automatically modify it. The most obvious candidate being gh-pages, but lately I've also wanted to submodule wiki.

Now, submoduling a branch is easy enough when you explicitly specify a remote and branch.

git submodule add -b <branch-name> <remote> <folder-name>

But I want to not have to specify the remote. I want it done automatically. This way, any forks of the repo don't have to edit the NPM run tasks to edit their own repo.

Git is even supposed to do this automatically when you give a relative url. But for some reason this fails when done in C9.io, it looks like the trailing / puts things off!

So I've chosen to do with by querying the remotes and scraping the url.

git submodule add --force -b <branch> `git remote -v | grep "(push)" | head -n 1 | cut -f 2 | cut -d ' ' -f 1` <folder-name>

HOWEVER, an important thing to note is that in most cases if I want to submodule a branch I don't want it committed into the repository. These cases include gh-pages or wiki.

It makes no sense to use submodules for them, and instead I should just clone the branches directly under an ignored path! To be sensible about it, I should only clone a single repo. Git supports this.

Equivalent commands for this would be:

git clone --single-branch -b <branch> `git remote -v | grep "(push)" | head -n 1 | cut -f 2 | cut -d ' ' -f 1` <folder-name>

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