Recently I have started working with git version control system
and I’m loving it, especially the Git bash.
Usually in each and every project there are some files which we don’t
want to commit and push on live again and again. For example configuration
files and db files once committed don’t need to be modify again for live site.
In such cases to stop manual intervention git provide us the facility to ignore
files.
Let’s discuss:
We can ignore files in two ways.
1) Using .gitignore file :
If you don’t want some files to be added and want to ignore
them while uploading on
server, then you can use .gitignore file. For example you have a file named
bkp_config.php and while pushing you code live you don’t want to push that file.
In such cases you can use .gitignore file. There are again two scenarios.
Means first you have added the files,
committed them and then pushed them on master repository.
Like :
$ git add .
$ git commit -m 'Pushed Code live'
$ git push origin master
After some time you realized that you shouldn’t have done that. Then you added .gitignore file so that in future git can ignore this file to be committed.
.gitignore file
//content of .gitignore file
bkp_config.php
It will use its rules when looking at files to commit. Note
that git will not ignore a file that was already tracked
before a rule was added to this file to ignore it. In such a case the file must
be un-tracked, usually with
git
rm --cached < filename >
So
in this case we have to un-track the file first.
Run
$ git rm --cached 'bkp_config.php'
$ git
add .
$ git
commit –m ‘message’
$ git push origin master
(when you will commit changes and will push on server and other users have pulled the data then the file will get deleted from their clones request as well.)
Now your files are in un-tracked stage. Now go to point (b).
This file(.gitignore) can be committed into
the repository, thus sharing the rule list with any other users that clone the
repository.
Note that you can create a
.gitignore in any subpath to have its rules applied at that path. Sometimes an
empty .gitignore file is used as a placeholder for an empty path, for example
to force git to generate a log/ path for your development environment to use.
b)
Files are
un-tracked.
In this case just add the rule in .gitignore
file and commit the file. Push it.
$ echo .gitignore >> .gitignore.
Next time when you will run git add . command ignored files will not get added.
2) Ignore Versioned Files :
There are some files like db.config or config.php which
includes different data for local and live server. So such files are once added
to the live server and after that we rarely need those to be changed and they
would have different data on both local and live files. In that case we can use
following command so that whenever we make any changes in local config files we
should’t commit it knowingly or mistakenly.
Use
If you will make any changes in a file then it would not appear in git status list. So will not get added and committed.
List all files marked as --assume-unchanged
$ git ls-files -v | grep ^[a-z]
If you want to start tracking changes again run the following command:
That's it. Please send me your valuable feedback through comments. All type of suggestions are welcome.
Thanks!!!!!!!!! Enjoy Programming :)
Comments
Post a Comment
Thanks for your valuable comments.