Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - preventing a particular commit from ever being pushed

I am using git with Source Tree.

I have made 3-4 changes to some files in a local copy of a branch. I will always need these changes locally but I want to never accidentally push it to remote. I can of course keep these changes uncommitted, but that way I risk losing the changes in case of an accidental discard or clean etc.

Is there any way I can mark some files for never being pushed.

Alternately is there any better way to achieve my purpose.

Some of these are file changes & some of these new files I have added to the local repository

like image 810
user93353 Avatar asked Oct 24 '25 07:10

user93353


2 Answers

I am sorry but someone has to tell you the truth.. You cannot do what you want.

You simply cannot prevent a commit from being pushed, unless it is in another, not pushed branch. But in this case of course you won't have both The Files and your dev branch in the same place.

The answer you do not want

For new files, as others said, you can ignore them.

For already tracked files you can tell git to ignore local modifications with git update-index --assume-unchanged <file>, but this won't prevent git from dropping changes with a checkout, for example.

What can you do

I think that the only option is to change how your application loads those files (I assume that they are something like configuration files).

For example have a configurable local folder where other property files can be found. This folder would be outside the repository, and maybe in another repository or backed up with other services

like image 141
Francesco Avatar answered Oct 25 '25 22:10

Francesco


If you want to commit the changes, I suggest doing so on a separate branch which you rebase onto master after every commit you do there. You would then work while having checked out your local only branch, then checkout master just before committing, then push master, checkout local only branch and do a rebase on master.

If you don't need to commit the changes : You could try to use the .git/info/exclude file, which works similar to .gitignore, but only works locally on your machine, and isn't commited.

From Gitdoc - Ignoring files

Explicit repository excludes

If you don't want to create a .gitignore file to share with others, you can create rules that are not committed with the repository. You can use this technique for locally-generated files that you don't expect other users to generate, such as files created by your editor.

Use your favorite text editor to open the file called .git/info/exclude within the root of your Git repository. Any rule you add here will not be checked in, and will only ignore files for your local repository.

In Terminal, navigate to the location of your Git repository.
Using your favorite text editor, open the file .git/info/exclude.
like image 39
sp1nakr Avatar answered Oct 25 '25 21:10

sp1nakr