Skip to content

Instantly share code, notes, and snippets.

@mcroach
Last active April 30, 2024 14:40
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcroach/811c8308f4bd78570918844258841942 to your computer and use it in GitHub Desktop.
Save mcroach/811c8308f4bd78570918844258841942 to your computer and use it in GitHub Desktop.
Using an 'assets' branch to store images in your repo

Storing image assets in your repo and referencing in markdown

Create an assets branch and make the initial commit

git checkout --orphan assets
git reset --hard
cp /path/to/cat.png .
git add .
git commit -m 'Added cat picture'
git push -u origin assets
  • git checkout --orphan assets creates the assets branch without any parent history
  • git reset --hard clears the working tree

Link to the branch head version (most recent)

When viewing a file on GitHub, you usually see the version at the current head of a branch. Your markdown would look like this:

![Readme](https://github.com/{username}/{repo}/blob/{branch}/README.md)

Refers to the {username} {repo} repository, and shows the {branch} branch's current version of the README.md file.

The version of a file at the head of branch can change as new commits are made, so if you were to copy the normal URL, the file contents might not be the same when someone looks at it later.

Link to a specific commit version

If you need to always reference a specific version create a permalink by viewing the file on GitHub and pressing y to convert the asset's URL into a permalink that can then safely be shared.

Alternatively:

git checkout assets
git log --pretty=format:%C(yellow)%h\ %ad%C(red)%d\ %C(reset)%s%C(cyan)\ [%cn]\ %C(yellow)%cr --decorate --date=short
  • git log ... lists the branch commit history; the first column per line is the short commit SHA

Then in your markdown you can reference the permalink:

![A cat](https://github.com/{username}/{repo}/blob/{commit-SHA}/cat.png)
@Tony-Sol
Copy link

git reset --hard

To keep working tree files and prevent commit them into assets branch, suggest to use

git rm --cached -r .

instead

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