简体   繁体   中英

How to ignore Git changed files?

I'm working with this setup at the moment. I have 3 changed files that contains mostly environment variables such as Jwt tokens, cloud hosting variables, etc. The changes are mostly of this kind: empty string --> my env variable

I won't ever include them in a commit because, obviously, each member of our team have them different from the other.

But it's pretty annoying in my workflow. I can't stash them permanently because I want that the changes in those 3 files affect my local application. I have to stash them and then stash pop to have them back, every single time I switch branch.

Somehow, I want Git to assume them as changed in every branch of my local repository without keeping them unstaged all the time... Have them untracked even if I changed the files.

Thank you.

You can unstage files from the index using

git reset HEAD -- path/to/file

Just like git add, you can unstage files recursively by directory and so forth, so to unstage everything at once, run this from the root directory of your repository:

git reset HEAD --.

Also, for future reference, the output of git status will tell you the commands you need to run to move files from one state to another.

You can tell git that it should "assume" that the file is unchanged.

git update-index --assume-unchanged some_file.txt

This just sets a flag that tells git

When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index.

You can unset the flag with

git update-index --no-assume-unchanged some_file.txt

EDIT

What's the different between this update-index options and simply unstaging them and keeping them unstaged? Anyway, thanks a lot.

When the flag is set the path will not show up in a git status as modified. Changes to that path will be ignored. If you unstage the changes the working copy will show up as modified and you can accidentially add and commit it. Eg

git init test_repo
cd test_repo
cat << EOF > .credentials
# credentials template file
user=
pass=
EOF
git add .credentials
git commit -m 'Credentials template file added'

When you now edit the .credentials file it will show up in git status as modified.

git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   .credentials

no changes added to commit (use "git add" and/or "git commit -a")

But if you tell git to assume that the file is unchanged it will be ignored.

git update-index --assume-unchanged .credentials
git status
On branch master
nothing to commit, working tree clean

PS: It would be better to add those files to .gitignore and never commit then, even templates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM