70

I often need to relay my Git output to others. The best way I know how is by doing this:

Right-click Git Bash title bar > Edit > Mark > Select lines > Enter

Now everything I selected is in my clipboard. But I'd like to do it on the command line.

In Windows, you can pipe console output to your clipboard like so:

C:\> dir | clip

But when you try to do that in Git Bash, here's what happens:

> git branch | clip
sh.exe": clip: command not found

Is there a way to pipe Git Bash output to my clipboard in Windows?

5 Answers 5

101

Well, actualy git branch | clip works fine for me. clip command just calls clip.exe from C:\Windows\System32\. Make sure you have clip.exe installed somewhere in your PATH.

4
  • 2
    This worked! After some searching I found that you have to set your PATH variable inside git-bash, not just as an environment variable.
    – Bucket
    Commented Sep 23, 2013 at 14:53
  • 1
    Wow, magical. But the answer here is slightly annoying. For most of us, we don't want to only do a copy of git branch. Here's how to copy the contents of a file: cat [file] | clip. And now you can ctrl+v it wherever you want yay. Commented Nov 4, 2017 at 10:01
  • 2
    Great tip! Worked out of the box! Commented Jul 5, 2018 at 15:37
  • Yes, yes, and yes. Thank you.
    – Erutan409
    Commented Mar 8, 2019 at 14:20
54

copy thing.txt to clipboard

cat thing > /dev/clipboard

Put contents of clipboard into thing.txt

cat /dev/clipboard > thing.txt

I aliased these things to pbcopy and pbpaste so I feel like I'm on my mac.

3
  • 7
    imo this is the best answer
    – rfcoder89
    Commented Nov 25, 2015 at 12:06
  • 1
    The best answer - this handles utf-8 encoding correctly. Piping to clip.exe does not.
    – codeape
    Commented Jan 31, 2022 at 10:54
  • OMG! /dev/clipboard works on Windows GitBash!
    – NeilG
    Commented Feb 3, 2023 at 2:13
5

@madhead's answer is correct - the PATH variable must be set from within git-bash. Here's an elaboration on how to fix this issue, courtesy of Cairnarvon's answer on superuser:

To check what PATH is currently set to:

> echo $PATH

And to set it, assuming a 64-bit architecture:

> export PATH="$PATH:/c/Windows/System32:/c/Windows/SysWOW64"

Result of git branch | clip:

* master
  dev
  dev_foo
3

For Mac users, you can simply pipe the git diff output to pbcopy utility. Something like

git diff | pbcopy

And paste the changes anywhere. No need to first highlight all the git difference from the terminal and then paste it somewhere.

0

Easiest way to copy Git Bash console's entire content:
Right click anywhere on the console > Select All

Keyboard shortcut for the same: Ctrl+Shift+A
Enable this keyboard shortcut by enabling Options > Keys > Ctrl+Shift+letter shortcuts.

mintty version: 3.4.4 (x86_64-pc-mysys) [Windows 19042]

Not the answer you're looking for? Browse other questions tagged or ask your own question.