跳至主要內容

How to exclude the folder but include the file inside the folder in .gitignore

This happened when I was coding for a backend server with Springbok framework. I want to upload gradlew and it’s related files in order to make my co-workers’ life easier.

In the official gradle docs says,

To make the Wrapper files available to other developers and execution environments you’ll need to check them into version control. All Wrapper files including the JAR file are very small in size. Adding the JAR file to version control is expected. Some organizations do not allow projects to submit binary files to version control. At the moment there are no alternative options to the approach.

Which means, I need to add gradlew, gradlew.bat, build/wrapper/gradle-wrapper.jar and build/wrapper/gradle-wrapper.properties into my git commit.

However, somehow, I saw there are some more files/folders in build folder. To avoid any potential issues, I just think that’s a good idea, just exclude build/wrapper/gradle-wrapper.jar and build/wrapper/gradle-wrapper.properties from .gitignore.

Just tried to write it down, but I knew there must be a lot of answers on the internet or you can force add to git commit, but whatever…

Basically, it’s simple but has a trick. Let’s see the example below.

# To ignore file, we can just list out the folder or file.
gradle/
.DS_Store
*.jar

# To exclude folder/file from ignore, you may add ! to the beginning
!**/src/main/**

Then, let’s get on it!

# To ignore folder but exclude any specific file like what I need in my description.

# Wrong example: 
gradle/*
!build/wrapper/gradle-wrapper.properties
!build/wrapper/gradle-wrapper.jar

# The trick is... You need to list out every layer til the file you need.

# Right example:
gradle/*
!gradle/wrapper
gradle/wrapper/*
!gradle/wrapper/gradle-wrapper.jar
!gradle/wrapper/gradle-wrapper.properties
分類:Git

搶先發佈留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

由 Compete Themes 設計的 Author 佈景主題