Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cleanly clipboard copy code from a GitHub.com diff?

Tags:

github

Consider the following split view code diff from a browser on GitHub.com:

diff

While looking at this diff, I want to clipboard copy the code from the right-hand side (e.g. in order to paste that snippet into a different project in another window).

However, if I try to use the mouse to select the code on the right-hand side in the usual fashion, the code from both sides ends up selected:

enter image description here

And if I hit Ctrl+C / Cmd+C, I end up with kind of a mess on my clipboard which includes copies of the code from both sides, and all of the + and - indicators, which leaves me with a lot of error-prone manual cleanup to do after I paste. (Particularly when copying from a diff that's more long and complex then the simple one I picked out for illustration purposes in this question!)

My question: How can I cleanly and easily copy code from one side of a split view diff in a web browser on GitHub.com to my clipboard?

like image 386
Jon Schneider Avatar asked Jan 25 '18 15:01

Jon Schneider


People also ask

How do I copy text to clipboard?

Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.


1 Answers

Update March 2021: this should be, this time, supported:

Copy one side of split diff

When comparing changes to a file using the split view, you can now select and copy just one side of the diff.
Previously both sides were copied. Selecting over a comment will still copy the comment's contents.

split diff -- https://i1.wp.com/user-images.githubusercontent.com/7900087/110049768-5b9a1600-7d07-11eb-80eb-52ef1ada617c.gif?ssl=1


May 2018:

As mentioned by Hugo Giraudel in his tweet in late April 2018, a bit too quickly:

GitHub finally moved the + and - symbols from diffs to CSS pseudo-elements, making it possible to copy code directly from a diff without having to clean it up.
That is long overdue and fantastic. ✨

https://pbs.twimg.com/media/DcBOo-tXcAArfD_.jpg:large

Actually... this is only true with the help of third-party extension!
Like sindresorhus/refined-github.

Without, you would still copy the '+' and '-'.
GitLab has that feature though.
This is still pending for BitBucket (issue 16204)

timotheecour reports in the comments that it does not help with side-by-side diffs:

the code from both sides ends up selected

This is in the context of using refined-github, a Browser extension that simplifies the GitHub interface: issue 2765.
Update Nov. 2020: that issue just got resolved with PR 3698

https://user-images.githubusercontent.com/46634000/98137788-abd0ab00-1ec2-11eb-9e04-6f20786d5dd8.gif

like image 183
VonC Avatar answered Oct 17 '22 14:10

VonC