pre-push hook to mimic gitlab's behaviour on githubIf you are using Gitlab, you should have noticed that when you push to origin, you receive a message like the following:
remote: Create merge request for pre-push_hook: remote: https://gitlab.com/group/project/merge_requests/new?merge_request%5Bsource_branch%5D=branch remote: To gitlab.com:group/project.git
This is incredibly handy because you do not need to open your browser, reach for gitlab, open project, and select your merge request. You can just follow link from your terminal and create your merge request straight away.
For details about gitlab's implementation, see this merge request.
In order to achieve the same thing with github, you can use a pre-push hook.
Inside your project directory create the file .git/hooks/pre-push
with the following content:
#!/bin/sh # Full repository name (group and project) repo=$(echo $(git ls-remote --get-url) | sed 's/git@github.com:\(.\+\)\.git/\1/') # Branch name branch=$(git rev-parse --abbrev-ref HEAD) # Display link to pull request echo "Create pull request for pre-push_hook:" echo -e "\thttps://github.com/$repo/compare/$branch?expand=1" exit 0
Do not forget to make it executable for git:
chmod +x .git/hooks/pre-push
And you are done!
Next time you will git push,
you'll receive on your terminal the same link as if you were pushing to gitlab.
If your are usually pushing to a branch different of the default one, you can also set it with the following:
- echo -e "\thttps://github.com/$repo/compare/$branch?expand=1" + echo -e "\thttps://github.com/$repo/compare/default_branch..$branch?expand=1"