Git: renamed file manually, Git confused

前端 未结 5 1056
庸人自扰
庸人自扰 2020-12-23 09:21

I am using Git and manually renamed a file that I had added to the repository. Now, I have added the \"new\" file which I renamed to the repository but Git complains that th

相关标签:
5条回答
  • 2020-12-23 09:53

    If this error occurs, it probably means that the file you are trying to move is not originally tracked by git or the directory you are trying to move the file into is not a git tracked directory.

    0 讨论(0)
  • 2020-12-23 09:55

    Try this:

    mv new old
    git rm new
    git mv old new
    
    0 讨论(0)
  • 2020-12-23 10:06

    There is no problem. Simply git rm old or even git add -A and it will realize that it is a rename. Git will see the delete plus the add with same content as a rename.

    You don't need to undo, unstage, use git mv etc. git mv old new is only a short hand for mv old new; git rm old; git add new.

    0 讨论(0)
  • 2020-12-23 10:06

    This has to be automated. I never remember git until I commit, and I don't want to.

    #!/usr/bin/env python3
    
    # This `git rm` wrapper respects the `--cached` option (like `git rm`).
    
    import sys
    import subprocess
    
    if "--cached" in sys.argv:
        dest = sys.argv[-1]
        rest = sys.argv[1:-1]
        subprocess.check_call(["git", "add", dest])
        subprocess.check_call(["git", "rm"] + rest)
    else:
        subprocess.check_call(["git", "mv"] + sys.argv[1:])
    

    I don't want to "remember git mv", because git mv adds hidden state behind git diff that is implicitly added to the next commit, even if you explicitly commit something totally unrelated. I know, it's the so called "staged" state, but I don't like it. I like to commit without surprises.

    0 讨论(0)
  • 2020-12-23 10:09

    First, cancel your staged add for the manually moved file:

    $ git reset path/to/newfile
    $ mv path/to/newfile path/to/oldfile
    

    Then, use Git to move the file:

    $ git mv path/to/oldfile path/to/newfile
    

    Of course, if you already committed the manual move, you may want to reset to the revision before the move instead, and then simply git mv from there.

    0 讨论(0)
提交回复
热议问题