Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Git to keep different versions of a file locally vs. in the master repository

I have a PHP config file i'd like to manipulate locally but ignore those changes during git commits to my master repository. I had a .gitignore file which used to ignore this PHP file but bad things happen and now the config.php file is no longer being ignored and I can't remember how to re-ignore it.

I know people on SO say to use git rm --cached <filename> but I can't for the life of me figure out how not to make git rm... keep deleting my config.php file.

I'm wondering if someone can list how to ignore my config.php such that I can keep editing it locally but these changes don't get added to the repo.

here's the entire contents of my .gitignore:

application/config.php

here's some php code in my config.php I'd like to keep local and NOT go in my master repo:

$config['base_url'] = "http://localhost/";
//$config['base_url'] = "http://mysite.com/"; // this is the code and NOT
// "http://localhost" that i'd like to keep in my repo

This is what deletes my file:

  1. git rm --cached application/config.php
  2. make changes to the application/config.php file and another file (as a control)
  3. git commit -m 'config.php shouldn't be changed'

    result: 2 files changed, 1 insertions(+), 370 deletions(-) (config.php is 370 lines long)

like image 219
tim peterson Avatar asked Sep 05 '12 02:09

tim peterson


People also ask

How does git store different versions?

Git stores every single version of each file it tracks as a blob. Git identifies blobs by the hash of their content and keeps them in . git/objects . Any change to the file content will generate a completely new blob object.

How do I manage multiple versions in git?

Probably the easiest way is create branches for each version and then do for example git checkout v1. 0.14 or whatever branch you want to work on. The clients can then also check out the appropriate branch for the one they need as well as when upgrading to next version.

What is the advantage of git as a local repository?

One of the biggest advantages of Git is its branching capabilities. Unlike centralized version control systems, Git branches are cheap and easy to merge. This facilitates the feature branch workflow popular with many Git users. Feature branches provide an isolated environment for every change to your codebase.

Does git store every version of a file?

Second, Git's internal database efficiently stores every version of every file—not their differences—as files go from one revision to the next. Because Git uses the hash of a file's complete content as the name for that file, it must operate on each complete copy of the file.


1 Answers

I solved my own problem. I actually need this:

git update-index --assume-unchanged <filename>

Assuming the .gitignore and config.php files being specified as above in my question, here's the complete workflow:

  1. git update-index --assume-unchanged application/config.php
  2. git add -A
  3. git commit -m 'now config.php will be ignored'
  4. git push origin master

Now http://localhost will remain in the config.php on my local copy of the repo and http://my-site.com will be preserved in the config.php in my master repo.

like image 147
tim peterson Avatar answered Oct 13 '22 01:10

tim peterson