# rm and mv commands: should I use them?

11 messages from 2008-01-06 to 2008-01-07. Participants: Jon Hancock, David Brown, Jeff King, Brian Swetland, Linus Torvalds, Junio C Hamano, Jay Soffian, Robin Rosenberg.
Thread: https://gitlist.dev/t/11490

## Jon Hancock, 2008-01-06 07:55

Subject: rm and mv commands: should I use them?
Message-ID: <379EDA94-A67B-483A-BC5F-E961DD52AD0C@gmail.com>
URL: https://gitlist.dev/e/379EDA94-A67B-483A-BC5F-E961DD52AD0C%40gmail.com

```
Hello list,
I'm fairly new to git and coming from svn and have tasted hg and bzr  
along the decision path.

Here's my newbie issue:

In reading Aristotle Pagaltzis's article http://plasmasturm.org/log/ 
487/  the author stresses that a major difference between git and  
something like bzr or svn is that git tracks content and not metadata  
(or at least less metadata); meaning is inferred through content and  
less through metadata.

The heart of Pagaltzis's argument copied here:
<COPY>
Among the systems I did look into, there are really just two  
contenders: git and Mercurial. All the other systems track metadata;  
git and hg just track content and infer the metadata.

By tracking metadata I mean that these systems keep a record of what  
steps were taken. “This file had its name changed.” “Those  
modifications came from that file in that branch.” “This file was  
copied from that file.” Tracking content alone means doing none of  
that. When you commit, the VCS just records what the tree looks like.  
It doesn’t care about how the tree got that way. When you ask it about  
two revisions, it looks at the tree beforehand and the tree  
afterwards, and figures out what happened inbetween. A file is not a  
unit that defines any sort of boundary in this view. The VCS always  
looks at entire trees; files have no individual identity separate from  
their trees at all.

As a consequence, whether you used VCS tools to manipulate your  
working copy or regular command line utilities or applied a patch or  
whatever is irrelevant. The resulting history is always the same.
</COPY>

So, do I need to use git's mv and rm commands?  Can't I just rename,  
add, and remove files using any means I like and then just ensure my  
"index" is staged properly when I do a commit?  Additionally, is there  
a simple procedure with git to say: "I want to version exactly what is  
in my working tree.  If I removed something or added something, just  
handle it".  This is sort of what "git add ." does, but "git add"  
doesn't handling things I removed or moved, correct?

thanks, Jon

```

## David Brown, 2008-01-06 08:04

Subject: Re: rm and mv commands: should I use them?
Message-ID: <20080106080415.GA15268@old.davidb.org>
URL: https://gitlist.dev/e/20080106080415.GA15268%40old.davidb.org
In-Reply-To: <379EDA94-A67B-483A-BC5F-E961DD52AD0C@gmail.com>

```
On Sun, Jan 06, 2008 at 03:55:22PM +0800, Jon Hancock wrote:

> So, do I need to use git's mv and rm commands?  Can't I just rename,  
> add, and remove files using any means I like and then just ensure my  
> "index" is staged properly when I do a commit?

Yes.  You can use either git-rm or 'git-commit -a' to remove files.  To
rename, you can use git-mv, or you can rename the file yourself, git-add
the new name, and git-rm the old name.

There is no metadata stored with git-mv and git-rm, they just update the
tree and the index.

Dave

```

## Brian Swetland, 2008-01-06 08:08

Subject: Re: rm and mv commands: should I use them?
Message-ID: <20080106080801.GA1483@bulgaria.corp.google.com>
URL: https://gitlist.dev/e/20080106080801.GA1483%40bulgaria.corp.google.com
In-Reply-To: <379EDA94-A67B-483A-BC5F-E961DD52AD0C@gmail.com>

```
[Jon Hancock <redstarling@gmail.com>]
>
> So, do I need to use git's mv and rm commands?  Can't I just rename, add, 
> and remove files using any means I like and then just ensure my "index" is 
> staged properly when I do a commit?  Additionally, is there a simple 
> procedure with git to say: "I want to version exactly what is in my working 
> tree.  If I removed something or added something, just handle it".  This is 
> sort of what "git add ." does, but "git add" doesn't handling things I 
> removed or moved, correct?

"git add ." only adds new or modified files to the index.  You can use
"git add -u ." to update the index to reflect any deleted files.

Brian

```

## Jeff King, 2008-01-06 08:08

Subject: Re: rm and mv commands: should I use them?
Message-ID: <20080106080825.GA9718@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20080106080825.GA9718%40coredump.intra.peff.net
In-Reply-To: <379EDA94-A67B-483A-BC5F-E961DD52AD0C@gmail.com>

