问题
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
回答1:
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
回答2:
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.
回答3:
There is a relatively easy answer for new files, but the files you've modified in some way are a bit harder.
For new files, you should just be able to use a .gitignore
. If you don't want to add the .gitignore
to the project's .gitignore
, then you can use git to add a config to your users and ignore the particular file. Let's say you want to ignore all files in the foo/
directory. You can do that via:
git config --global --add core.excludesfile $HOME/.gitignore
echo "/foo" >> $HOME/.gitignore
For the edited files, you will probably have to create a new file, and modify your start-ups to look for these new files correctly.
来源:https://stackoverflow.com/questions/45484019/git-preventing-a-particular-commit-from-ever-being-pushed