给Git仓库添加.gitignore:清理、删除、排除被Git误添加的临时文件

文章目录

    • 一、前言
    • 二、发现提交的临时文件
    • 三、去掉临时文件的方法
      • 3.1 添加.gitignore
      • 3.2 删除临时文件缓存
      • 3.3 添加后的效果

一、前言

最近维护代码过程中,发现某APP代码库里被提交了许多临时文件,而这些临时文件每次都会变化,所以导致每次修改提交时候,都会出行他们的身影,非常烦人。其原因,很明显是没有包含.gitignore文件,楼下,就介绍加上.gitignore的方法。

二、发现提交的临时文件

如下,发现许多临时文件目录,如 .idea 被提交到了版本库,导致每次git status很难分清自己修改的文件,所以需要将它们去掉。


Windows PowerShell
版权所有(C) Microsoft Corporation。保留所有权利。

安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows

PS C:\Users\66176\Documents\works\mt9652\all_app\mtvsetting> git status .
On branch master_liebao
Your branch is up to date with 'origin/master_liebao'.

Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   app/src/main/java/com/kingboard/vavatvsettings/activity/bluetooth/BluetoothDeviceManagerActivity.java
        modified:   app/src/main/java/com/kingboard/vavatvsettings/fragment/currency/MachineFragment.java
        modified:   app/src/main/res/values-zh-rCN/strings.xml
        modified:   app/src/main/res/values-zh-rTW/strings.xml
        modified:   app/src/main/res/values/strings.xml
        modified:   app/src/main/res/xml/menu_child_currency_machine.xml

Changes not staged for commit:
  (use "git add/rm ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   .gradle/6.1.1/executionHistory/executionHistory.bin
        modified:   .gradle/6.1.1/executionHistory/executionHistory.lock
        modified:   .gradle/6.1.1/fileHashes/fileHashes.bin
      ……  省略  ……
        modified:   .idea/modules/settingframework/VAVATvSettings.settingframework.main.iml
        modified:   .idea/modules/settingframework/VAVATvSettings.settingframework.unitTest.iml
        modified:   app/build.gradle
        modified:   local.properties

三、去掉临时文件的方法

3.1 添加.gitignore

文件 .gitignore内容如下 ,将需要排除的文件、文件夹添加进入

*.iml
.gradle
.idea
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
PS C:\Users\66176\Documents\works\mt9652\all_app\mtvsetting> git add .gitignore

3.2 删除临时文件缓存

如本案例,许多临时文件已经被提交到版本库,所以要将他们删除

PS C:\Users\66176\Documents\works\mt9652\all_app\mtvsetting> git rm -r --cached .gradle
PS C:\Users\66176\Documents\works\mt9652\all_app\mtvsetting> git rm -r --cached .idea
PS C:\Users\66176\Documents\works\mt9652\all_app\mtvsetting> git commit -m "add .gitignore"

可见删除打印/提交打印:

rm '.idea/.gitignore'
rm '.idea/.name'
……  省略 ……
rm '.idea/modules/settingframework/VAVATvSettings.settingframework.iml'
rm '.idea/modules/settingframework/VAVATvSettings.settingframework.main.iml'
rm '.idea/modules/settingframework/VAVATvSettings.settingframework.unitTest.iml'
rm '.idea/vcs.xml'
PS C:\Users\66176\Documents\works\mt9652\all_app\mtvsetting> git commit -m "add .gitignore"
[master_liebao d0ddbcc] add .gitignore
 112 files changed, 14 insertions(+), 2522 deletions(-)
 create mode 100644 .gitignore
……  省略 ……
 delete mode 100644 .idea/modules/settingframework/VAVATvSettings.settingframework.main.iml
 delete mode 100644 .idea/modules/settingframework/VAVATvSettings.settingframework.unitTest.iml
 delete mode 100644 .idea/vcs.xml
PS C:\Users\66176\Documents\works\mt9652\all_app\mtvsetting>

3.3 添加后的效果

PS C:\Users\66176\Documents\works\mt9652\all_app\mtvsetting> git status
On branch master_liebao
Your branch is ahead of 'origin/master_liebao' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   app/build.gradle
        modified:   gradle.properties

PS C:\Users\66176\Documents\works\mt9652\all_app\mtvsetting>

给Git仓库添加.gitignore:清理、删除、排除被Git误添加的临时文件_第1张图片

你可能感兴趣的:(安卓程序员日记,git,github)