In my project, it is required to add changes to the master branch using pull requests from feature branches. Currently, the repository is configured to forbid direct pushing to master, but in the past it was allowed.
Is it possible to find commits that were pushed directly to master
, without a pull request?
You can do a forced push. The forced push will erase all commit history of the remote repository's branch, and replace it to your branch. Check the answers for doing forced pushes in "How do I properly force a Git push?".
You can list the pull requests associated with a commit using GET /repos/:owner/:repo/commits/:commit_sha/pulls , which will show the pull requests which the given commit is associated with. This does mean that you'll need to check every commit to see if its associated with the PR.
So commiting changes without pushing allow the save-load behaviour done locally during development. Once you are happy with your work, you then commit AND push.
As a general rule, when merging a pull request from a feature branch with a messy commit history, you should squash your commits. There are exceptions, but in most cases, squashing results in a cleaner Git history that's easier for the team to read.
If your pull requests are kept around you can simply check that the second parent of every merge to master is a pull request.
Github keeps pull requests as refs so you can list them with git ls-remote u://r/l refs/pull/*/head
, trying to find an equivalent on Atlassian's service felt like swimming in lard so you get to figure that out yourself.
For repos that use the Github convention for publishing pull requests, something like
awk 'ARGIND==1 { prhead[$1]=1; next}
!prhead[$3] { print $0, "was not merged from a pull request" }' \
<(git ls-remote u://r/l refs/pull/*/head)
<(git rev-list --first-parent --merges master --parents)
will do it.
You can (unsurprisingly enough) push anything the repo's set up to accept, so before your restrictions were in place someone could have pushed a fast-forward. Those will show up as non-merge commits on master:
git rev-list --first-parent --no-merges master
will list every commit that was made on master and not merged.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With