Skip to content

unguiculus/merge-pull

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Python script for merging pull requests

One of the projects I’m working on uses Bitbucket Cloud. Changes are made via pull requests. Unfortunately, Bitbucket only does non-fastforward merges when you merge a pull request via web interface, even if it is only one commit that needs to be merged. There is no rebase or squash option. We don’t like this because it clutters up the history with lots of unnecessary merge commits that do not add any value. There may be cases when you want to merge a series of commits explicitly, but most of the time, this is not what we want. We prefer to have a nice and mostly linear history.

You can work around this limitation by pushing the feature branch the pull request was created from directly to the target branch. Bitbucket recognizes this and closes the pull request automatically. This also gives us rebase and squash options. We create pull requests, keep committing on them until everything is nice and polished, and then squash the feature branch and force-push the squashed commit to the feature branch and the target branch.

While the script was originally developed for Bitbucket Cloud, it can be used for Github as well. The same principles apply.

💡
Update:
Meanwhile, Bitbucket has a squash option. What it still lacks is an option to rebase and merge without squashing, which would then be a fast-forward merge without merge commit.

Features

  • Rebase and merge pull requests optionally squashing commits

  • When squashing commits, the scripts optionally finds the message of the first commit on the pull request and uses that for the squashed commit

  • After merging, the script switches to the target branch and optionally deletes the feature branch (locally and on the remote)

  • Never creates a merge commit. If you want that, use Github’s or Bitbucket’s standard workflow

  • Tested to work with Bitbucket Cloud and Github

Python Compatibility

The script uses Python 3 by default but is compatible with Python 2.7.

Usage

$ merge_pull.py --help
usage: merge_pull.py [-h] [--target-branch TARGET_BRANCH] [--remote REMOTE]
                     [--message MESSAGE] [--assume-yes] [--no-squash]

Merges a pull request. Multiple commits are squashed by default (may be turned
off). The commits are then rebased on top of upstream changes and pushed
directly to the feature branch and the target branch. This automatically marks
the pull request as merged. The script then switches to the target branch and
optionally deletes the feature branch.

optional arguments:
  -h, --help            show this help message and exit
  --target-branch TARGET_BRANCH, -t TARGET_BRANCH
                        The target_branch of the pull request (default:
                        master)
  --remote REMOTE, -r REMOTE
                        The name of the remote (default: origin)
  --message MESSAGE, -m MESSAGE
                        If commits need to be squashed, a commit message for
                        the final commit is required. You will be prompted to
                        re-use the message of the first commit on the feature
                        branch. You may then decide to enter a different
                        message (default: None)
  --assume-yes, -y      Automatic yes to prompts (default: False)
  --no-squash, -n       Do not squash commits (default: False)

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Releases

No releases published

Packages

No packages published

Languages