hg addremove in git

10,142

Solution 1

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

Solution 2

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

Solution 3

Short answer. The command most similar to hg addremove in git is:

git add -A

Oooorr...

git add --all
Share:
10,142
holms
Author by

holms

Just a DevOps, who's passionate about scaling and automation. Who doesn't want to do less job anyway? DevOps practices brought a new way to decrease maintenance for the whole company, which is basically increase of profit up to 80% in some cases! My background is mostly Python with HA web services. Currently working with Kubernetes and Terraform.

Updated on June 15, 2022

Comments

  • holms
    holms about 2 years

    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??