Skip to content

Instantly share code, notes, and snippets.

@ScriptAutomate
Last active June 7, 2022 08:55
Show Gist options
  • Save ScriptAutomate/a3240e8767f0e8e70644ebf3ca4b74fb to your computer and use it in GitHub Desktop.
Save ScriptAutomate/a3240e8767f0e8e70644ebf3ca4b74fb to your computer and use it in GitHub Desktop.
GitHub, GitLab, and git: Cheat codes

GitHub, GitLab, and git: Cheat codes

GitHub: Reviewing PRs with tons of modified files and Load diff buttons

For those doomed to try reviewing a major amount of diffs in GitHub for a single PR:

  • Scroll down the entire list of changes so that GitHub loads all of the collapsible boxes associated with all of the changes
  • Open up the inspector/console in the browser devtools. This is probably accessed easily with ctrl+i. We want to be in the Console, which in Firefox is ctrl+shift+k.
  • Type in the following to expand all diffs, in order to avoid needing to click on hundreds of Load diff buttons:
// Expand boxes only for unviewed files that haven't checked the 'Viewed'
// checkbox at the top-right of files within the file reviewer in GitHub UI
Array.from(document.querySelectorAll('.file')).filter(container => !container.querySelector('input.js-reviewed-checkbox:checked')).forEach(pnode => pnode.querySelectorAll('.load-diff-button').forEach(node => node.click()))

Wish GitHub had a Load all diffs button!

It also would be a very good idea to use the checkboxes on the top-right of each file within the GitHub PR UI! It ensures that only modified files in new commits will only uncheck files that have been modified, avoiding needing to review all files over again.

Source GitHub conversation where I found the snippet:

I initially found this JS broswer hack useful when reviewing this PR, which had over 1,000 files modified:

Pulling down PRs for local checkout/review

Easiest functionality for me has been to use the gh (GiHub CLI) and glab (GitLab CLI) tools.

# pwd / within the repo dir, for GitHub-based repos
gh pr checkout <pr-number>
# pwd / within the repo dir, for GitLab-based repos
glab mr checkout <mr-number>

NOTE: GitHub uses "Pull Requests" (PRs), while GitLab refers to them as "Merge Requests" (MRs).

Alternatively, git commands can be used to checkout a PR/MR:

# $ID = pull request id
# $BRANCHNAME = name of new branch you wish to create for it locally
git fetch origin pull/$ID/head:$BRANCHNAME
git checkout $BRANCHNAME

Source: https://stackoverflow.com/questions/27567846/how-can-i-check-out-a-github-pull-request-with-git#30584951

Syncing local, forked master branch with upstream master branch

In repositories you are contributing to, it's a good idea to configure your local forked copy to also reference the upstream repo you forked from. This allows for easily updating your fork to be synced with the upstream.

Configuring git remotes

Example, using the salt repo. This path assumes that you're setup for Connecting to GitHub with SSH.

# Using SSH key method, clone down your fork
git clone git@github.com:<forked-repo-path>/salt.git
cd salt

# Using SSH key method for upstream remote config
git remote add upstream git@github.com:saltstack/salt.git

Using git-sync

I store this script as git-sync somewhere in my $PATH.

# Example file location: ~/.local/bin/git-sync
# Simple script for git usage
# git-sync master
# git-sync main
git checkout $1
git fetch upstream
git pull upstream $1
git push origin $1

From within my salt repo, I can now sync a specific branch.

git-sync master

Now my fork of salt has synced the master branch with what is upstream in the main salt repo.

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