0

Let's suppose we have a file which we are heavily changing. So in case of trial-and-error, we write something for testing. which we don't want to send to commit or even get noticed by git. like

function testThis(){
   console.log('helloworld');
}

I write this function, this is part of a very big file. during staging this change will be considered. Since there are many changes like this in a file. So it is possible that during hand picking which change to stage, this change might get staged by mistake.

So my question is: Is there any wrapper or something that is particular to git, in which if I wrap this code, then this change will not even be considered for staging, let alone for committing. something like

<.gitignore>
function testThis(){
   console.log('helloworld');
}
</.gitignore>
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
rajeshtva
  • 410
  • 6
  • 18
  • 4
    Why not just put this code into a separate file, link the files and do the requisite testing, and `.gitignore` the test file? Although you probably should be writing and committing formal tests of your codebase to your repository anyway – esqew Jul 19 '22 at 10:47
  • 2
    Does this answer your question? [Can git ignore a specific line?](https://stackoverflow.com/questions/6557467/can-git-ignore-a-specific-line) or [How to tell git to ignore individual lines, i.e. gitignore for specific lines of code?](https://stackoverflow.com/questions/16244969/how-to-tell-git-to-ignore-individual-lines-i-e-gitignore-for-specific-lines-of) – NullDev Jul 19 '22 at 10:48
  • @esqew. i ended up doing just that. – rajeshtva Jul 19 '22 at 19:25
  • @NullDev yes these looks promising, but there are risks doing it. also it looks like it will increase work load instead of decreasing it because i will have to gitattribute each individual line for that – rajeshtva Jul 19 '22 at 19:28

2 Answers2

1

gitignore allows you to ignore patterns/files. As a result, a very neat solution is to separate all the code you are developing for your own experiments and gitignore it. Let's suppose that your site is using myfile.js. However, somewhere you could implement a logic that does this:

  • if myfile-custom.js exists, then it uses it
  • otherwise it uses myfile.js
  • gitignore myfile-custom.js

This would mean that you could create a copy of myfile.js by the name of myfile-custom.js, starting with the exact content of myfile.js and doing your changes. If the logic above is implemented, then your local will pick up your custom code and use that, while you will never be in danger of committing it.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
1

You may choose a special keyword, which you can reject in a pre-commit hook.

For example : I use the word dontcommit, and I have the following section in my .git/hooks/pre-commit file :

dontcommit=0

# list files that are staged:
for file in $(git diff-index --cached --name-only HEAD --diff-filter=ACMR); do
    # look for the forbidden keyword in their content:
    if git grep --cached -q -i dontcommit "$file"; then
        dontcommit=1
        git grep --cached -n -i dontcommit "$file"
    fi
done

if test $dontcommit -eq 1; then
    echo " *** aborting commit, 'dontcommit' tag found in staged files"
    exit 1
fi

That way, when I add code and know that I shouldn't commit it at all, I just a add // dontcommit next to it :

function testThis(){
   // dontcommit
   console.log('helloworld');
}

To stage the other parts of such a file, I can still use partial staging with git add -p or a graphical tool to add only specific chunks (for example: the built in git gui utility)

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • does this method covers a block ?. i myself use git feature in vscode, i always go throgh each changes and file manually to stage it. bit tedious but worth it. – rajeshtva Jul 19 '22 at 19:24