Ignacio Oroná
Ignacio Oroná

Reputation: 4399

Git push reminder/helper?

Sometimes I need (in order to make my development faster) harcode some stuff in my code. That can be credentials, or maybe just a hack to allow me to test certain feature. For many reasons, I never want to push this code to the main codebase or even the development branch. For some time I've been using the 'git-assume-unchaged' command but after merges, rebases, etc that can get mixed up and you might be pushing something you don't want. Is there a cool clean way to achieve this? maybe some command that warns me that I have to remember to checkout some file before pushing or something like that? any ideas?

Upvotes: 5

Views: 2152

Answers (3)

Peter Poulsen
Peter Poulsen

Reputation: 542

I know it is an old question, but it helped me solve my problem.

I ended up creating a pre-commit hook that looks for a string in committed files.

So when have to hard code a value that should not go to the repository, I insert a comment like this:

// GIT_BLOCK

I get a warning and a list of the files.

#!/bin/sh
if git rev-parse --verify HEAD >/dev/null 2>&1
then
        against=HEAD
else
        # Initial commit: diff against an empty tree object
        against=$(git hash-object -t tree /dev/null)
fi

exec 1>&2

if git diff --cached --name-only $against | xargs grep --color "GIT_BLOCK"
then
  echo Commit would modify one or more files that must not change.
  exit 1
else
  exit 0
fi

Upvotes: 1

Mark Adelsberger
Mark Adelsberger

Reputation: 45649

In many cases (like credentials) I would suggest thinking about ways in which you can avoid having to put those - even temporarily - into source-controlled files. (This might end up helping you in other ways as well.) But there may just be cases where temporary code is the most straightforward workflow for you, so ...

One solution would be to use a pre-commit hook. You could mark your temporary code with a comment (something like // xxxTEMPxxx in C-like languages maybe) and then reject any commit that finds your marker in the diff. See the example in .git/hooks/pre-commit.sample.

You can then keep your change in place as you work, committing or doing whatever, but if you git add the change then you'll be reminded that it's there and can unstage it. (If you're working on other code in the same file, you can use git add -p to selectively stage the changes you intend to commit while keeping your temp code out.)

Of course your temp changes still exist as untracked changes, which can get in the way of some operations; but in that case you can stash them. It's got to be better than trying to play with assume-unchanged at least.

Upvotes: 3

agonzalezmc
agonzalezmc

Reputation: 113

If you want see the files that you have modified you can use git gui. With this you can choose the modified files to push.

Upvotes: 0

Related Questions