threads / discuss / 11490

rm and mv commands: should I use them?

Subject: rm and mv commands: should I use them?

## tl;dr

11 messages between Jan 6, 2008 and Jan 7, 2008.

replies: 10people: 8as markdown or json

Jon Hancock· Jan 6, 2008, 07:55 UTC · lore

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· Jan 6, 2008, 08:04 UTC · re: Jon Hancock · lore

Re: rm and mv commands: should I use them?

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· Jan 6, 2008, 08:08 UTC · re: Jon Hancock · lore

Re: rm and mv commands: should I use them?

[Jon Hancock <redstarling@gmail.com>]
Show 8 quoted lines
>
> 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· Jan 6, 2008, 08:08 UTC · re: Jon Hancock · lore

Re: rm and mv commands: should I use them?

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".

Show 5 quoted lines
> 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· Jan 6, 2008, 19:22 UTC · re: Jon Hancock · lore

Re: rm and mv commands: should I use them?

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· Jan 7, 2008, 01:55 UTC · re: Linus Torvalds · lore

Re: rm and mv commands: should I use them?

On Sun, Jan 06, 2008 at 11:22:50AM -0800, Linus Torvalds wrote:
Show 10 quoted lines
> > 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· Jan 7, 2008, 02:05 UTC · re: Jeff King · lore

Re: rm and mv commands: should I use them?

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.

Jeff King· Jan 7, 2008, 03:06 UTC · re: Junio C Hamano · lore

Re: rm and mv commands: should I use them?

On Sun, Jan 06, 2008 at 06:05:16PM -0800, Junio C Hamano wrote:
Show 14 quoted lines
> 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
Robin Rosenberg· Jan 7, 2008, 18:37 UTC · re: Jeff King · lore

Re: rm and mv commands: should I use them?

måndagen den 7 januari 2008 skrev Jeff King:
> On Sun, Jan 06, 2008 at 06:05:16PM -0800, Junio C Hamano wrote:
...
Show 8 quoted lines
> > "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
Jay Soffian· Jan 7, 2008, 03:05 UTC · re: Jon Hancock · lore

Re: rm and mv commands: should I use them?

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.
David Brown· Jan 7, 2008, 05:01 UTC · re: Jay Soffian · lore

Re: rm and mv commands: should I use them?

On Sun, Jan 06, 2008 at 10:05:34PM -0500, Jay Soffian wrote:
Show 10 quoted lines
>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

← back to recent threads