Fetching PRs opened by others for reviewing or testing new functionalities is a common task. Generally, you’d do the following for this:
git fetch upstream pull/<PR_NUMBER>/head:<BRANCH> && git checkout <BRANCH>
Typing this everytime can soon become irritating and you can’t have an alias for it because PR number and branch name will vary.
So, shell function comes to the rescue! Functions can take parameters, so it makes our lives easier in this case.
So, if you want a shorthand for the above commands, try the following:
- Put this in your shell configuration file like, .bashrc or .zshrc :
Source the config file with
Fetch and checkout a PR branch with:
pr <PR_NUMBER> <USER:BRANCH>
The shell function is a bit opinionated. I prefer to name branches as originally named by the author in their fork. This helps keep track when you have a lot of branches.
GitHub has recently introduced a copy button beside the branch name.
So, it makes it easier just to click that button to copy the branch name but it comes with a user name as well. But you don’t have to worry about it, just copy it as it is with the button and use that as the second variable to call the shell function. The first line of the function splits and takes only the branch name to use it as the new branch name.