git/list[1] front-page[2] threads[3] people[4] search[5] about
 

How to keep log history when renaming and changing simultaneously

From
Urs Thuermann <urs@isnogud.escape.de>
Date
Apr 17, 2017, 11:36 UTC
Message-ID
<ygfpogbb711.fsf@tehran.isnogud.escape.de>

Sometimes I need to rename and change a file in one commit. One example would be a file foo.h that begins with

    #ifndef FOO_H
    #define FOO_H

which should be renamed bar.h and have the FOO_H changed to BAR_H. In subversion I would

    svn mv foo.h bar.h; vi bar.h; svn ci
and then a
    svn log bar.h
also shows the history of that file when it was named foo.h.
This does not work in git.  After committing,
    git mv foo.h bar.h; vi bar.h; git commit -a
the command
    git log --follow bar.h

shows only the history of the file when it was named bar.h, but not its life as foo.h.

The only workaround I found was to do the rename and the change in two separate commits, so that git sees the same name and the same SHA hash which allows it to follow the files' history. This can be a problem when the intermediate version with either only the change or only the rename doesn't compile correctly.

Is there a better way to do this in git?

A similar problem is splitting a file into two files. With subversion I'd do

    printf "void foo() {}\nint main() { foo(); }\n" > prog.c
    svn add prog.c; svn ci -m "Add prog.c"
Then I would split
    svn cp prog.c foo.c; svn cp prog.c main.c; svn rm prog.c
    printf "2d\nwq\n" | ed foo.c; printf "1d\nwq\n" | ed main.c
    svn ci -m "Split prog.c"; svn up
and I get
    $ svn log -v
    ------------------------------------------------------------------------
    r2 | urs | 2017-04-17 10:03:06 +0200 (Mon, 17 Apr 2017) | 1 line
    Changed paths:
       A /foo.c (from /prog.c:1)
       A /main.c (from /prog.c:1)
       D /prog.c
    
    Split prog.c
    ------------------------------------------------------------------------
    r1 | urs | 2017-04-17 10:02:51 +0200 (Mon, 17 Apr 2017) | 1 line
    Changed paths:
       A /prog.c
    
    Add prog.c
    ------------------------------------------------------------------------

And I can also see this when I run svn log on one of both files. How could I do this in git?

urs
Next: Duy Nguyen
Message 1 of 6 in “How to keep log history when renaming and changing simultaneously”
  1. Urs ThuermannApr 17, 2017
  2. Duy NguyenApr 17, 2017
  3. Igor DjordjevicApr 17, 2017
  4. Urs ThuermannApr 17, 2017
  5. Jacob KellerApr 18, 2017
  6. Junio C HamanoApr 18, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.