Tuesday, April 5, 2016

Ignoring in GIT

Let's say we have an eclipse project.
And we have a file called MyFile.java in that project. We want to make sure that this file must not go to a remote repository when we push this to GIT (version control system)

The basic thing we can do is to add this file name in .gitignore file. If your file has no changes (not modified or newly added) then this will work at first time. But assume you added this file to .gitignore after you did some changes to the file. (i.e. when you enter git status, the file will be shown in the modified section). The GIT might be still tracking the file.

What can you do, there are several cases.

case 1 : you don't need this file to be on the remote repository.
git rm --cached MyFile.java
this will remove the file from git index but it won’t do anything with your working copy. It won’t be a part of the git repository anymore. And the next time you push to a remote repository this file won’t be pushed and, if it is already existed on the remote repository, it will be DELETED.

case 2 : You just changed the file because it has some local dependency, such as may be some configurations related to my local IDE or machine. Then you cannot delete this file from the remote repository. What you want is to keep the changed file in the local, but doesn't need to be tracked.

But you can tell the GIT to ignore tracking it's changes.
git update-index --assume-unchanged MyFile.java

//to revert the effect

git update-index --no-assume-unchanged MyFile.java
You can use     git ls-files -v     to view the files. And if the first letter is in lower case then those are the files which --assume-unchanged.
To print just the files that are --assume-unchanged use:
git ls-files -v | grep '^[[:lower:]]'

you can put this to .gitconfig (or .git/config in the repository, or the gloabal one(~/.gitconfig to get the location)) and use this directly.
Edit your .gitconfig file to add this snippet:

[alias]
    ignored = !git ls-files -v | grep "^[[:lower:]]"
now typing git ignored will print the same out-put


References

1) https://jlordiales.wordpress.com/2012/12/28/when-git-ignores-your-gitignore/ 2) http://stackoverflow.com/questions/17195861/undo-git-update-index-assume-unchanged-file 3) https://git.wiki.kernel.org/index.php/Aliases 4) http://stackoverflow.com/questions/2363197/can-i-get-a-list-of-files-marked-assume-unchanged 5) Making git “forget” about a file that was tracked but is now in .gitignore

0 comments:

Post a Comment