ziraak
ziraak

Reputation: 131

Git commits keep local

I'm wondering if it is possible to make a commit for some changes that if I push, these commits do not get pushed with the other commits to the remote branch.

for example: I've got some local changes i need for testing purposes. Like some changes to the config files and some json files. These changes will only be needed on this local branch and should never get on the remote branch. So when I create a commit I'm always trying to be carefull not to stage those exact files and every time I switch branches I need to stash and pop these changes.

So what I'm wondering is if there is a way within Git to keep those changes on the local branch without having to stash/pop or unstage them.

Upvotes: 0

Views: 428

Answers (4)

Zoe Edwards
Zoe Edwards

Reputation: 13677

Consider using assume unchanged:

When this flag is specified, the object names recorded for the paths are not updated.

git-update-index from the docs.

git update-index --assume-unchanged app.config

That would stop app.config from being marked as changed.

temporarily ignoring files’ is probably also worth a read.

Upvotes: -1

termosa
termosa

Reputation: 701

I usually configure my projects to include files like main.config and then local.config if only it is exist and then merge them in a propper way. And those local files are .gitignore'd of course.

Depends on your architecture, the article on .git/info/exclude can be handy

Upvotes: 0

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391396

There are multiple ways to handle this but it depends on the extent of your local modifications.

Let's first assume you have some configuration files in your repository. These configuration files may contain things such as the local hostname, connection strings or similar for databases, etc.

In this situation the best way is to only commit template configuration files.

For instance, if the application needs a file named app.config to be present, but this file contains such things, you would instead commit a template file named app.config.template. This file would not contain actual hostnames, usernames, etc. but at most contain placeholders where you're expected to add this information.

Then, in your build script (or similar) you would have something that would look like this:

IF NOT EXIST app.config COPY app.config.template app.config

This would, if needed, make a fresh copy from the template to the real file, on build.

Additionally, you would instruct git to ignore the real file:

# in .gitignore
app.config

Now, sometimes this is not enough. It should be, but sometimes it isn't.

In this situation a different approach would use an extra branch for this. Let's assume you're developing on a branch named develop.

You would then first create a new branch for your local only modifications:

git branch develop-local develop

Then you would check out this branch, make all the necessary local modifications, and commit these:

git checkout develop-local
# hack hack hack
git add .
git commit -m "LOCAL modifications for test environment"

Then you would switch back to the real branch and work on a case:

git checkout develop
# work work work
git add .
git commit -m "Case #123, fixed bug in SQL parsing"

To test this, go back to your local branch, merge in the real branch and test:

git checkout develop-local
git merge develop
# test test test

Finally:

  • never push develop-local, only develop
  • never merge develop-local back into develop

Upvotes: 2

Zoe Edwards
Zoe Edwards

Reputation: 13677

Yes, but it’s a little cumbersome.

Read 7.6 Git Tools - Rewriting History from the docs.

Upvotes: 0

Related Questions