I have a folder let's say,
src/main/resources/basic_abc
For running my code locally, I need to rename it to
src/main/resources/basic
This is a change I want to perform each time I work on this repository. I don't want git to keep on tracking this folder and telling me that basic_abc was renamed to basic. How can I stop git from tracking this rename change?
Furthermore, if I run the command git status, it really gives a messy output since there is a large number of files in this folder.
How could I avoid it?
CodePudding user response:
I think this can be done by ignoring a directory "locally".
- create a new "basic" directory
- copy all contents in "basic_abc" into it
- goto
\.git\infoin your project directory, open theexcludefile. - add a new line saying
src/main/resources/basic. - execute
git statusagain, you will see git does not consider the new "basic" directory as an untracked file. But you need to synchronize its content with the "basic_abc" directory manually. As long as in the remote repository there is no directory or file named "basic" you are good to go.
The exclude file is for a "local git ignore". It has the same format as the .gitignore file but only works on your machine.
CodePudding user response:
Telling Git to ignore that basic_abc/ is gone
You can use git update-index --skip-worktree <file> to tell Git to ignore changes you've made locally to a file, so that git status and all other commands will just treat it as unchanged.
I tested, and this also works for a file that was removed. What I did not find is a way to do it for a whole directory, but it's not hard to script on the bash command line.
One file:
git update-index --skip-worktree basic_abc/file1
All the files in basic_abc/:
git status --porcelain |
grep " D src/main/resources/basic_abc" |
sed "s/^ D //" |
xargs git update-index --skip-worktree
Explanations:
git statusis run with--porcelainso that you have stable machine-readable output.- If needed, fix the grep expression so it finds exactly the files you want Git to pretend were not deleted/moved.
- The
sedcommand keeps just the filename. - And
xargscallsgit update-index --skip-worktreeon the results of that pipeline.
Telling Git to ignore that basic/ has appeared
As @EddieDeng said, you can use .git/info/exclude or .gitignore to accomplish this, see his answer.
Reverting the --skip-worktree operation
You didn't ask, but the next logical question, when you've made changes to files in basic_abc/ basic, will be how to commit them. I'm mentioning it here because once you've run that --skip-worktree loop, renaming basic back to basic_abc and running git status or git add won't work!
Fortunately, that question is already half answered here on SO: How to list files ignored with 'skip-worktree' shows how to list the skipped files, and this command can revert them to not being skipped:
git ls-files -v . |
grep "S src/main/resources/basic_abc" |
sed "s/^S //" |
xargs git update-index --no-skip-worktree
I'm not sure whether the git ls-files -v . output is as stable as the git status --porcelain output is, especially since the -t/-v/-f options are marked semi-deprecated in the manual, but this works at the moment, and I don't know how else to extract that list from Git.
CodePudding user response:
You could use git rm <folder> --cached - when needed you can simply add the folder again with git add <folder>. Note though that if you do a git commit -a before re-adding the folder, the removal will appear in that commit.
Important notice:
THE --cached argument IS IMPORTANT - WITHOUT THAT, git will DELETE the folder FROM YOUR HARD DISK.
