Skip to content

Instantly share code, notes, and snippets.

@shreewatsa
Last active February 23, 2023 04:12
Show Gist options
  • Save shreewatsa/8835c00ae65588eb9cbfcac804612b1c to your computer and use it in GitHub Desktop.
Save shreewatsa/8835c00ae65588eb9cbfcac804612b1c to your computer and use it in GitHub Desktop.
Untrack files in git

Untracking Files in Git:

.gitignore is the primary way to untrack files and folders. But, what if you want to do the same without using .gitignore file ?

  1. Ignoring untracked files ie files that are new or has not been added to git yet.

Note: the .git/info/exclude file doesnot get uploaded to remote.

$ touch my_local_file.txt;  # Lets say, you don't want this file to be tracked by git.
$ git status -s;            # You can see that git detects this file, but you don't want this to happen.
$ cd .git; 
$ echo "my_local_file.txt" >> info/exclude;	 
# Note: you can add contents here just like you do in .gitignore file.

$ git status -s;            # Now, the file doesnot show up here.
$ git ls-files --others;    # To verify, you should be able to see your file here. The flag --others shows only untracked files.

# In case, you want to version this file, just remove the entry in .git/info/exclude.
  1. Ignoring changes in files that are already tracked by git.
$ git update-index --assume-unchanged app/config/local/database.php;

Now on, changes in database.php are not tracked anymore by git. So, I can update it to my liking without fear of getting uploaded to remote.

Extras:

$ git ls-files --others --exclude-from=.git/info/exclude;  # To show the files ignored by .gitignore only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment