Git如何忽略指定文件 - Git how to ignore specified file

- 此内容更新于:2016-02-02



Suppose I have a git repository with 2 branches:

  • branch1

    • file.exe
    • config.xml
  • branch2

    • file.exe
    • config.xml

Both of repositories contain the same files. But after running file.exe it overwrites config.xml so each branch contains its own version of this file.

If I want to git checkout branch2 from branch1 I get error that file config.xml was changed. I tried to ignore this file with .gitignore but after git push remoteserver it removes config.xml on remote server.

I tried git update-index --assume-unchanged config.xml. After this I can push to remote server although config.xml was changed but I still can't switch to another branch. The error: Entry 'config.xml' not uptodate. Cannot merge. So my question - is there way to ignore file in local repository so I can push to remote server or switch to another brunch but not to remove this file at all? And nice option will be to push this file when I need manually.


(原文:add this file to .gitignore)


(原文:@Babul He already tried that, q.v. the question.)


(原文:Do you want config.xml to be versioned at all? Would you consider making the file completely untracked by Git?)


(原文:that means your .gitignore is not working right ?)


(原文:I tried to ignore this file with .gitignore but after git push remoteserver it removes config.xml on remote server. - that is what is supposed to happen when you remove a file from version control. Surely, it will appear on the remote server when it runs the file.exe, no?)


To ignore congif.xml in any path Create .gitignore and add a entry **/config.xml. To resync run

git rm -r --cached . 


git rm –cached `git ls-files -i –exclude-standard`

then commit


(原文:Will it remove config.xml on remote server after git push?)


(原文:The file may still be there on the server, but next time someone else pulls from the branch the file will not be updated. You need to rethink your design here.)


(原文:@folibis to remove it from remote server you have to delete it in local and push the commit to remote.)


(原文:No, that exactly what I want, that the file will stay on the server. I'll try it later. Thanks)