quinta-feira, 30 de julho de 2020

Untracking a local file on GIT

To untrack files in local repository but keep at origin, as our scenario needed; to keep folder or files in git origin repository but untrack any local changes made in this folder, You can use “git update-index –assume-unchanged”. This will enable new developers to get all the files in the repository when cloning. The application can make all the changes it wants in the specified folder, but locally they won’t be committed an pushed to the origin. The only minor problem with this solution is that you have to do this on every new machine that clones the repo. But something positive that comes out of this is that when you need to update files in the untracked folder you can.

1. Preparing the repo

Make sure you add the folder with all the containing files(that you later want to untrack) to the repo. And then commit and push it to your origin. In this example my folder is named Library.






git add -- .
git commit -a -m 'adding library folder'
git push

2. Add this folder to .gitignore







/library/*





And then commit this update. If you need more information about how to use the .gitignore file you can look at git online documentation.
At this stage the folder library and its contents are still tracked by git. Any changes to a file in this folder will therefor be marked as changed still. Lets take care of that next.

3. Untrack files in local repository but keep at origin

Now run the following command to untrack the library folder and its contents:






git update-index --assume-unchanged library/*

Make a change to a file in this folder and git will assume it has not been changed, so you won’t commit anything in this folder.

4. Undo this change

Say you need to update a file in the library folder after a while. Then you need only one command to start track this folder again:






git update-index --no-assume-unchanged library/*

5. Make alias of these commands

If you like you can create aliases for these commands to something simpler:






git config --global alias.assume-unchanged 'update-index --assume-unchanged'
git config --global alias.assume-changed 'update-index --no-assume-unchanged'

This makes you able to run:






git assume-unchanged library/*

and:






git assume-changed library/*

Nenhum comentário:

Postar um comentário