Git

Git配置Java开发环境的.gitignore文件

2020-01-01  本文已影响0人  handsomemao666

一、为什么要配置.gitignore?

在使用git管理java项目时,如果没有配置.gitignore,会把除源代码之外其它的一些无关文件也同步到代码仓库,会造成以下问题:

二、如何配置.gitignore?

作为Java开发者,一般使用Eclipse或Intelij IDEA作为开发工具,Maven作为项目管理工具,Git作为版本管理工具,这些工具在使用过程中都会产生一些文件,我们只要配置.gitignore文件就可以排除这些与源代码无关的文件。
github为开发者提供一个github/gitignore的仓库,里面包含了大部分语言和开发工具的gitignore文件模板,我们只要根据需要下载相关模板进行配置即可。

github/gitignore仓库地址:https://github.com/github/gitignore

由于Eclipse,Intelij IDEA和Maven每个项目都会用到,我们把这几个的配置作为Git的全局配置,这样就可以一劳永逸了,配置一次即可。而与项目相关的,例如Java,JS,Python等等其他语言的,只要在项目中按实际使用情况进行配置即可。

全局配置

  1. 创建~/.gitignore_global文件
$ touch ~/.gitignore_global
  1. 将上面的文件设置为git的全局core.excludesfile文件
$ git config--globalcore.excludesfile~/.gitignore_global
  1. 编辑~/.gitignore_global文件
$ vim ~/.gitignore_global
  1. 添加Eclipse规则,规则模板地址:
    https://github.com/github/gitignore/blob/master/Global/Eclipse.gitignore
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# CDT- autotools
.autotools

# Java annotation processor (APT)
.factorypath

# PDT-specific (PHP Development Tools)
.buildpath
  1. 配置Intelij IDEA规则,规则地址:
    https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn.  Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
  1. 配置Maven规则,规则地址:
    https://github.com/github/gitignore/blob/master/Maven.gitignore
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

项目配置

  1. 在Java项目中添加.gitignore文件
$ touch .gitignore
$ vim .gitignore

2、添加Java规则,规则地址:https://github.com/github/gitignore/blob/master/Java.gitignore

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

上一篇下一篇

猜你喜欢

热点阅读