.gitignore在更改文件后不会忽略我的文件

时间:2019-09-16 15:55:00

标签: android git android-studio gitignore

我有一堆文件不应该受版本控制。但是,如果我不对它们进行版本控制,则构建将失败。由于这些文件包含键/值内容,因此我在其中添加了这些文件,但将实际值替换为伪值。因此,我能够通过构建,并且在VC下也没有那些实际的键/值。我将此更改提交给了VC。我还将这些文件的列表放在.gitignore文件下,如下所示:

import pandas as pd
import numpy as np
import csv
import os
import json

content = {
    'rootpath' : os.path.abspath ( os.getcwd () ),
    'indir' : '.',
    'outdir' : 'output',
    'workdir' : 'temporary',
    'datafile' : 'file.csv',
    'configfile' : 'configs.txt',
    'savefile' : 'contents.csv',
    'model' : np.arange (4),
    'datafiles' : [os.path.join (os.getcwd (), 'data.csv'), os.path.join (os.getcwd (), 'data.dat')],
    'data' : pd.DataFrame ( np.arange (15).reshape (3, 5) ),
    'dataid' : 'g_g;r_g;r_r;',
    'result' : None,
}

hmm = {
    'hmm_a' : np.random.rand (9).reshape (3, 3), # test input of a two-dimensional (np.nd)array because one isn't enough and they will appear (during/after computation)
    'hmm_b' : np.zeros (3),
    'hmm_mu' : np.random.rand (1),
    'hmm_pi' : np.random.rand (3),
    'hmm_block' : np.random.rand (27).reshape (3, 3, 3), # test input of a three-dimensional (np.nd)array because of their appearance
}

'''
computation, changing pathname and filename, rest of the program
'''

write_in = content.copy ()
write_in.update (hmm)
path = os.path.join ( write_in ['rootpath'], write_in ['outdir'], write_in ['savefile'] )
p, filetype = os.path.splitext (path)

'''
if write_in == write_in ['data'] : # write_in only contains data
    onlydata = True
else :
    onlydata = False
'''

for c in write_in :
    if type ( write_in [c] ) == np.ndarray :
        write_in [c] = write_in [c].tolist ()
    elif type ( write_in [c] ) == pd.DataFrame :
        #write_in [c] = pd.DataFrame.to_numpy ( write_in [c], copy = True ).tolist () # needs pandas +0.24.0
        write_in [c] = write_in [c].values.tolist ()

# saving as *.csv Comma Separated Values
if 'csv' in filetype or 'CSV' in filetype :         # If chosen, take care when loading this file. Remember the data structure…!
    if onlydata :
        with open ( path, mode ) as f :                         # alternative 0; pd.DataFrame
            write_in.to_csv ( f, header = None, index = False ) # alternative 0; pd.DataFrame
       #write_in.to_csv ( path, header = None, index = False )   # alternative 1; pd.DataFrame
    else :                                          ### works.
# converting the chosen variables to linewise objects
        for c in write_in :
            if type ( write_in [c] ) == np.ndarray :
                write_in [c] = write_in [c].tolist ()
            elif type ( write_in [c] ) == pd.DataFrame :
                #write_in [c] = write_in [c].to_numpy (copy = True).tolist ()            # alternative 2
                #write_in [c] = DataFrame.to_numpy (write_in [c], copy = True).tolist () # alternative 1
                write_in [c] = write_in [c].values.tolist ()                             # alternative 0
# the saving itself
        with open ( path, 'w', newline = '' ) as f :
            w = csv.writer ( f, delimiter = ',', quotechar = '"' )#, quoting = csv.QUOTE_ALL ) # alternative 1; dict
            for key, value in write_in.items () :                                                # alternative 1; dict
                w.writerow ( [key, value] )                                                      # alternative 1; dict
# saving as *.json JavaScript Object Notation; the dict should be written into the file as onto the command line.
elif 'json' in filetype or 'JSON' in filetype :
    if onlydata :
        with open ( path, mode ) as f :
            write_in.to_json (f)#, orient = 'columns' )#, orient = DataFrame )#, index = False ) # alternative 0; pd.DataFrame
    else :                                          ### works.
