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_branchthis will put you in detached state but if you just want to temporarily test these changes this is fine.git checkout FETCH_HEAD
and after you are done testing you can merge back it to your branch.
git checkout your_prevous_branchgit 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 aTo find it go to Settings > Branches > Branch Protection Rules
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.
and click 'Add Rule'.
Then, enter the name of the branch you want to protect and click the checkbox to require pull request reviews before merging.
By default, this only stops people who are not moderators. There is also another checkbox later down for ensuring that even moderators cannot merge.
Related Topics
Deleting a Specific Line in a Text File
Gitlab Configuration Issues:: Nginx Unicorn Port Conflict
Paperclip: Integration with Mailboxer Gem
Proper Usage of Ruby Statement Modifiers
Creating a Ruby on Rails Environment on Windows, in a Vm Vagrant Box
Explanation of Ruby Code for Building Trie Data Structures
Rails Has_Many Through Form with Additional Attributes
How to Downgrade My Rails Version
Obtaining Number of Block Parameters
Ruby How to Generate a Tree Structure Form Array
How to Fix Difference in Behavior of Activesupport 3.0.0 Compare to 2.X
Does The Rails Orm Limit The Ability to Perform Aggregations
How to Add Values Dynamically to I18N
Why Does Including This Module Not Override a Dynamically-Generated Method