There’s no ‘git cp filename’?

Here’s a sequence of unbelievable things:

  1. Yes, despite a lifetime in Subversion, I’m really this new to git!
  2. I’m going to link to Livejournal in this post!
  3. Git really doesn’t have an equivalent to svn cp filename!

I spent a surprisingly long time reviewing the man pages and surfing the internet to confirm this, but git really assumes you’ll never want to copy a file with history. Here’s that Livejournal link I promised, where markpasc has similar complaints — from 2008, no less.

I was looking for a way to copy a file with history because I needed to break a single file into multiple pieces. In my Subversion days I would have done a svn cp old-file.php new-file.php. From there I could prune old-file.php and new-file.php down to what I needed while preserving the full history for all the code that remained.

Or perhaps I’m missing something?

4 thoughts on “There’s no ‘git cp filename’?

  1. I’m not sure I fully understand the use case, but the answer to most git questions like these is “just use branches”.

    It might be possible to do what you want with some clever use of git-cherrypick. But fundamentally, nobody does the equivalent of “svn cp” in git because it’s so easy to create branches, and undo changes (and, with a few small caveats, history never gets destroyed, and you can always go back in time.)

  2. Git tries to figure this out automatically. Sometimes this necessitates a couple commits: one to create a new file, one to make changes to that file. You can test the behavior with this series of commits (I just did):

    1. Make changes to file1
    2. Rename the file using `git mv file1 file2`
    3. View the history using `git log –follow file2`; see changes that happened to file1 (magic!)
    4. `cp file2 file3`, add and commit
    5. `git log –follow file3`, see the cp operation, plus everything from step #3

    Log output will include something like this, which is sure to get mangled as I leave this comment:

    commit 406d4460a229cfe6e2c3d1a3135a099ccd38f1b8
    Author: Adam Backstrom 
    Date:   Wed Nov 21 11:29:29 2012 -0500
    
        commit at Wed Nov 21 11:29:29 EST 2012
    
    diff --git a/file.php b/new.php
    similarity index 100%
    copy from file.php
    copy to new.php
    
  3. @Adam: I’d seen `git log –find-copies` and its friends, but my frustration is that I have to know to look for copies to find them, whereas the svn approach made that history very obvious.

    @Casey: the issue is that I want to split one file that’s grown too large into two and preserve the history in both.

    In short, I want to be able to do the equivalent of `svn log filename` that will tell me the full history of the file, including the file it was copied from and its history too.

Comments are closed.