设为首页 收藏本站
查看: 933|回复: 0

[经验分享] git rm

[复制链接]

尚未签到

发表于 2018-9-18 11:21:16 | 显示全部楼层 |阅读模式
转载自:http://hubingforever.blog.163.com/blog/static/17104057920123174954666/  本文翻译整理自:http://web.mit.edu/~mkgray/project/silk/root/afs/sipb/project/git/git-doc/git-rm.html
  在git中我们可以通过git rm命令把一个文件删除,并把它从git的仓库管理系统中移除。但是注意最后要执行git commit才真正提交到git仓库
  示例1
  git rm 1.txt
  删除1.txt文件,并把它从git的仓库管理系统中移除。
  示例2
  git rm -r myFolder
  删除文件夹myFolder,并把它从git的仓库管理系统中移除。
  示例3
  $ git add 10.txt
  $ git add -i
  staged     unstaged path
  1:        +0/-0      nothing 10.txt
  2:        +0/-0      nothing branch/t.txt
  3:        +0/-0      nothing branch/t2.txt
  *** Commands ***
  1: tatus     2: pdate     3: [r]evert     4: [a]dd untracked
  5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elp
  What now> 7
  Bye.
  $ git rm --cached 10.txt
  rm '10.txt'
  $ ls
  10.txt  2  3.txt  5.txt  readme.txt
  $ git add -i
  staged     unstaged path
  1:        +0/-0      nothing branch/t.txt
  2:        +0/-0      nothing branch/t2.txt
  *** Commands ***
  1: tatus     2: pdate     3: [r]evert     4: [a]dd untracked
  5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elp
  What now>
  在通过 git add 10.txt 命令把文件10,txt添加到索引库中后,又通过 git rm --cached 10.txt 把文件10.txt从git的索引库中移除,但是对文件10.txt本身并不进行任何操作。
  另外对于已经被git rm删除掉(还没被提交)的文件或目录,如果想取消其操作的话,可以首先通过git add -i的子命令revert从索引库中把它们剔除,然后用git checkout 命令来达到取消的目录
  关于git add请参考《git add详解》
  关于git checkout请参考《》
  语法
  git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] …
  DESCRIPTION

  Remove files from the index, or from the working tree and the index. git rm will not remove a file from just your working directory. (There is no option to remove a file only from the working tree and yet keep it in the index; use /bin/rm if you want to do that.) The files being removed have to be>  OPTIONS
  …
  Files to remove. Fileglobs (e.g. *.c) can be given to remove all matching files. If you want git to expand file glob characters, you may need to shell-escape them. A leading directory name (e.g. dir to remove dir/file1 and dir/file2) can be given to remove all files in the directory, and recursively all sub-directories, but this requires the -r option to be explicitly given.
  -f
  --force
  Override the up-to-date check.
  -n
  --dry-run
  Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.
  -r
  Allow recursive removal when a leading directory name is given.
  --
  This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for command-line options).
  --cached
  Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
  --ignore-unmatch
  Exit with a zero status even if no files matched.
  -q
  --quiet
  git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output.
  DISCUSSION
  The  list given to the command can be exact pathnames, file glob patterns, or leading directory names. The command removes only the paths that are known to git. Giving the name of a file that you have not told git about does not remove that file.
  File globbing matches across directory boundaries. Thus, given two directories d and d2, there is a difference between using git rm 'd*' and git rm 'd/*', as the former will also remove all of directory d2.
  REMOVING FILES THAT HAVE DISAPPEARED FROM THE FILESYSTEM
  There is no option for git rm to remove from the index only the paths that have disappeared from the filesystem. However, depending on the use case, there are several ways that can be done.
  Using “git commit -a”
  If you intend that your next commit should record all modifications of tracked files in the working tree and record all removals of files that have been removed from the working tree with rm (as opposed to git rm), use git commit -a, as it will automatically notice and record all removals. You can also have a similar effect without committing by using git add -u.
  Using “git add -A”
  When accepting a new code drop for a vendor branch, you probably want to record both the removal of paths and additions of new paths as well as modifications of existing paths.
  Typically you would first remove all tracked files from the working tree using this command:
  git ls-files -z | xargs -0 rm -f

  and then untar the new code in the working tree.>  After that, the easiest way to record all removals, additions, and modifications in the working tree is:
  git add -A
  See git-add(1).
  Other ways
  If all you really want to do is to remove from the index the files that are no longer present in the working tree (perhaps because your working tree is dirty so that you cannot use git commit -a), use the following command:
  git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached
  EXAMPLES
  git rm Documentation/\*.txt
  Removes all *.txt files from the index that are under the Documentation directory and any of its subdirectories.
  Note that the asterisk * is quoted from the shell in this example; this lets git, and not the shell, expand the pathnames of files and subdirectories under the Documentation/ directory.
  git rm -f git-*.sh
  Because this example lets the shell expand the asterisk (i.e. you are listing the files explicitly), it does not remove subdir/git-foo.sh.


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-589776-1-1.html 上篇帖子: 12个git实战建议和技巧 下篇帖子: Git 常用命令速查表(三)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表