Find a Pull Request on Github where a commit was originally created

后端 未结 6 2136
暗喜
暗喜 2020-12-12 11:14

Pull Requests are great for understanding the larger thinking around a change or set of changes made to a repo. Reading pull requests are a great way to quickly \"grok\" a p

6条回答
  •  庸人自扰
    2020-12-12 11:37

    I've been a heavy user of the cheeky little link on the GitHub web UI but wanted a faster way that would take me straight there from the terminal, basically a git pr SHA command. It took a bit of doing, but here's a series of git aliases that will set that up for you on MacOS:

      git config --global alias.merge-commits '!funct() { git log --merges --reverse --oneline --ancestry-path $1..origin | grep "Merge pull request";  }; funct'
      git config --global alias.pr-number '!funct() { git merge-commits $1 | head -n1 | sed -n "s/^.*Merge pull request #\\s*\\([0-9]*\\).*$/\\1/p"; }; funct'
      git config --global alias.web-url '!funct() { git config remote.origin.url | sed -e"s/git@/https:\/\//" -e"s/\.git$//" | sed -E "s/(\/\/[^:]*):/\1\//"; }; funct'
      git config --global alias.pr '!funct() { open "`git web-url`/pull/`git pr-number $1`" ;}; funct'
    

    If you're on Linux, replace open with xdg-open and you're golden. It shouldn't be too difficult to adapt to work with GitLab either.

    Note this will only work if you practicing GitHub flow and creating explicit merge commits.

    I've written a more detailed explanation of how this all works here: https://tekin.co.uk/2020/06/jump-from-a-git-commit-to-the-pr-in-one-command

提交回复
热议问题