```
On Sun, Jan 06, 2008 at 03:55:22PM +0800, Jon Hancock wrote:

> So, do I need to use git's mv and rm commands?  Can't I just rename, add, 
> and remove files using any means I like and then just ensure my "index" is 
> staged properly when I do a commit?

No, you don't need to use those commands. They really are just wrappers
that manipulate the working tree files and the index at the same time.
So instead of "git-mv a b" you can do "mv a b; git rm a; git add b".

> Additionally, is there a simple procedure with git to say: "I want to
> version exactly what is in my working tree.  If I removed something or
> added something, just handle it".  This is sort of what "git add ."
> does, but "git add" doesn't handling things I removed or moved,
> correct?

"git add ." will add the contents of any modified files to the index, as
well as add any untracked files (which may or may not be what you want).
It will not add removals. Try "git add -u" which updates all files that
git knows about (i.e., modifications and removals). You can also simply
use "git commit -a" which is the moral equivalent of "git add -u ; git
commit".

-Peff

```

## Linus Torvalds, 2008-01-06 19:22

Subject: Re: rm and mv commands: should I use them?
Message-ID: <alpine.LFD.1.00.0801061108320.2811@woody.linux-foundation.org>
URL: https://gitlist.dev/e/alpine.LFD.1.00.0801061108320.2811%40woody.linux-foundation.org
In-Reply-To: <379EDA94-A67B-483A-BC5F-E961DD52AD0C@gmail.com>

```


On Sun, 6 Jan 2008, Jon Hancock wrote:
> 
> So, do I need to use git's mv and rm commands?

Nope.

They are there only to

 (a) make people who are used to do "svn mv" not complain

 (b) simplify things a little teeny bit, by avoiding having to "git add" 
     the new file.

So if you do just a rename, you can do either

	git mv old-file new-file
	git commit

or you can do

	mv old-file new-file
	git add new-file
	git commit -a

and the *result* will be the same (the "git commit -a" is there to 
automatically pick up the fact that "old-file" went away: you could have 
done it with "git rm old-file" too, or "git add -u" or any number of 
other ways that update the index).

> Can't I just rename, add, and remove files using any means I like and 
> then just ensure my "index" is staged properly when I do a commit?

Absolutely. And depending on your workflow, that may well be the right 
thing to do. 

In particular, this all means that it's perfectly fine to make changes to 
a git repository using *any* non-git-aware tools, including things like 
graphical file managers etc.


> Additionally, is there a simple procedure with git to say: "I want to 
> version exactly what is in my working tree.  If I removed something or 
> added something, just handle it".  This is sort of what "git add ." 
> does, but "git add" doesn't handling things I removed or moved, correct?

You should be able to do that with either

	git add .
	git commit -a

or if you don't want to do a commit, you can do a

	git add -u
	git add .

where the "git add -u" will look at any files git already knows and update 
them (which includes removing them if they are gone), and then "git add ." 
will add any new files.

		Linus

```

## Jeff King, 2008-01-07 01:55

Subject: Re: rm and mv commands: should I use them?
Message-ID: <20080107015518.GD17748@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20080107015518.GD17748%40coredump.intra.peff.net
In-Reply-To: <alpine.LFD.1.00.0801061108320.2811@woody.linux-foundation.org>

```
On Sun, Jan 06, 2008 at 11:22:50AM -0800, Linus Torvalds wrote:

> > So, do I need to use git's mv and rm commands?
> 
> Nope.
> 
> They are there only to
> 
>  (a) make people who are used to do "svn mv" not complain
> 
>  (b) simplify things a little teeny bit, by avoiding having to "git add" 
>      the new file.

I haven't looked to see if this optimization is in place, but "git mv"
can also avoid having to recompute the sha1 of the file (which in most
cases doesn't matter, but on a large file with a cold cache, can make
the operation seem just as snappy as a regular "mv").

-Peff

```

## Junio C Hamano, 2008-01-07 02:05

Subject: Re: rm and mv commands: should I use them?
Message-ID: <7v63y62xwj.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v63y62xwj.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <20080107015518.GD17748@coredump.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> I haven't looked to see if this optimization is in place, but "git mv"
> can also avoid having to recompute the sha1 of the file (which in most
> cases doesn't matter, but on a large file with a cold cache, can make
> the operation seem just as snappy as a regular "mv").

Actually, I think I found a bug.

	$ git reset --hard
	$ echo >>Makefile
        $ git diff --numstat
        1	0	Makefile
        $ git mv Makefile makefile
        $ git diff
        $ git diff --cached -M --numstat
        1	0	Makefile => makefile

