This sounds to me like a strong case for a pre-commit hook. In your .git folder you should have a hooks folder. It may have some samples in there but they're ignored since they have a .sample extension. If you add a file called 'pre-commit' (no extension) that contains the script below, the script will run each time you make a commit. Don't forget to make the script executable by running "chmod ug+x pre-commit".
I've added some comments explaining what each line does, but essentially how you would use it is you would put '#start' above the code you do not wan't to be included in your git commits, and then '#end' below that code. You can use different start and end flags in the sed command, just make sure you're using the comment syntax for whatever language you're developing in so the compiler ignores them. When you eventually perform a commit, the following will happen:
- The code in the middle (including the #start and #end flags) will be removed
- The file will be commited without the code and flags
- The code and flags will then be returned to your local copy of the file
Since this is all contained in a pre-commit hook, this will all happen automatically each time you commit.
pre-commit
#!/usr/bin/env bash
echo "Running pre-commit hook..."
set -e
export PATH=$PATH:/usr/local/bin
exit_status=0
echo "Removing supercomments..."
# Get a list of files part of this commit
files=$(git diff --cached --name-status)
# Loop through each file
for f in $(echo $files | awk '{ print $2}')
do
# Create a temp copy of the file
cp ${f} ${f}.temp
# Remove all chunks between '#start' and '#end'
sed -i '/#start/,/#end/d' ${f}
# Add the file with chunks removed
git add ${f}
# Replace file with temp file containing code you didn't want to comm
it
cp ${f}.temp ${f}
done
echo "Hook completed!"
exit $exit_status
Example Usage:
test.txt
This is a file
It's really just a test file
#start
This code is only relevant to me
When I eventually commit this file...
...please don't inclue all of this!
#end
This part of the file is important,
make sure it's included in my next commit.
add & commit test.txt:
git add test.txt
#
git commit -m "A great commit message."
# Running pre-commit hook...
# Removing supercomments...
# Hook completed!
# [master commit_id_1234] A great commit message.
test.txt (in commit_id_1234)
This is a file
It's really just a test file
This part of the file is important,
make sure it's included in my next commit.
test.txt (local copy after the commit)
This is a file
It's really just a test file
#start
This code is only relevant to me
When I eventually commit this file...
...please don't inclue all of this!
#end
This part of the file is important,
make sure it's included in my next commit.
NOTE:
There are some nuances to your question to consider. It sounds like you have a set of code that you want to have in your files but only when compiling locally (you primarily develop and test your code locally I'm assuming), but then you want to be able to commit that development code and automatically have your "only-locally-relevant-code" removed before committing. While the above hook should do just that, keep in mind that the portion of "only-locally-relevant-code" is not being versioned since it's never hitting your remote repository when you eventually push. This may be obvious but just make sure you're OK with this. If something happens to your local copy, all of that code, even though it's only relevant to you, can be lost and can't be recovered by simply re-cloning the repository.