# converting the chosen variables to JSON serializable objects
        for c in write_in :
            if type ( write_in [c] ) == np.ndarray :
                write_in [c] = write_in [c].tolist ()
            elif type ( write_in [c] ) == pd.DataFrame :
                write_in [c] = write_in [c].to_json ( orient = 'columns' )
# the saving itself
        with open ( path, mode ) as f :
            #f.write ( json.dumps (write_in) )      # alternative 1; dict
            json.dump ( write_in, f, indent = 4 )   # alternative 0; dict
# saving as *.txt
elif 'txt' in filetype or 'TXT' in filetype :
    if onlydata :
            with open ( path, mode ) as f :
                f.write ( str (write_in) )          ### Better make this with pd.iterrows
    else :
        with open ( path, mode ) as f :
            f.write ( str (write_in) )
# saving as *.dat; this shouldn't make a difference for the file here as if saved as *.txt.
elif 'dat' in filetype or 'DAT' in filetype :
    if onlydata :
        with open ( path, mode = mode + 'b' ) as f :
            f.write (write_in)                      ### Better make this with pd.iterrows…?
    else :
        with open ( path, mode = mode + 'b' ) as f :
            f.write (write_in)
else :
    print ( 'save_file: Unknown file format. Aborting program part.' )

现在的问题是,为了测试我的更改,我需要用实际的替换那些伪造的文件/值。但是,这样做可以在运行# from *.csv if 'csv' in filetype or 'CSV' in filetype : read_out = {} with open ( path, 'r' ) as f : reader = csv.reader (f) for k, v in reader : read_out [k] = v #for line in f : # if the above fails #(key, val) = line.split (',') # if the above fails #read_out [key] = val # if the above fails #d = {} # if the above fails #for key, val in read_out.iterrows () : # if the above fails #d [key] = val # if the above fails # converting the str the their original dtype, determined by what's in the str; better have put it in the file…? for a in read_out : if read_out [a] == '' : if 'dir' in a : # Relative paths; set *indir*, *outdir* and *workdir* to *rootpath* if not specified. read_out [a] = '.' elif a == 'result' : # No result was achieved. read_out [a] = None elif a == 'dataid' : if '[' == read_out [a] [0] and ']' == read_out [a] [-1] and "', '" in read_out [a] : read_out [a] = read_out [a].split ("', '") [ 1 : -1 ] # Take away the enclosing brackets -> split up the long str to multiple short ones by the separating sequence of a str (list) elif "', '" in read_out [a] : read_out [a] = read_out [a].split ("', '") elif '; ' in read_out [a] : read_out [a] = read_out [a].split ('; ') elif ';' in read_out [a] : read_out [a] = read_out [a].split (';') else : seppi = input ( "read_configs: Couldn't determine the separating character of *dataid*. Please type it (Standard: comma): " ) if seppi == '' : seppi = ',' if seppi in read_out [a] : read_out [a] = read_out [a].split (seppi) else : read_out [a] = ['g_g', 'r_g', 'r_r'] elif a == 'datafiles' : # input list of data files which weren't taken into computation yet; absolute paths read_out [a] = read_out [a].split ("'") [ 1 : -1 : 2 ] elif '[' == read_out [a] [0] and ']' == read_out [a] [-1] : # Should be a np.ndarray because other entries with '[' and ']' are already sorted out. if read_out [a].count ('[') == read_out [a].count (']') == 1 : # one-dimensional array #floats = np.array ( read_out [a] [ 1 : -1 ].split (', ') [:] ).astype (np.float128) # alternative 1 #ints = np.array ( read_out [a] [ 1 : -1 ].split (', ') [:] ).astype (np.int64) # alternative 1 floats = np.fromstring ( read_out [a] [ 1 : -1 ], dtype = np.float128, sep = ',' ) # alternative 0 ints = np.fromstring ( read_out [a] [ 1 : -1 ], dtype = np.int64, sep = ',' ) # alternative 0 if ints.all () == floats.all () and not ints.all () == np.zeros (floats.size).all () : read_out [a] = ints else : read_out [a] = floats else : # multi-dimensional array; actually works only with two-dimensional arrays. md_array = read_out [a] [ 2 : -2 ].split ('], [') # Removing '[[' from the beginning and ']]' from the end, splitting it up to a list where the elements are the inner np.ndarrays as str. f, i = [], [] for b in range ( len (md_array) ) : # iterating over the length of *md_array* #floats = np.array ( md_array [b].split (', ') ).astype (np.float128) # alternative 1 #ints = np.array ( md_array [b].split (', ') ).astype (np.int64) # alternative 1 floats = np.fromstring ( md_array [b], dtype = np.float128, sep = ',' ) # alternative 0 ints = np.fromstring ( md_array [b], dtype = np.int64, sep = ',' ) # alternative 0 f.append (floats) i.append (ints) floats = np.array (f) ints = np.array (i) if ints.all () == floats.all () and not ints.all () == np.zeros (floats.size).all () : read_out [a] = ints else : read_out [a] = floats # from *.json; the dict should be written into the file as onto the command line. elif 'json' in filetype or 'JSON' in filetype : with open ( path, 'r' ) as f : # alternative 0 read_out = json.load (f) # alternative 0 #f.read ( json.dumps (read_out) ) # alternative 1 # converting the str the their original dtype, determined by what's in the str; better have put it in the file…? for a in read_out : if a == 'result' or a == 'datafiles' or a == 'dataid' : # input list of data files which weren't taken into computation yet; absolute paths pass elif read_out [a] == '' and 'dir' in a : # Relative paths; set *indir*, *outdir* and *workdir* to *rootpath* if not specified. read_out [a] = '.' elif type ( read_out [a] ) == list : #read_out [a] = pd.read_json ( read_out [a], numpy = True, precise_float = True ) read_out [a] = np.asarray ( read_out [a], dtype = np.float128 ) # from *.txt elif 'txt' in filetype or 'TXT' in filetype : with open ( path, 'r' ) as f : reading = f.read () # from *.dat; this shouldn't make a difference for the file here as if saved as *.txt. elif 'dat' in filetype or 'DAT' in filetype : with open ( path, 'rb' ) as f : f.read (read_out) # Put the variables in the dicts *content* or *hmm*. for a in read_out : if 'hmm_' in a : hmm [a] = read_out [a] else : content [a] = read_out [a] if 'data' in content : content ['data'] = pd.DataFrame ( content ['data'] ) 时将这些文件显示在已更改文件的列表中。

