Git,本地和远程有1个文件内容不同

Git, having 1 file content different in local and remote

好的,所以我正在尝试拥有 2 个版本的配置文件:

在本地,在我的电脑上,我想要 config.js :

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

在用于在服务器上拉取和部署的 git 存储库中,我希望在 config.js 中包含:

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

我使用 IntelliJ 是为了 commit/push 和 Ctrl+K & Ctrl+Shift+k

所以我尝试在 git 存储库中创建我想要的文件,推送它,然后重命名它而不是将它添加到 git。但是在上次推送后文件从 git 仓库中删除了。

我也试过push然后把它的路径添加到.gitignore再修改。 (虽然不确定我是否做对了)

在 git.

中了解一点知识,实现起来一定不会那么复杂

以下是您可以做的几件事。

几个选项:

.gitignore

将文件添加到 .gitignore 文件 - 因此 git 从现在开始将忽略它。
注意:在你的情况下,因为你已经添加了文件,你将不得不使用 assume-unchanged

# .gitignore content
robots.txt

assume-unchanged

使用此标志将您的本地更改标记为未更改,这样 git 将不会跟踪任何更改,包括您的文件删除。

暂时忽略某个文件的修改,运行:

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

当您想再次跟踪更改时:

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

--[无-]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.


git add -p

为每个分支添加所需的部分(添加补丁)

git update-index --skip-worktree <path_to_file>正是您所需要的。

来自 git 文档:

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

不要混淆 --skip-worktree--assume-unchanged。 当您设置最新标志时,您承诺 git 不做任何更改。

Git 手册对此不是很清楚,所以我找到了最好的解释 here(来自 Junio Hamano,git 维护者):

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)