After git clone, to make the code work I have to make some changes locally. So, anytime I have to push code I need to undo those changes before pushing. Is there any way to have some local changes that won't show up in git diff. I don't want to ignore complete files only some part of the code.
-
You can't partially ignore a file. You can, however, only _add_ parts of the file. – jhpratt Aug 21 '18 at 02:58
-
What *kind* of changes? There are good ways to handle most scenarios but no good way to handle broken processes, please elaborate on what kind of changes you have to do to make the code work locally. – Lasse V. Karlsen Aug 21 '18 at 12:23
4 Answers
Not sure if you can partially ignore a file
You can do an environment variable in a config file that is in the gitignore, then in your code do like
if ENV['some-environment-variable'] == 'some-value'
//do certain things
else if ENV['some-other-environment-variable'] == 'some-other-value'
//do other certain things
else
//do nothing
end
Would that work for your solution?

- 911
- 1
- 10
- 31
-
I don't want to have any residue in code pushed. So unfortunately this approach doesn't work for me. – animek sahu Aug 25 '18 at 00:20
You could interactively decide wich changes you want to commit. Use git add --patch
to iterate through the chunks of changed code and add what you need. Then you can push those changes and keep the local modifications local.

- 311
- 1
- 7
-
I want to have some permanent local changes in the code. So, I don't have to choose changes to push every time. @ElpieKay 's solution works for that. Thanks for the response. – animek sahu Aug 25 '18 at 00:21
Suppose your branch is foo
. Create a new branch bar
from it. Commit the pushable changes in foo
and the non-pushable in bar
.
Whenever you want to test if foo
's code can work, run git rebase foo bar
first, by which bar
will be checked out and updated, and then test the code of updated bar
. If it works, you can push foo
.
If foo
is updated with new commits later, run git rebase foo bar
again to update bar
.

- 27,194
- 6
- 32
- 53
I suggest splitting the file/code/class into separate files.
Then you have multiple options:
git update-index --assume-unchanged path/to/the/file
git stash
magic with Stash only one file out of multiple files that have changed with Git?- Add the file with changes you don't want to commit to git ignore.

- 31,798
- 8
- 86
- 126
-
I can't change the structure used by the master project. So, this doesn't work for me. Thanks for the response. – animek sahu Aug 25 '18 at 00:23