Git: Forcing Tests Before Pushing to Local or Remote Master

Git: forcing tests before pushing to local or remote master

Use a git pre commit hook which executes rake spec (taken from Tips for using a git pre-commit hook).

git stash -q --keep-index
`rake rspec`
RESULT=$?
git stash pop -q
[ $RESULT -ne 0 ] && exit 1
exit 0

Testing latest code on Git remote master branch before merging

you can fetch the remote branch which you can reference using FETCH_HEAD and then checkout that branch.

git fetch remote_branch

git checkout FETCH_HEAD

this will put you in detached state but if you just want to temporarily test these changes this is fine.

and after you are done testing you can merge back it to your branch.

git checkout your_prevous_branch

git merge FETCH_HEAD

How can I test if I am allowed to force push?

The only completely-reliable test for whether you have permission to do X (for some arbitrary X, often not even Git-related) is to attempt to do X and see if it succeeds. The fundamental problem with "test if X is allowed, then do X" is that the answer could change in between the test and the attempt. (Some systems do support ask-permission-first reliably, but most don't: most give you an answer that expires by the time you get it.)

That said, Git's force-push permissions are determined by the other Git, and there's no way to ask it other than to try it anyway! The closest you can come to asking the other Git is to ask the other system about its Git, in some other-system-specific way. Your best bet might be to create a temporary branch, force-push to it, then delete it. If the force-push step succeeds, then you know that you at least had permission to force-push to that one branch for that one moment.

Since common host sites like GitHub allow administrators to "lock" or "protect" some particular branch(es). The details of how this works, and who has what permission, are up to those hosting sites. Here is the GitHub page on their branch restriction options.

Updates were rejected because the tip of your current branch is behind its remote counterpart

The -f is actually required because of the rebase. Whenever you do a rebase you would need to do a force push because the remote branch cannot be fast-forwarded to your commit. You'd always want to make sure that you do a pull before pushing, but if you don't like to force push to master or dev for that matter, you can create a new branch to push to and then merge or make a PR.

Prevent pushing to master on GitHub?

Since the original question / answer, Github has added a new option for this to the restricted branches UI which allows you to set this up.

Require pull request reviews before merging When enabled, all commits must be made to a non-protected branch and submitted via a
pull request with the required number of approving reviews and no
changes requested before it can be merged into a branch that matches
this rule.

To find it go to Settings > Branches > Branch Protection Rules
and click 'Add Rule'.
Github UI after following instructions
Then, enter the name of the branch you want to protect and click the checkbox to require pull request reviews before merging.
Github UI with the button to click
By default, this only stops people who are not moderators. There is also another checkbox later down for ensuring that even moderators cannot merge.
Github UI with the other button to click



Related Topics



Leave a reply



Submit