Skip to content

Instantly share code, notes, and snippets.

@disouzam
Created January 25, 2026 23:53
Show Gist options
  • Select an option

  • Save disouzam/6ad9c43e56add6c64988054e1f9a107f to your computer and use it in GitHub Desktop.

Select an option

Save disouzam/6ad9c43e56add6c64988054e1f9a107f to your computer and use it in GitHub Desktop.
Get file URL in a git repository
#!/bin/bash
function get_file_url() {
repourl=$(echo "$(git config --get remote.origin.url)" | sed -e 's/git@/https:\/\//' -e 's/\.git$//')
branch=$(git branch --show-current)
relative_file_path=$(echo $1 | sed -e 's/\\/\//g')
echo $repourl/blob/$branch/$relative_file_path
}
@disouzam
Copy link
Author

How to use it

get_file_url {path relative to repo's root}

Example:

Consider the following file:

https://github.com/disouzam/data-analysis-with-python-and-pandas-by-chris-bruehl-maven-analytics/blob/main/.vscode/launch.json

To get the URL in command line, you need:

get_file_url .vscode/launch.json
# https://github.com/disouzam/data-analysis-with-python-and-pandas-by-chris-bruehl-maven-analytics/blob/main/.vscode/launch.json

If in Windows, add double quotes embracing relative file path:

get_file_url ".vscode\launch.json"
# https://github.com/disouzam/data-analysis-with-python-and-pandas-by-chris-bruehl-maven-analytics/blob/main/.vscode/launch.json

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