"git mv" should not have staged the change.  It should have
moved the index entry from Makefile to makefile and moved the
work tree files.

The fix for this would fall out from your "optimization" quite
naturally, but I think that might be a post 1.5.4 item.

```

## Jay Soffian, 2008-01-07 03:05

Subject: Re: rm and mv commands: should I use them?
Message-ID: <76718490801061905k30c1ac86r51a65165d47807d4@mail.gmail.com>
URL: https://gitlist.dev/e/76718490801061905k30c1ac86r51a65165d47807d4%40mail.gmail.com
In-Reply-To: <379EDA94-A67B-483A-BC5F-E961DD52AD0C@gmail.com>

```
On 1/6/08, Jon Hancock <redstarling@gmail.com> wrote:
> Additionally, is there
> a simple procedure with git to say: "I want to version exactly what is
> in my working tree.  If I removed something or added something, just
> handle it".

>From http://www-cs-students.stanford.edu/~blynn/gitmagic/ch05.html#id2553633
is the helpful hint:

$ git-ls-files -d -m -o -z | xargs -0 git-update-index --add --remove

I've got the following aliases in my .gitconfig:

alias.addremove=!git-ls-files -d -m -o -z -X .git/info/exclude -X
$(git-config core.excludesfile) --exclude-per-directory=.gitignore |
xargs -0 git-update-index --add --remove

alias.ls=!git-ls-files -d -m -o -v -X .git/info/exclude -X
$(git-config core.excludesfile) --exclude-per-directory=.gitignore

(In 1.5.4, you can replace the two -X's and the
--exclude-per-directory with --exclude-standard.)

j.

```

## Jeff King, 2008-01-07 03:06

Subject: Re: rm and mv commands: should I use them?
Message-ID: <20080107030642.GA18194@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20080107030642.GA18194%40coredump.intra.peff.net
In-Reply-To: <7v63y62xwj.fsf@gitster.siamese.dyndns.org>

```
On Sun, Jan 06, 2008 at 06:05:16PM -0800, Junio C Hamano wrote:

> Actually, I think I found a bug.
> 
> 	$ git reset --hard
> 	$ echo >>Makefile
>         $ git diff --numstat
>         1	0	Makefile
>         $ git mv Makefile makefile
>         $ git diff
>         $ git diff --cached -M --numstat
>         1	0	Makefile => makefile
> 
> "git mv" should not have staged the change.  It should have
> moved the index entry from Makefile to makefile and moved the
> work tree files.

I thought there was some discussion about this a few months ago,
concerning what exactly it should do, and that was how we arrived at the
current behavior. However, I can't seem to find it now. Maybe I dreamed
it.

-Peff

```

## David Brown, 2008-01-07 05:01

Subject: Re: rm and mv commands: should I use them?
Message-ID: <20080107050128.GA20484@old.davidb.org>
URL: https://gitlist.dev/e/20080107050128.GA20484%40old.davidb.org
In-Reply-To: <76718490801061905k30c1ac86r51a65165d47807d4@mail.gmail.com>

```
On Sun, Jan 06, 2008 at 10:05:34PM -0500, Jay Soffian wrote:
>On 1/6/08, Jon Hancock <redstarling@gmail.com> wrote:
>> Additionally, is there
>> a simple procedure with git to say: "I want to version exactly what is
>> in my working tree.  If I removed something or added something, just
>> handle it".
>
>>From http://www-cs-students.stanford.edu/~blynn/gitmagic/ch05.html#id2553633
>is the helpful hint:
>
>$ git-ls-files -d -m -o -z | xargs -0 git-update-index --add --remove

As long as your git is new enough to have git-add -u, you should be able to
do:

   $ git add .
   $ git add -u

and have the index match the working tree (keeping excludes out).

Dave

```

## Robin Rosenberg, 2008-01-07 18:37

Subject: Re: rm and mv commands: should I use them?
Message-ID: <200801071937.38976.robin.rosenberg.lists@dewire.com>
URL: https://gitlist.dev/e/200801071937.38976.robin.rosenberg.lists%40dewire.com
In-Reply-To: <20080107030642.GA18194@coredump.intra.peff.net>

```
måndagen den 7 januari 2008 skrev Jeff King:
> On Sun, Jan 06, 2008 at 06:05:16PM -0800, Junio C Hamano wrote:
...
> > "git mv" should not have staged the change.  It should have
> > moved the index entry from Makefile to makefile and moved the
> > work tree files.
> 
> I thought there was some discussion about this a few months ago,
> concerning what exactly it should do, and that was how we arrived at the
> current behavior. However, I can't seem to find it now. Maybe I dreamed
> it.

I had the same dream.

-- robin

```
