From time to time, there are files you don't want Git to check in to GitHub. There are a few ways to tell Git which files to ignore.

Create a local .gitignore 推薦使用!

If you create a file in your repository named .gitignore, Git uses it to determine 確定 which files and directories to ignore, before you make a commit.

.gitignore file should be committed into your repository, in order to share the ignore rules with any other users that clone the repository.

GitHub maintains 維護 an official list of recommended 推薦的官方列表 .gitignore files for many popular operating systems, environments, and languages in the github/gitignore public repository.

  1. In Terminal, navigate to the location of your Git repository.在終端(本地磁盤),導航到到Git庫所在位置
  2. Enter touch .gitignore to create a .gitignore file.創建文件

If you already have a file checked in, and you want to ignore it, Git will not ignore the file if you add a rule later. In those cases, you must untrack the file first, by running the following command in your terminal:

git rm --cached 

Create a global .gitignore 全局的

You can also create a global .gitignore file, which is a list of rules for ignoring files in every Git repositories on your computer 電腦中的所有庫. For example, you might create the file at ~/.gitignore_global and add some rules to it.

  1. Open Terminal.
  2. Run the following command in your terminal:
    git config --global core.excludesfile ~/.gitignore_global

Explicit repository excludes 明確的庫排除

If you don't want to create a .gitignore file to share with others,不想共享忽略文件(僅僅想在自己本地庫中忽略) you can create rules that are not committed with the repository 規則不會被提交到庫中. You can use this technique 技巧 for locally-generated files 本地生成的文件 that you don't expect other users to generate,不希望其他用戶生成 such as files created by your editor.

Use your favorite text editor to open the file called .git/info/exclude within the root of your Git repository. Any rule you add here will not be checked in, and will only ignore files for your local repository.

  1. In Terminal, navigate to the location of your Git repository.
  2. Using your favorite text editor, open the file .git/info/exclude.