Reputation: 1149
I have a project with two versions of my code, one "maintenance" and one "new" for a newer version.
I wanted to ignore all *.min.js files in my code in these branchs. Some .js files are in "new" but not in "maintenance". Eclipse generates .min.js everytime a .js file is edited.
The problem is, when i'm on branch "new" and switch to branch "maintenance", the minified js generated in new and that don't have their .js files on maintenance are still there.
Is there a way to tell git to delete these files when switching to maintenance ?
I would like to defined a merge strategy in gitattributes instead of ignoring .min.js files, but Eclipse doesn't handle gitattriutes.
Upvotes: 1
Views: 191
Reputation: 489083
The whole point of an untracked file is that Git does not know anything about it. Git therefore cannot know to remove it either.
Git does provide a general mechanism to let you run your own operations after each git checkout
, namely what it calls a post-checkout hook. (Whether your Eclipse IDE obeys this same protocol, I have no idea.) The post-checkout hook is simply any executable file1 named .git/hooks/post-checkout
. Git will run it, with some arguments as described in the githooks documentation, after each git checkout
.
Since git checkout
can be used for operations that are not change the branch name to which HEAD
is attached, you will need to test for whatever conditions you deem appropriate. For instance, you could check the third parameter, the one called flag
in the documentation. If it is set, the command was an operation that modified the commit or branch name to which HEAD
is bound. If HEAD
is now bound to a commit or branch in which some *.min.js
files should be removed if they exist, you can now remove them if they exist. If HEAD
is now bound to a commit or branch in which some *.min.js
files should be created if they do not exist, you can now create them if they do not exist.
Such a script might look like this, although you will need to fill in the missing parts:
#! /bin/sh
# Post-checkout hook to deal with `*.min.js` files.
# If flag is not 1, do nothing (and succeed).
if [ "$3" != 1 ]; then exit 0; fi
# Otherwise, determine whether there are *.min.js files that need to
# be created and/or removed. The code below is meant to be illustrative,
# not efficient.
for file in $(compute files that should be removed); do rm -f "$file"; done
for file in $(compute files that should be created); do create_minified "$file"; done
exit 0 # always report success, regardless of actual success
That last line is important: the Git documentation claims that the post-checkout hook cannot affect the outcome of git checkout
, but in fact, the exit status of the post-checkout hook sets the exit status of git checkout
itself. If your post-checkout hook reports a failure-status, git checkout
will report a failure-status, which may mislead larger automated systems. This could be good or bad, depending on your point of view, so while the last line is important, it's not necessarily correct for your particular application.
1An executable file, here, must be actually executable. That is, it not only needs to have the x
bit set (on a Unix-like system, use chmod +x
or chmod 755
or similar to set the executable bits), it must be directly executable via an execve
system call. For scripts on Unix-like systems, this means the script must begin with a line like #! /bin/sh
or #! /usr/bin/env bash
, where the path name after #!
is the path name of the interpreter. Python scripts may be made executable via #! /usr/bin/env python
or #! /usr/bin/env python3.6
or the like, depending on which Python interpreter you need to invoke.
Upvotes: 1
Reputation: 1244
After switching to new
branch, you can run the following command to make the working tree match the HEAD state.
git reset --hard && git clean -dfx
Upvotes: 0