DEV Community

Kay Gosho
Kay Gosho

Posted on

Merge GitHub Pull Requests without leaving command line

My previous workflow in my personal project was like this:

  • git checkout -b awesome-feature
  • Edit file
  • git add .; git commit -m 'created awesome feature'
  • git push origin awesome-feature
  • hub pull-request Using hub command
  • Open GitHub PR page
  • Press the "Merge Button"

Although I use some alias in Git, Open GitHub PR page and Press the "Merge Button" are stressful task. I cannot be patient of browser loading!

Basically, I don't need to review my own Pull Request for my personal project, so I create a tiny function to merge Pull Request from command line:

function merge
    set repo (pwd | perl -pe 's#.+github.com/##')

    curl \
        -XPUT \
        -H "Authorization: token $GITHUB_TOKEN" \
        https://api.github.com/repos/$repo/pulls/$argv[1]/merge
end
Enter fullscreen mode Exit fullscreen mode

More Detail Lines

Use that command like this:

merge 22
Enter fullscreen mode Exit fullscreen mode

Quite simple.

It assumes that you are in /path/to/github.com/user/repo. You can also use git remote -v to get current directory's repository info.

I publish my dotfiles to GitHub, so I made GITHUB_TOKEN environment variable, which is very sensitive info.

https://github.com/acro5piano/dotfiles

Oldest comments (8)

Collapse
 
vbjay profile image
Jay Asbury

You do realize that if you do the merge locally and push the commits to the GitHub remote, the pull request will say merged?

Collapse
 
acro5piano profile image
Kay Gosho

Yeah... I forgot the feature! Haha

git checkout master
git merge awesome-feature
git push origin master

is enough! Thanks.

Collapse
 
chrisbaltazar profile image
Christian Baltazar

Hi Jay, I'm having an issue on that note actually, I did the following on my side:
git checkout feature
git checkout master
git merge --squash feature
git commit -m "Merged feature by squash"
git push origin master
git push --delete feature

But the original PR created still says OPEN, not merged... could this be because I also deleted the related branch?? I was expecting to be as easy as you said...

Collapse
 
emjayoh profile image
Matt Ogram

yeah or just merge the branch locally.. heh.

Collapse
 
pauguillamon profile image
PauGuillamon

Interesting tool! But if you're working alone and no one is going to review the code, then why not avoid the PR process and merge/commit directly into the main branch?

Collapse
 
acro5piano profile image
Kay Gosho

Always pushing to master is okay, but I want to log my work explicitly.
And if I develop my own OSS, lots of pull request is good for the project insights, as it looks development is active.

Collapse
 
pauguillamon profile image
PauGuillamon

What do you mean by log your work explicitly? You can still create your feature branches and merge them to master. Or am I missing something?
IMHO development will still look active if you update your project regularly, without needing to do PRs if you're the only developer.

Thread Thread
 
acro5piano profile image
Kay Gosho

What do you mean by log your work explicitly?

I mean, people can follow code changes by looking at PRs of the repository.

Yes, as you said we don't have to create each PRs if I'm the only developer.
However, I sometimes want to do self-review on GitHub.