Jump to content

Ignore the Package-Lock.JSON File in Git


Linux Hint

Recommended Posts

Whether you are just getting started with Git or a seasoned pro, one of the most fundamental features you will use is the “gitignore”.

It is common to have files and directories that contain sensitive information such as API keys, passwords, configuration files, etc. One of the most common file for web developers is the package-lock.json file.

Luckily, Git has the “gitignore” feature that allows us to tell Git not to include these files and directories into the repository.

In this tutorial, we will explore the fundamentals of the “gitignore” feature and how we can use it to exclude specific files and directories from being tracked by Git.

We will then focus on how to use the “gitignore” feature to ignore the infamous package-lock.json file.

What Is the Package-Lock.JSON File?

The package-lock,json file is a file that uses the JSON format to manage the dependencies in a Node.js project. The file is deterministic and consistent which makes it very useful and easy to manage the dependencies.

The file is automatically generated by the package manager such as npm and yarn once we install or update the packages in a given project.

Although it is useful in the project, it is redundant from the actual packages required to run the project that is defined in package.json. It is therefore non-essential to include in a repo.

Gitignore Feature

In Git, “gitignore” is a fundamental feature that allows us to configure the files and directories that are excluded from version control tracking.

It is simply a text file containing entries for the names of directories and files that we wish to exclude. We can also include patterns in the file to determine the files that should be excluded.

Creating a Gitignore File

Before we dive into the process of configuring the “gitignore” file, we need to ensure that we have the file present in the target repository.

Start by navigating to the root directory of the Git repository. For example, suppose we have a repository called “hello_world”.

We can create or navigate to the following directory:

$ cd ~/src/hello_world

In the root directory, create a new file called “.gitignore”. This is where we store all the entries for all files and directories that we wish the version control system to ignore.

$ touch .gitignore

Adding Entries to the Gitignore Files

Once we created the “gitignore” file, we can proceed and add the files and directories that we wish to include.

You can do this by opening the “gitignore” file with a text editor. Next, add the patterns to specify the files and directories to ignore.

Each pattern should be on a separate line. For example, suppose we are working within a Python-based repository and we want to exclude the Python-compiled bytecode and virtual environment directories.

We can add the entries as follows:

__pycache__/
venv/

This should ensure that Git does not track any “pycache” files and any file that is stored within the “venv” directory.

Ignoring the Package-Lock.JSON in Git

To tell Git to ignore the package-lock.json file, we can add the entry in the “gitignore” file as shown in the following example:

Start by editing the “.gitignore” file and add a pattern to ignore the “.env” files.

package-lock.json

This should force Git to ignore the specified file.

Removing the Already Included Files

In some cases, you may have already added the file before adding it to the “gitignore”. Therefore, it is prudent to remove them from the version control system.

We can do this using the “git rm” command as follows:

git rm --cached package-lock.json

This should remove the already included files from “git tracking”.

Conclusion

In this tutorial, we learned about one of the most common and powerful tool in Git called “git tracking” which helps us to prevent Git from tracking specific files.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...