我清楚地将它们添加到/app/src/main/assets/*.json /app/src/main/assets/*.kt 下,为什么还要在这里再次看到它们?这是我看到的:

git status

2 个答案:

答案 0 :(得分:1)

问题的根源是无法忽略当前跟踪的文件。

不幸的是,修复需要从索引/暂存区中删除文件(以使它们不被跟踪),这又意味着Git将删除文件从工作树中。您可以使用git rm --cached禁止立即删除此类文件,但是将来,任何回到包含文件的现有提交的人都会将文件放入其索引/临时文件中-area及其工作树,然后从该提交移回到当前提交(没有文件)意味着Git将删除文件。

也就是说,每当您git checkout提交具有 个文件的提交时,它们都会同时进入您的索引/临时区域(这是同一件事的两个名称)和您的工作-tree(可以在其中查看和使用文件)。每当您从签出这种具有 种提交的文件转移到没有没有这些文件的提交时,Git都会从文件中删除这些文件。索引,并从您的工作树中。因此,这些文件在任何 historyical 中的存在都会有效地使存储库“中毒”。

此问题的唯一完整的解决方案永远不要首先提交文件。这就要求您及时返回并停止提交文件。此解决方案的一种近似方法是重写提交历史记录,以便没有剩余的提交都具有文件:消除所有 did 都具有文件的提交,也许插入新的和改进的替换提交,即没有文件。查看How to make Git "forget" about a file that was tracked but is now in .gitignore?

的许多答案中的一些

此解决方案的缺点是提交中不能包含示例原型文件。解决此缺陷的方法是将示例原型文件放入您的提交中,但要使用其他一些文件名:

  

我有一堆文件不应该受版本控制。但是,如果我不对它们进行版本控制,则构建将失败。由于这些文件包含键/值内容,因此我在其中添加了这些文件,但将实际值替换为伪值。

让您的构建过程使用替代(样本文件)文件代替实际文件进行测试构建。

该软件的用户将使用实际文件,这些文件将不受版本控制。测试版本的用户将使用测试文件,这些文件受版本控制。

答案 1 :(得分:-1)

您确定将.gitignore文件放在正确的目录中吗?您应将.gitignore放在工作目录中,而不是.git(存储库)目录中