Chetan
Chetan

Reputation: 48069

Git - Refraining from pushing some local changes

While working on my project's user interface, I test it locally. Thus, I want to be able to uncomment some remote includes in the HTML head, work for a bit, and then push the changes without the uncomments, which are there just to make testing it locally faster. What's the best way to do this with git?

Upvotes: 1

Views: 291

Answers (3)

Matthew Rankin
Matthew Rankin

Reputation: 461237

What about using client-side hooks to automate uncommenting on post-checkout and commenting on pre-commit?

If the comments that you're changing aren't always the same, this may not be the best solution.

For info on Git's hooks, see Section 7.3 of the Pro Git book.

Upvotes: 0

Emil Sit
Emil Sit

Reputation: 23562

You could set up a separate branch off of your master that just has these lines uncommented, e.g.,

git checkout -b testing master
# edit foo/bar/baz.html
git commit -a -m "Enable local testing optimizations."

And then leave this branch alone. You would rarely have to touch it. Later you're doing some work you want to test, so you check in your feature ...

# hack hack hack
git commit -m "Feature complete."

...and then (temporarily) merge in your testing changes.

git merge testing

Assuming you're not modifying the headers, this will rarely cause any conflicts and should merge cleanly. You finish your testing, and then you can

git reset --hard HEAD^

to throw away the merge commit, leaving your tree where the HEAD points to your "Feature complete." commit.

Occasionally, you can rebase the testing branch against master, or re-work it to deal with any changes from that portion.


Another option is to maintain your changes simply in a .patch file. Then ...

# Feature complete!
# Apply testing code... 
git apply testing.patch
# Test...
git apply -R testing.patch
# Remove testing code

Same idea, but handled with a separate patch file. A little simpler, maybe allowing a little bit less discipline, but also maybe a bit riskier.


You might also want to write an update hook (see githooks(5)) to ensure that you never push a change that includes this testing patch.

Upvotes: 3

dgnorton
dgnorton

Reputation: 2247

Don't add the file to a commit. Add & commit only the files with changes you want to push. For example...

Uncomment remote includes in foo.html
edit file1.html
edit file2.html

git add file1.html
git add file2.html
git commit -m "added feature"
git push origin master

The changes made to foo.html will not be pushed.


If you want to blow away your local changes to a file, the use git checkout ...

git checkout path\to\foo.html

Upvotes: 0

Related Questions