Git开发实战(四)之删除文件、忽略操作(rm,gitignore)

一、删除本地文件 git rm/commit

      1.比如,我先查看当前目录有哪些文件,然后选择删除message文件,然后再查看当前状态,我们能发现git已经把我们删除的message文件记住了,git提示我们已经在本地代码中把message文件删除了,但是没有提交到本地仓库中,也就是说如果我们不提交,git是不承认把message文件删除的!我们发现看到提示,我们要删除文件其实是要用git rm 文件名 命令才能将删除这个改动提交到待提交列表中,我们再次执行git rm message命令,这个时候我们可以看到,我们已经将改动放到了待提交列表中,提示我们提交到本地仓库中去!

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ ls
message  README

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ rm message

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ ls
README

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ 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:    message

no changes added to commit (use "git add" and/or "git commit -a")

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git rm message
rm 'message'

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    message

      2.我们将删除改动,正式提交到本地仓库中去,然后查看本地提交的改动;

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git commit -m '删除了message文件'
[master d4d2af3] 删除了message文件
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 message

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git show d4d2af
commit d4d2af303b2f512b64336a8ed6568a64a48cf9cf
Author: kevinShaw <aibinxiao@126.com>
Date:   Thu Oct 26 09:48:40 2017 +0800

    删除了message文件

diff --git a/message b/message
deleted file mode 100644
index e69de29..0000000

      以上,就是删除本地文件的操作!

二、忽略文件.gitignore

      1.使用理由:因为在协同开发的时候,比如同时做后端开发,每个人的数据库的密码是不一样的,如果纳入版本控制,每次pull代码后都要重新修改(当然你也可以不提交这个文件),所以数据库的配置文件就不要纳入git的版本控制中去!

      2.比如,我们在原来的目录中创建一个database.yml数据库配置文件,如果不将它纳入忽略版本控制中,git就会提示我们这个文件没有提交到待提交列表,没有放到仓库中管理,就会很烦;

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ touch database.yml

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        database.yml

nothing added to commit but untracked files present (use "git add" to track)

      3.这个时候,我们就可以创建一个.gitignore文件(注意:这个文件要和.git文件在同一个目录中),来将我们不需要纳入版本控制的文件添加到这个文件中去,git就不会再提示你了。我们再次查看git status,发现之前的database.yml文件已经不再有提示信息了,现在只有.gitignore文件的提示信息;

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ vim .gitignore

我们在编辑.gitignore中添加*.yml,就表示所有的以.yml结尾的文件都将不再纳入版本控制中

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore

nothing added to commit but untracked files present (use "git add" to track)

      4.我们提交.gitignore文件

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git add .gitignore
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory.

aibin@XiaoAibin MINGW64 ~/Desktop/test_git (master)
$ git commit -m '忽略.yml的配置文件'
[master 04c9dd8] 忽略.yml的配置文件
 1 file changed, 1 insertion(+)
 create mode 100644 .gitignore

      以上就是.gitignore文件的操作,可以根据自己需要,配置需要忽略的一些文件!

 

本文为原创文章,如果对你有一点点的帮助,别忘了点赞哦!比心!如需转载,请注明出处,谢谢!

 

转载于:https://my.oschina.net/aibinxiao/blog/1556470

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值