如何告诉Git它是相同的目录,只是一个不同的名字 [英] How to tell Git that it's the same directory, just a different name

查看:77
本文介绍了如何告诉Git它是相同的目录,只是一个不同的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我输入

在学习Git过去几个障碍后,我遇到了一个新的挑战:重命名一个目录(本地,在工作目录中) code> git status ,它会列出旧目录名称中的所有文件(在新目录中存在具有相同确切文件名的文件)为已删除,而新目录名称为untracked。

有没有办法告诉Git它实际上是同一个目录,只是一个不同的名字?



这样,所有的文件都会被 git status 列为修改对象吗?



为了举例说明问题,这里是我从 git status 收到的输出,当我重命名整个目录时:



<$ p $ b $ git status
#在分支master上
#没有为commit提交更改:
#(使用git add / rm< file> ...更新将提交的内容)
#(使用git checkout - < file> ...放弃工作目录中的更改)

#deleted:old-dir-name / file1
#deleted:old-dir-name / file2
#deleted:old-dir-name / file3

#未写入文件:
#(使用git add< file> ...来包含将要提交的内容)

#new-dir-name /
没有更改添加到提交(使用git add和/或git commit -a)
〜/ sb / ws>


解决方案

这里的所有答案都非常有帮助,针对我所面对的特定场景的实际解决方案。我以实际步骤的形式提供对我有用的东西,希望能帮助其他人遇到相同的挑战:


  1. git mv< old-dir-name>
    >

  2. git status
    标记为重命名,而不是已删除) ; old-dir-name>< new-dir-name>
    (这是一个假提交,准备
    用于下一个$
  3. git branch
    git_mv_20110708_1500_DO_NOT_USE

    (带时间戳的假分支
    提醒我们只做了
    的解决方法)

  4. / bin / rm -Rf< new-dir-name>

  5. cp -Rp ... /< new-dir-name> 。(将
    复制到具有
    重命名名称的实际文件夹中)

  6. git status (大多数修改过的文件
    现在会被正确标记为
    已修改,而不是已删除。已重命名的文件将被标记为已删除的
    加入,
    尽管内容相同! - 对于这些文件,如果需要重命名跟踪,则重复步骤1-7)

  7. git add< untracked files>

  8. git commit -a -m最后重命名
    this文件夹

  9. git branch FOLDER_RENAMED

PS gitk 喜欢这个,但是 Emacs 仍然与重命名混淆。 :(

After getting past a few hurdles learning Git, I came across a new challenge: Renaming a directory (locally, in the working directory).

When I type git status, it lists all the files in the old directory name (that exist with the same exact filenames in the new directory) as deleted and the new directory name as "untracked".

Is there a way to tell Git that "it's actually the same directory, just a different name"?

So that all the files will be listed by git status as modified only?

To exemplify the problem, here is the output I receive from git status when I rename an entire directory:

git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    old-dir-name/file1
#   deleted:    old-dir-name/file2
#   deleted:    old-dir-name/file3
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   new-dir-name/
no changes added to commit (use "git add" and/or "git commit -a")
~/sb/ws>

解决方案

All answers here were very helpful on the way to the actual solution for the particular scenario I was facing. I am providing what worked for me in the form of actual steps, hopefully helping others encountering the same challenge:

  1. git mv <old-dir-name> <new-dir-name>
  2. git status (verify that all files marked renamed, not "deleted")
  3. git commit -a -m "git mv <old-dir-name> <new-dir-name>" (this is a "fake" commit, to prepare for the real rename in the next steps)
  4. git branch git_mv_20110708_1500_DO_NOT_USE ("fake" branch with timestamp reminding that we only did this as a workaround)
  5. /bin/rm -Rf <new-dir-name>
  6. cp -Rp .../<new-dir-name> . (copy over the actual folder with the renamed name)
  7. git status (most modified files will now be marked correctly as modified, not "deleted". Files that have been renamed will be marked as deleted and added, despite having the same content! -- repeated steps 1-7 for those files if rename tracking is needed for them too)
  8. git add <untracked files>
  9. git commit -a -m "finally renamed this folder"
  10. git branch FOLDER_RENAMED :)

P.S. gitk loves this, but Emacs is still confused with the renames. :(

这篇关于如何告诉Git它是相同的目录,只是一个不同的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