Skip to content

Instantly share code, notes, and snippets.

@probonopd
Last active January 6, 2019 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save probonopd/87a74afda6a60ac58d5c861985ca765f to your computer and use it in GitHub Desktop.
Save probonopd/87a74afda6a60ac58d5c861985ca765f to your computer and use it in GitHub Desktop.
Squash GitHub pull requests as the sender

Combine existing GitHub pull requests into one commit

By the "receiver" of the pull request

If you would like to see only one entry for the Pull Request in your project's history, then please enable this GitHub functionality on your repo. It allows you to squash (combine) the commits when merging.

By the "sender" of the pull request

Unfortunately, the "sender" of the pull request does not have a button in the GitHub GUI to squash the commits in an existing pull request. Instead, they need to to it by hand using the command line, which is unfortunately cumbersome but can be done like this:

# Install and configure the git command line client
which apt-get >/dev/null 2>&1 && sudo apt-get -y install git
which yum >/dev/null 2>&1 && sudo yum -y install git nano
which zypper >/dev/null && sudo zypper -n install --no-recommends git
sudo git config --global user.email "<YOUR_GITHUB_USERNAME>@users.noreply.github.com"
sudo git config --global user.name "<YOUR_GITHUB_USERNAME>"
sudo git config --global core.editor nano

# Fold all commits into one
git clone https://github.com/<YOUR_GITHUB_USERNAME_OR_PROJECT>/<GITHUB_REPO_NAME>
cd <GITHUB_REPO_NAME>/
git checkout <NAME_OF_THE_BRANCH> # e.g., patch-1
git rebase -i <SHA_OF_YOUR_FIRST_COMMIT>^ # e.g., 7d6088a2ab3efbdc4e9e3e785e4075e305f15b5d^ - note the "^" at the end
# Now edit, in the first line use "p" (pick = use commit) and in all following lines use "f" (fixup = like "squash", but discard this commit's log message)

# Upload the changes to GitHub; this will update the GitHub pull request
git push --force

This should be scriped to fully automate it. Volunteers?

Here are the beginnings, not working yet:

# More information:
# https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History

INPUT="https://github.com/probonopd/linuxdeployqt/pull/317"

TARGET_GH_USER=$(echo "$INPUT" | cut -d '/' -f 4)
TARGET_GH_REPO=$(echo "$INPUT" | cut -d '/' -f 5)
TARGET_GH_PR=$(echo "$INPUT" | cut -d '/' -f 7)

APIURL=$(wget -c "https://api.github.com/repos/$TARGET_GH_USER/$TARGET_GH_REPO/pulls/$TARGET_GH_PR/commits" -O - | grep "/git/commits/" | head -n 1 | cut -d '"' -f 4)
MAIL=$(wget -c "$APIURL" -O - | grep '"email":' | head -n 1 | cut -d '"' -f 4)
SHA=$(wget -c "$APIURL" -O - | grep '"sha":' | head -n 1 | cut -d '"' -f 4)

echo "$MAIL"
echo "$SHA"
echo "$TARGET_GH_USER"
echo "$TARGET_GH_REPO"
echo "$TARGET_GH_PR"

# Need to generate something like
# p f7f3f6d
# f 310154e
# f a5f4a0d

export GIT_AUTHOR_NAME="..." # TODO: Figure out
export GIT_AUTHOR_EMAIL="$MAIL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment