Is there a way to get push accepted and merged automatically if there are no conflicts?
I know that this is a bad practice, but I have a special case in mind where peripheral git repo is updated by bots
Is there a way to get push accepted and merged automatically if there are no conflicts?
I know that this is a bad practice, but I have a special case in mind where peripheral git repo is updated by bots
Is there a way to get push accepted and merged automatically if there are no conflicts?
The flow of reasoning should be reversed - you can only know that there are no conflicts after you attempt to merge. The merge is necessary when the local and remote trees diverge - and that divergence is detected on the remote only after you attempt to push, that is, after you bring your local tree to the remote. In that case it would be common for git on the remote side to reject the push to let you sort out the differences - you are in the best position to do so on the client side rather than the remote side.
If you are sure that there are no conflicts, you can put your trust in git pull
- it merges with the FETCH_HEAD in the default mode. For example,
git pull --no-edit
git push
The --no-edit
option isn't encouraged but will avoid dumping you into the editor by accepting the default merge message. You can alias the two into one command that fails if the merge isn't silently successful,
git config alias.merge-n-push '!f() { git pull --no-edit && git push; }; f'
I can't guarantee that while I'll do pull and merge, nothing will change on remote
You cannot of course, git is a distributed version control system, and that kind of race is inherent and even desired. An approach that would somehow lock the remote while it goes back and forth with you deciding on what and how to merge will not be git-idiomatic. You need to simply push and pull and push until you succeed. And in practice this is rarely an issue, as in the worst case you can ask your peers to hold off while you finish your complicated merge/push.
Nevertheless you can mitigate this somewhat by checking the remote: How to know local repo is different from remote repo, without fetch?