Git ignore local changes

Greg Foster
Greg Foster
Graphite software engineer

Sometimes you may need to modify files but want these changes to remain local, without affecting the remote repository. This could include changes to configuration files, debug code, or other temporary adjustments that shouldn't be committed. Git provides several strategies for this exact situation. This guide will walk you through different methods to ignore local changes in Git, from ignoring changes in tracked files to preventing accidental commits of specific modifications.

There are multiple reasons why you might want to ignore local changes in Git:

  • Configuration files: Environment-specific settings in configuration files that should not be shared.
  • Debugging code: Temporary changes for debugging that are not meant to be part of the permanent codebase.
  • Local overrides: Local paths or settings specific to your development machine that should not affect others.

Here are several approaches to managing local changes without affecting the remote repository:

If the files you want to ignore are not yet tracked by Git (i.e., new files), you can simply add their paths to a .gitignore file. This file tells Git which files or patterns to ignore in all directories of the project.

Example .gitignore Entry:

Terminal
# Ignore all .env files
*.env

This entry will ignore any file with the .env extension in your project. This means that when running commands such as git add, these files will be ignored and left untouched.

For files that are already tracked by Git, modifications to them can be ignored using the update-index command. This is useful for files that are already part of the repository but require temporary or local changes that should not be pushed.

Command to ignore changes:

Terminal
git update-index --assume-unchanged <file-path>

Replace <file-path> with the path to the file you wish to ignore. This tells Git to temporarily ignore changes to the file until you decide otherwise.

Reverting back to track changes again:

When you want to start tracking changes again, use the following command:

Terminal
git update-index --no-assume-unchanged <file-path>

This will tell Git to once again start tracking changes to the file.

If you need to switch branches or perform other tasks and want to temporarily ignore changes without committing them, you can use git stash. This command saves your local modifications in the "git stash" and reverts the working directory to match the HEAD commit.

Stashing changes:

Terminal
git stash push -m "Message describing changes"

This command will temporarily save (or "stash") your current working directory changes, including staged and unstaged modifications, without committing them. By specifying the -m option followed by a message, you provide a description that helps identify the purpose or content of the stash for later reference.

This is particularly useful for saving your work progress in a clean state, allowing you to switch branches or perform other tasks without losing your uncommitted changes.

Reapplying Stashed Changes:

Terminal
git stash pop

Once you want to restore these changes back to your local working directory, you can run git stash pop and Git will attempt to merge these changes back into your local state.

If you want to keep changes but not commit them, you can use git add selectively or use the interactive staging process.

Adding specific files:

Terminal
git add specific_file1 specific_file2

Interactive adding:

Terminal
git add -p

This command allows you to selectively stage parts of files in "chunks". Running this command will commence an interactive sequence where Git will walk you through every chunk of changes across all of the modified files in your local state, allowing you to pick and choose which files and lines to commit.

In cases where you want to discard local changes and reset the state of files to match the remote repository:

Resetting a specific file:

Terminal
git checkout HEAD -- <file-path>

The command git checkout HEAD -- <file-path> is used in Git to discard changes made to a specific file in your working directory, resetting it back to the last committed state. By specifying HEAD, which points to the latest commit of the branch you're currently on, along with the file path, this command effectively reverts any local modifications to that file. This is particularly useful for undoing changes that you do not wish to keep, thereby restoring the file to its original state from the repository without affecting other files or the entire branch.

Resetting the entire branch:

Terminal
git reset --hard
git pull

The command git reset --hard is used in Git to reset the entire working directory and index (staging area) to the last commit of the current branch, discarding all local changes and uncommitted work. Following this, the git pull command fetches the latest changes from the remote repository and integrates them into your local branch, ensuring that your local copy is up-to-date with the remote source. This sequence is particularly useful when you need to completely discard local modifications and synchronize your branch with the remote repository, effectively aligning your local environment with the latest shared progress.

For further reading on local changes in your Git repository, see the official Git documentation.

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2