Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hg addremove in git

Tags:

git

mercurial

I really need this command in git

hg addremove 

So now look at scenario and see how mercurial would save me in here:

I had some kind of dir in here var/htdocs/static/static. I accidentally moved files to wrong location (with git-mv). anyway... now I moved some folders around by hand:

mv static static2 mv static2/static ./ 

Maybe I've changed some files in here too... and now everything is great... so now git doesn't know what happend? How he could trace a movement of files without notifying it like mercurial does with addremove.

For example now with mercurial I could do:

hg addremove --similarity 80% 

that's it - mercurial traced where files was moved by recognizing files content, and I saved my files history.

one lad in here have some trick for this:

git add . git ls-files --deleted | xargs git rm 

but it's like in CVS back then. you deleting files, you adding files. what about saving history of files??

like image 537
holms Avatar asked Nov 05 '11 15:11

holms


2 Answers

Basically, git always automatically does something sort of like "addremove" when it applies a commit to update the working tree: it looks at added and removed files in the commit and detects pairs of added and removed files that are probably renames (using a "amount of change" heuristic). So you don't have to use a special command; just do the adds and the removes, and git will later figure things out on its own.

In your example scenario, just tell git about the new files: git add static and about the deleted files: git add -u static, and you're ready to commit. If you do a git status before committing, you can see that it's already detected the rename.

[As another answer mentioned, git add -A is a nice shortcut that just adds everything in the working tree, including new and deleted files; the only reason to use the narrower commands above is if you want to avoid adding some other changes that happen to be in the working tree.]

like image 94
snogglethorpe Avatar answered Sep 30 '22 09:09

snogglethorpe


Git doesn't track renames via additional metadata so you can just do git add -A with the full assurance that you aren't missing any metadata.

While Git tracks "whole tree" history rather than file history, you can activate its rename and copy detection after the fact with commands like git log -M -C and git log --follow <file>.

like image 22
CB Bailey Avatar answered Sep 30 '22 10:09

CB Bailey