Git:重命名分支中的目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1261515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:46:53  来源:igfitidea点击:

Git: Renaming a directory in a branch

gitdirectorybranchrename

提问by jmohr

I have a project using Git where I've branched off of master to rename a directory.

我有一个使用 Git 的项目,我从 master 分支出来重命名一个目录。

The rename in the branch works as expected. When I switch back to the master branch the directory has its original name, but there's also an empty directory tree with the name that I changed it to in the branch.

分支中的重命名按预期工作。当我切换回 master 分支时,该目录具有其原始名称,但还有一个空目录树,其名称是我在分支中更改的名称。

Is this the expected behavior? Am I missing a step?
Do I just need to delete these empty directory trees as they appear?

这是预期的行为吗?我错过了一步吗?
我是否只需要在这些空目录树出现时删除它们?

I know Git doesn't track empty directories and that may be a factor here.

我知道 Git 不跟踪空目录,这可能是这里的一个因素。

My current workflow is:

我目前的工作流程是:

# create and checkout a branch from master
/projects/demo (master)
$ git checkout -b rename_dir

# update paths in any affected files

# perform the rename
/projects/demo (rename_dir)
$ git mv old_dir new_dir

# add the modified files
/projects/demo (rename_dir)
$ git add -u

# commit the changes
/projects/demo (rename_dir)
$ git commit -m 'Rename old_dir to new_dir'

I get to this point and everything is as expected:

我到了这一点,一切都按预期进行:

# old_dir has been renamed new_dir
/projects/demo (rename_dir)
$ ls
new_dir

The issue comes when I switch back to master:

当我切换回主时出现问题:

/projects/demo (rename_dir)
$ git checkout master

# master contains old_dir as expected but it also
# includes the empty directory tree for new_dir
/projects/demo (master)
$ ls
old_dir new_dir

new_dir is an empty directory tree, so git won't track it - but it's ugly to have there.

new_dir 是一个空的目录树,所以 git 不会跟踪它 - 但在那里很难看。

采纳答案by Rob Di Marco

Yes, you can remove it. You can also use git clean -dto remove the directory.

是的,您可以删除它。也可以使用git clean -d删除目录。

回答by apenwarr

It's rather likely that you have hidden files in the new directory. "ls -a newdir"

您很可能在新目录中有隐藏文件。“ls -a 新目录”