Ellone
Ellone

Reputation: 3898

Git, having 1 file content different in local and remote

Ok, so I'm trying to have 2 versions of a config file :

Locally, on my computer, I'd like to have config.js :

angular.module('app')
  .constant('config', {
    backendBaseUrl: 'http://127.0.0.1:8000/api/catalog'
  });

And on the git repo used to pull and deploy on a server, I'd like to have in config.js :

angular.module('app')
  .constant('config', {
    backendBaseUrl: '/api/catalog'
  });

I'm using IntelliJ in order to commit/push with Ctrl+K & Ctrl+Shift+k.

So I tried to make the file like I want on the git repo, push it, and then rename it and not adding it to git. But the file got deleted from the git repo after the last push.

I also tried to push it and then add its path to the .gitignore before modifying it. (Not sure I did that right though)

It must not be that complicated to achieve with a bit of knowledge in git.

Upvotes: 0

Views: 79

Answers (2)

Dmitry P.
Dmitry P.

Reputation: 834

git update-index --skip-worktree <path_to_file> is exactly what you need.

From git documentation:

When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead

Do not confuse --skip-worktree with --assume-unchanged. When you set the latest flag you promise git not to do any changes.

Git manual is not very clear about it, so the best explanation I've found here (from Junio Hamano, git maintainer):

Assume-unchanged should not be abused for an ignore mechanism. It is "I know my filesystem operations are slow. I'll promise Git that I won't change these paths by making them with that bit---that way, Git does not have to check if I changed things in there every time I ask for 'git status' output". It does not mean anything other than that. Especially, it is not a promise by Git that Git will always consider these paths are unmodified---if Git can determine a path that is marked as assume-unchanged has changed without incurring extra lstat(2) cost, it reserves the right to report that the path has been modified (as a result, "git commit -a" is free to commit that change)

Upvotes: 1

CodeWizard
CodeWizard

Reputation: 141956

Here are few things which you can do.

several options:

.gitignore

Add the file to .gitignore file- so git will ignore it from now on.
Note: In your case since you have already added the file you will have to use the assume-unchanged

# .gitignore content
robots.txt

assume-unchanged

Use this flag to mark your local changes as unchanged so git will not track any changes including your file delete.

To temporarily ignore changes in a certain file, run:

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

When you want to track changes again:

git update-index --no-assume-unchanged <file>

--[no-]assume-unchanged

When this flag is specified, the object names recorded for the paths are not updated.
Instead, this option sets/unsets the "assume unchanged" bit for the paths.

When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

enter image description here


git add -p

Add the desired part per branch (add patch)

enter image description here

Upvotes: 1

Related Questions