Search code examples
gitmacosclipboard

How to pass the current clipboard value to "git commit -m" on Mac?


I'd like to pass the current value stored in the clipboard as the message of the git commit -m command. I tried the following:

# 1st way
pbpaste | xargs git commit -m
# 2nd way
git commit -m pbpaste

But neither command worked. How can I pass the current clipboard value to git commit -m on Mac?


Solution

  • You don't want to use xargs here because it does some splitting that you won't want. You could do git commit -m "$(pbpaste)", which uses a command substitution (the $()) syntax. That takes the standard output of a command, strips off any trailing newlines, and inserts it into that spot, which, with the double quotes, is sufficient to use that as the message.

    However, on many systems, there's a maximum length for command line arguments, and it can be rather small. A nicer way is pbpaste | git commit -F-, since the -F- reads from standard input and doesn't have the problem with command-line argument length. That ensures that even if you wrote a minor novel for your commit message (which some Git contributors tend to do on occasion), then your command will succeed.