【问题标题】:I have two .git directories for one project/folder, how do I rename a file in working tree?我有一个项目/文件夹的两个 .git 目录,如何重命名工作树中的文件?
【发布时间】:2020-05-02 02:27:04
【问题描述】:

出于某种原因,我关注https://stackoverflow.com/a/17313342/4336225 获取.gitone 和.gittwo。现在我需要在我的工作目录中重命名一个文件。传统上,我会使用

git mv application.py newApplication.py

但是我现在如何用两个 .git 来做呢?我希望他们两个都能正确跟踪名称更改。

【问题讨论】:

    标签: git rename mv


    【解决方案1】:

    如链接答案所示,您应该使用--git-dir 选项。

    git --git-dir=.gitone mv application.py newApplication.py


    编辑

    这是一个完整的演练

    
    $ # Create multiple git directories
    $ git init .
    Initialized empty Git repository in /tmp/so_git/.git/
    
    $ mv .git .gitone
    
    $ git init .
    Initialized empty Git repository in /tmp/so_git/.git/
    
    $ mv .git .gittwo
    
    $ # Add a file to both directories
    $ touch foo
    
    $ git --git-dir=.gitone add foo
    
    $ git --git-dir=.gitone commit -m "add foo"
    [master (root-commit) 0db71dc] add foo
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 foo
    
    $ git --git-dir=.gittwo add foo
    
    $ git --git-dir=.gittwo commit -m "add foo"
    [master (root-commit) 10205ba] add foo
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 foo
    
    $ # Move file in first git repo
    $ git --git-dir=.gitone mv foo bar
    
    $ git --git-dir=.gitone commit -m "foo -> bar"
    [master 20276ee] foo -> bar
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename foo => bar (100%)
    
    $ # File is marked as deleted in second repo while "bar" is untracked
    $ git --git-dir=.gittwo status
    On branch master
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        deleted:    foo
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        .gitone/
        .gittwo/
        bar
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    $ # Add "bar" to the tracked files
    $ git --git-dir=.gittwo add bar
    
    $ git --git-dir=.gittwo status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        new file:   bar
    
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        deleted:    foo
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        .gitone/
        .gittwo/
    
    $ # Explicitly delete foo
    $ git --git-dir=.gittwo rm foo
    rm 'foo'
    
    $ # Now git discovered the rename
    $ git --git-dir=.gittwo status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        renamed:    foo -> bar
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        .gitone/
        .gittwo/
    

    如果您想使用git mv 而不是git [add|rm],另一种解决方案是将foo 文件检出并强制将其移至栏。

    $ git --git-dir=.gitone commit -m "foo -> bar"
    [master 20276ee] foo -> bar
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename foo => bar (100%)
    
    $ git --git-dir=.gittwo checkout -- foo
    
    $ git --git-dir=.gittwo mv -f foo bar
    
    $ git --git-dir=.gittwo status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        renamed:    foo -> bar
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        .gitone/
        .gittwo/
    

    【讨论】:

    • 我做了一个测试,使用 git --git-dir=.gitone mv temp1a.memo temp1ab.memo 将 temp1a.memo 重命名为 temp1ab.memo。它适用于 gitone。但是在 gittwo 内部,当我提交时,它说“删除模式 100644 temp1a.memo;创建模式 100644 temp1ab.memo”。所以 gittwo 不知道我只是更改了文件名。相反,它认为我删除了 temp1a 并创建了 temp1ab。我怎样才能让它知道我刚刚重命名了?
    • @halfmoonhalf 更新了我的答案。顺便说一句,如果一个标记为deleted,另一个标记为created,则在将两者都添加到索引时,git应该会自动检测“重命名”操作。
    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 2015-04-23
    • 2015-01-04
    相关资源
    最近更新 更多