You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git忽略Android项目assets文件夹,提交无法纳入仓库如何解决?

问题:Capacitor Android项目的assets文件夹无法提交至Bitbucket

使用Capacitor构建应用并添加Android平台后,android/app/src/main目录下自动生成了assets文件夹,但提交到Bitbucket仓库后该文件夹未显示。已检查本地.gitignore文件,未发现直接忽略该文件夹的规则。

本地Android Studio中的文件夹状态:

Android Studio中的assets文件夹

提交后Bitbucket中的仓库状态:

Bitbucket中缺失assets文件夹

本地.gitignore文件内容:

# Specifies intentionally untracked files to ignore when using Git
# http://git-scm.com/docs/gitignore

*~
*.sw[mnpcod]
.tmp
*.tmp
*.tmp.*
UserInterfaceState.xcuserstate
$RECYCLE.BIN/

*.log
log.txt


/.sourcemaps
/.versions
/coverage

# Ionic
/.ionic
/www
/platforms
/plugins

# Compiled output
/dist
/tmp
/out-tsc
/bazel-out

# Node
/node_modules
npm-debug.log
yarn-error.log

# IDEs and editors
.idea/
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-project
*.sublime-workspace

# Visual Studio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*


# Miscellaneous
/.angular
/.angular/cache
.sass-cache/
/connect.lock
/coverage
/libpeerconnection.log
testem.log
/typings

# System files
.DS_Store
Thumbs.db

解决方案

问题核心是Git默认不追踪空文件夹——你的.gitignore没有直接排除assets,但如果该文件夹内无任何文件,Git会自动忽略它,导致仓库中无法看到。

具体修复步骤:

  1. 同步Capacitor资源(若需要):
    Capacitor默认会将www目录的内容同步到assets/public,如果本地assets为空,先执行同步命令:

    npx cap sync android
    

    同步完成后,执行git add android/app/src/main/assets/并提交推送即可。

  2. 为空文件夹添加占位文件:
    如果assets确实需要保留为空,创建.gitkeep占位文件(Git会追踪该文件,从而保留整个文件夹):

    touch android/app/src/main/assets/.gitkeep
    

    然后提交:

    git add android/app/src/main/assets/.gitkeep
    git commit -m "Add .gitkeep to track assets folder"
    git push
    
  3. 排查文件被忽略的特殊情况:
    如果assets内有文件但未被追踪,用以下命令检查是否被.gitignore规则命中:

    git check-ignore -v android/app/src/main/assets/[目标文件名]
    

    若有命中,在.gitignore中添加例外规则,例如:

    !android/app/src/main/assets/your-target-file
    

内容的提问来源于stack exchange,提问作者Alexander García

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 15:13:27