Git

Git Ignore Local File Changes

Sometimes, while working on Git, developers want to make changes in the committed project source code files and execute them on the local machine. However, they do not want to commit these files with newly added changes. In this situation, Git allows them to temporarily stop tracking and ignore the changes of these files by utilizing the “git update-index –assume-unchanged <file-name>” command.

This write-up will explain the method of ignoring local file changes in Git.

How to Ignore Local File Changes in Git?

To ignore local file changes in Git, follow the provided steps:

  • Go to the required Git directory.
  • Generate a new file, track it and commit changes.
  • Update index value.
  • Add changes in the file.
  • Check Git status.

Look at the below-provided steps for practical demonstration!

Step 1: Redirect to Local Repository

First, move to the desired repository by utilizing the stated command:

$ cd "C:\Git\Repo3"

Step 2: Create New File

Then, use the “touch” command to create a new file in the repository:

$ touch demoFile.php

Step 3: Add File to Staging Area

Next, add a newly created file to the Git staging index through the following command:

$ git add demoFile.php

Step 4: Check Git Status

After that, run the below-provided command to check the current status of the working repository:

$ git status

The below output indicates that the newly created file exists in the staging area and needs to be committed:

Step 5: Commit Changes

To save all new changes to the Git repository, execute the “git commit” command:

$ git commit -m "demoFile added"

Step 6: Update Index Value

Now, use the “git update-index” command to ignore the most recent local changes by updating the index value:

$ git update-index --assume-unchanged demoFile.php

Here, the “–assume-unchanged” option is used to temporarily stop the tracking process and ignore newly added changes of the file.

According to the below-given screenshot, the index value has been set successfully, and modifications will not show up as a changed file in Git:

Now, let’s move ahead and make some changes in the selected file.

Step 7: Update File Content

To update the particular file, use the “echo” command:

$ echo "Hi! This is my Demo File" >> demoFile.php

Step 8: Add New Changes to Git Index

Then, move all the new changes to the Git staging area using the below-provided command:

$ git add .

Step 9: Verify New Changes

Lastly, check the Git repository status to ensure whether Git has ignored the changes in the file or not:

$ git status

Here, it can be observed that the working tree is clear, and there is nothing to commit which indicates that Git has ignored the file changes:

That’s all! We have compiled the easiest method of ignoring local file changes in Git.

Conclusion

To ignore local file changes in Git, first, switch to the required local repository and create a new file in the desired repository. Then, track it and commit changes. After that, execute the “git update-index –assume-unchanged <file-name>” command. Lastly, verify the ignored changes. This write-up explained the process of ignoring local file changes in Git.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.