Skip to content

Instantly share code, notes, and snippets.

@rakaramos
Created November 2, 2018 18:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rakaramos/1c2069a3b1a4edcf46c653f8ec5e1dd2 to your computer and use it in GitHub Desktop.
Save rakaramos/1c2069a3b1a4edcf46c653f8ec5e1dd2 to your computer and use it in GitHub Desktop.
Git command to push to the remote and open browser with pull request page
function git-pr() {
git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null
if [[ $? > 0 ]]; then
# no upstream, lets add that
upstreamAdd="-u"
fi
branch=$(git symbolic-ref --short HEAD)
git push "${upstreamAdd}" origin "${branch}" 2>&1 | egrep -iEo "http(s)://?.+$" | xargs open
}
@rakaramos
Copy link
Author

rakaramos commented Nov 3, 2018

Here's the git alias version

[alias]
    pr = "!f() { \
            upstreamAdd=\"\"; \
            result=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null); \
            if [[ $result > 0 ]]; then \
               upstreamAdd=\"-u\"; \
            fi; \
            branch=$(git symbolic-ref --short HEAD); \
            git push \"${upstreamAdd}\" origin \"${branch}\" 2>&1 | egrep -iEo \"https?://.+$\" | xargs open; \
         }; f"

One should add that to ~/.gitconfig for global and .git/config.

P.S: I'm not sure why Github only returns the PR URL on branch creation. Gitlab, for example, always returns it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment