GIT, a source code management tool is famous among developers. Often times, developers miss to realise or understand .gitignore file in GIT (a most popular open source repository tool).
This file helps to avoid unrelated files getting into the project
repository. As a newbie to GIT, developers miss to add .gitignore and
hence we get to see unwanted files or binaries added in the project
repository. This may sometimes lead to issues for other developers.
So, how do we remove a file from GIT history that should not have been added in the first place? Git has numerous commands of which git filter-branch can help us in this scenario.
For
example:
.iml files are Intellij editor files that will be created for
each module. Lets suppose a developer (newbie) added this .iml file in project repository unknowingly. An other developer imports project modules in
his/her IntelliJ editor. Then the latter will see *.iml as modified
file. But this should not have been considered for project repository. So Lets
help this other developer to remove this *.iml file using git
filter-branch command.
git filter-branch --index-filter "git rm --cached --ignore-unmatch *.iml" HEAD
On
executing the above command, git finds all the commits and remove *.iml
files and rewrite the history. This will ensure *.iml file removed from
GIT history at all. A word of caution, for larger projects, this
command may take time to complete as it has to go through all the
commits in the branch mentioned (HEAD). After the successful
execution of the command, the other developer can add *.iml in
.gitignore file so it never gets considered for Project Repository
Tracking.
For more on this command, please visit git-filter-branch
Caveat:
If the file can be removed using single commit, please refrain from
using the above command, and use the normal commit to achieve what you
need.
Comments
Post a Comment