skip to Main Content

Here’s a common use case for me: I’m trying to install a github project I’ve never used before. It doesn’t go smoothly and I waste 15 minutes (or 15 hours) only to realize, if the README.md had a single line of required information, this ordeal would have taken me 15 seconds.

Like a good samaritan, I want to add that step to the README, but the time and effort is like an order of magnitude larger than the change: fork the github repo, edit my fork of the README, write a commit message and commit, and create a PR.

Is there a more streamlined way to make these one-off changes to README files?


Note:

  1. Here’s how I’m going to approach it if there isn’t a better way: I’m going to install the github cli and automate this in a shell script that takes a github clone url as input.
  2. I want to elaborate on what I mean by "manually" in the title: I don’t mind using an automated script (I’m using WSL.exe with Ubuntu) to streamline this.

2

Answers


  1. The gh pr create command will automatically (will prompt you for confirmation) create a fork for you if youre trying to create a PR on a repo you don’t have push access to, so this can be automated with a bash script like:

    #!/usr/bin/env bash
    
    set -e
    
    # Create a temporary directory and cd into it
    TEMP_DIR="$(mktemp -d)"
    cd "$TEMP_DIR" || exit
    
    # First argument is the repo URL, second is the file to edit
    # defaults to README.md
    REPO="${1?Pass the repo URL as the first argument}"
    FILE="${2:-README.md}"
    
    # Clone the repo, cd into it
    git clone "$REPO"
    cd "$(basename "$REPO")" || exit
    
    # Prompt for branch name, create it
    read -rp "Branch name: " BRANCH
    [[ -z "$BRANCH" ]] && exit 1 # exit if the branch name is empty
    git checkout -b "$BRANCH"
    
    # open the file in $EDITOR, default to nano if not set
    "${EDITOR:-nano}" "$FILE"
    
    # Commit the changes
    git add "$FILE"
    git commit
    
    # open a PR, will fork if needed
    exec gh pr create
    

    This will, of course, install and setup gh beforehand

    Login or Signup to reply.
  2. fork the github repo, edit my fork of the README, write a commit message and commit, and create a PR.

    This is the right way, if you don’t have the GitHub repo’s permission.

    You are good samaritan, we know, but there may be some "bad samaritans" who wants to destroy some repos, you can see many of them in, for example torvalds/linux, or some other famous repositories.

    So, a check for repo owner / permissioned user, is necessary, to avoid anyone’s annoying attack that wants to destroy the repo. That’s why not anyone allowed to direct edit the repo.

    However, fork -> edit -> create PR is sometimes seems complex, so GitHub provides the simpler operation:

    1. Locate to the file in the repo you want to edit.
    2. On the top-right corner of this file, there is a button with icon of a pen, named with "Fork this repository and edit the file". Click it.
    3. Edit this file with your mind.
    4. Apply it, this will create a fork and automatically create a PR for your edit.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search