Saturday, January 21, 2012

tell Git to do ignore your changes to some files

Normally Git is so powerful with its branching capabilities, that you seldom need this feature. But sometimes it can be handy if you can simply tell Git to "pretend" that you have not changed some files, even they have really been changed.

If you are using Git as a SVN client, like I do, you will know that Git refuses sync with SVN with local changes. In a large project, it can be that there is a common development environment with a common deployed application server so the client developers can connect and debug without worrying about setup their own instances. So it's also common that when you checkout codes out of the server, in my case it's a SVN repository, the config file contains URL pointing to that common server. If you are the server side developer, then you have to start your own instance and use a client to connect to it. Then you have to change the config file to point the URL to your instance and remember never commit the changes ,otherwise your colleague will kill you :)

There are a bunch of ways to overcome this. You can first only check in your changes without the config files, then stash the config files and then commit to SVN. Or you can create your own local branch with your config files, checkout and rebase the branch each time you want to debug with your config, which is what I'm doing now.

A new way I realized today while reading this post was, I can tell Git simply ignore the changes you have made to some files, with below command:
git update-index --assume-unchanged somefile.txt
this way Git will assume the file is never changed and will perform happily whatever command you want that can normally only be performed to a clean copy. So what happens that the file is changed in upstream repository? By manual, "Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually."

No comments:

Post a Comment