git - 我的一个项目/文件夹有两个 .git 目录,如何重命名工作树中的文件?

标签 git rename mv

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

git mv application.py newApplication.py

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

最佳答案

如链接答案所示,您应该使用 --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],另一个解决方案是 checkout 您的 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 目录,如何重命名工作树中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59749415/

相关文章:

unix - 使用 mv 命令 - 文件已删除?

linux - 在 Oracle Enterprise Linux 5 上安装 git 存储库 -- SSH 问题

git - 如何忽略 Magit 中的 gitignore?

git - 如何重命名 Git 本地和远程分支名称?

重命名 div 的 JQuery 代码

linux - Linux 中用于版本控制脚本的变量

git - 无论如何强制执行 git 标签一致性?

git - 为什么 heroku 在我刚下载时告诉我更新 CLI?

MySQL从列标题中删除字符

ubuntu - 如何在 Ubuntu 上覆盖文件? mv -f/--force 不起作用