📜  git add gitignore - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:15:16.245000             🧑  作者: Mango

Git Add Gitignore - Shell/Bash

Introduction

As a programmer, you may have noticed that your Git repository contains many files that you'd prefer not to commit. These can be editor-specific files, caching files, compiled binaries, or other types of files that are not relevant to the repository's content. To prevent these files from being committed, you can use a ".gitignore" file, which specifies the files and directories that Git should ignore.

In this tutorial, we'll cover how to add a ".gitignore" file to your Git repository using the "git add" command in the Shell/Bash terminal.

Prerequisites
  • Git installed on your local machine
  • Basic understanding of the Shell/Bash terminal
Step-by-Step Guide
1. Create a ".gitignore" file

First, create a file in the root directory of your Git repository named ".gitignore". This file should contain a list of files and directories that Git should ignore. For example:

# Ignore editor-specific files
.idea/
.vscode/
*.swp

# Ignore compiled binaries
*.o
*.a
*.dll

These rules will ignore any files or directories starting with ".idea/" or ".vscode/", any files with the extension ".swp", and any files with the extensions ".o", ".a", or ".dll".

2. Use git add to add the ".gitignore" file to the repository

Once you've created the ".gitignore" file, you need to add it to the Git repository using the "git add" command:

git add .gitignore

This command adds the ".gitignore" file to the Git staging area, which means that it's ready to be committed in the next step. The "." before the filename indicates that we want to add all files with this name across all directories.

3. Commit your changes

Now that the ".gitignore" file is added to the Git staging area, we can commit it to the repository using the "git commit" command:

git commit -m "Add .gitignore file"

This command commits the changes to the repository along with a commit message explaining what changes were made. Make sure to use a descriptive commit message so that it's easy for other developers to understand what changes were made.

Conclusion

Adding a ".gitignore" file to your Git repository is essential to ensure that unnecessary files are not committed. By following the steps in this tutorial, you can easily add a ".gitignore" file to your repository using the "git add" command in the Shell/Bash terminal.