Directory renames under Git

January 16th, 2008

Hmmm, I know Subversion doesn’t do it any better, but I was led to believe Git tracked directory renames. At least, it didn’t behave as I expected. Here’s a reproduction recipe:

$ . repro.sh /home/francois/src/recipe Initialized empty Git repository in .git/ Created initial commit a50ea59: initial version 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 lib/main.rb Switched to a new branch “perf” Created commit 73d3379: perf improvements 1 files changed, 1 insertions(+), 0 deletions(-) Created commit 01c0e0e: new file 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 lib/helper.rb Switched to branch “master” Created commit 66ac898: renamed 2 files changed, 1 insertions(+), 1 deletions(-) delete mode 100644 lib/main.rb create mode 100644 lib0/main.rb Switched to branch “perf” Merge made by recursive. {lib => lib0}/main.rb | 0 1 files changed, 0 insertions(+), 0 deletions(-) rename {lib => lib0}/main.rb (100%) . ./lib ./lib/helper.rb ./lib0 ./lib0/main.rb

In prose, here’s what I did:

  • Initial development was in lib/main.rb
  • Then, I branched master to create a perf branch
  • On the perf branch, I added lib/helper.rb, and modified lib/main.rb
  • Back on master, I renamed lib/ to lib0/
  • Back on perf, I merged from master

What I expected to see is a move of lib/helper.rb and lib/main.rb to lib0/. Oh well, I don’t rename directories that often anyway. It’s more of a caveat than anything else.

4 Responses to “Directory renames under Git”

  1. macournoyer Says:

    hey francois,

    Glad to see you’re using git.

    You forgot to add before your last commits

    git add .

    or

    git commit -a

  2. François Beausoleil Says:

    Marc, I did a “git mv”, and not a plain “mv”, so git is already aware of the change. Thanks for reading !

  3. Jean-Francois Couture Says:

    Git does not track file or directories. It only tracks the content of file (you cannot add an empty directory in git).

    It this case, it could be argued that git did the right thing. Just because you moved the content of one file to another directory does not mean that someone who created a file in another branch wants their file also moved.

    As you say, it’s a caveat to be aware of.

  4. Jean-Francois Couture Says:

    Sorry it this appears twice, but it seems my comment from yesterday did not get through.

    Git does not track files or directories. It only tracks the content of files (you cannot add an empty directory in git).

    It could be argued that what git did here is the correct behavior. Just because you moved the files from a directory does not mean a file created in a new branch should also move.

    As you say, it is something to be aware of when using git.

Leave a Reply

 

Search

A picture of me

I am François Beausoleil, a Ruby on Rails coder. During the day, I work on XLsuite. At night, I am interested many things. Read my biography

Tags

(4) (1) (1) (1) (1) (2) (1) (1) (1) (2) (2) (1) (2) (1) (3) (1) (2) (1) (1) (1) (1) (2) (14) (1) (1) (1) (1) (2) (1) (1) (2) (0) (1) (4) (1) (3) (1) (1) (1) (1) (1) (1) (0) (3) (2) (1) (2) (1) (3) (1) (5) (2) (10) (10) (11) (14) (2) (1) (3) (1) (1) (1) (1) (1) (0) (1) (2) (2) (2) (1) (1) (1) (4) (1) (3) (1) (4) (2) (2) (25) (2) (1) (1) (0) (1) (1) (1) (23) (25) (1) (1) (13) (1) (1) (1) (4) (5) (1) (1) (1) (4) (1) (2) (3) (4) (4) (1) (1) (1) (8) (3) (1) (5) (5) (2) (2) (2) (4) (8) (7) (1) (1) (1) (1) (2) (4) (1) (4) (12) (2) (1) (2) (4) (1) (1) (1) (2) (8) (2) (3) (2) (2) (1) (3) (1) (1)

Links

Projects I work on

Categories

Archives