your programing

Git에서 특정 파일을 무시하는 방법

lovepro 2020. 9. 30. 11:13
반응형

Git에서 특정 파일을 무시하는 방법


파일이있는 저장소가 Hello.java있습니다. 컴파일하면 추가 Hello.class파일이 생성됩니다.

나는에 대한 항목이 생성 Hello.classA의 .gitignore파일을. 그러나 파일은 여전히 ​​추적되는 것으로 보입니다.

Git에서 무시하도록하려면 어떻게해야 Hello.class합니까?


문제는 .gitignore이전에 추적되지 않은 파일 만 무시한다는 것 git add입니다. git reset name_of_file파일을 언 스테이징하고 유지하려면 실행하십시오 . (푸시 후) 저장소에서 주어진 파일을 제거하려는 경우 git rm --cached name_of_file.


새 파일을 무시하는 방법

전 세계적으로

.gitignore 파일에 무시할 경로를 파일에 추가하고 커밋합니다. 이러한 파일 항목은 저장소를 확인하는 다른 사용자에게도 적용됩니다.

장소 상에서

무시할 파일 경로를 .git / info / exclude 파일에 추가하십시오. 이러한 파일 항목은 로컬 작업 복사본에만 적용됩니다.

변경된 파일을 무시하는 방법 (일시적으로)

변경된 파일이 수정 된 것으로 나열되도록 무시하려면 다음 git 명령을 사용할 수 있습니다.

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

무지를 되돌리려면 다음 명령을 사용하십시오.

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

.gitignore에 다음 줄을 추가합니다.

/Hello.class

이것은 git에서 Hello.class를 제외합니다. 이미 커밋 한 경우 다음 명령을 실행합니다.

git rm Hello.class

git에서 모든 클래스 파일을 제외하려면 .gitignore에 다음 행을 추가하십시오.

*.class

1) .gitignore파일을 만듭니다 . 이렇게하려면 .txt파일을 만들고 다음과 같이 확장자를 변경하면됩니다.

여기에 이미지 설명 입력

그런 다음 cmd 창에 다음 줄을 작성하여 이름을 변경해야합니다.

 rename git.txt .gitignore

git.txt방금 만든 파일의 이름은 어디에 있습니까 ?

Then you can open the file and write all the files you don’t want to add on the repository. For example, mine looks like this:

#OS junk files
[Tt]humbs.db
*.DS_Store

#Visual Studio files
*.[Oo]bj
*.user
*.aps
*.pch
*.vspscc
*.vssscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.[Cc]ache
*.ilk
*.log
*.lib
*.sbr
*.sdf
*.pyc
*.xml
ipch/
obj/
[Bb]in
[Dd]ebug*/
[Rr]elease*/
Ankh.NoLoad

#Tooling
_ReSharper*/
*.resharper
[Tt]est[Rr]esult*

#Project files
[Bb]uild/

#Subversion files
.svn

# Office Temp Files
~$*

Once you have this, you need to add it to your Git repository. You have to save the file where your repository is.

Then in Git Bash you have to write the following line:

git config --global core.excludesfile ~/.gitignore_global

If the repository already exists then you have to do the following:

  1. git rm -r --cached .
  2. git add .
  3. git commit -m ".gitignore is now working"

If the step 2 doesn’t work then you should write the whole route of the files that you would like to add.


To ignore:

git update-index --assume-unchanged <path/to/file>

To undo ignore:

git update-index --no-assume-unchanged <path/to/file>

You can use below methods for ignoring/not-ignoring changes in tracked files.

  1. For ignoring: git update-index --assume-unchanged <file>
  2. For reverting ignored files: git update-index --no-assume-unchanged <file>

Create a .gitignore in the directory where .git is. You can list files in it separated by a newline. You also can use wildcards:

*.o
.*.swp

From the official GitHub site:

If you already have a file checked in, and you want to ignore it, Git will not ignore the file if you add a rule later. In those cases, you must untrack the file first, by running the following command in your terminal:

git rm --cached FILENAME

or

git rm --cached .

You should write something like

*.class

into your .gitignore file.


Add which files you want to ignore to file .gitignore:

*.class

*.projects

*.prefs

*.project


By creating a .gitignore file. See here for details: Git Book - Ignoring files

Also check this one out: How do you make Git ignore files without using .gitignore?


You can also use .gitattributes (instead of .gitignore) to exclude entire filetypes. The file is pretty self-explanatory, but I'm pasting the contents here for reference. Pay attention to the last line (*.class binary):

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.gif binary
*.ico binary
*.mo binary
*.pdf binary
*.phar binary
*.class binary

If you have already committed the file and you are trying to ignore it by adding to the .gitignore file, Git will not ignore it. For that, you first have to do the below things:

git rm --cached FILENAME

If you are starting the project freshly and you want to add some files to Git ignore, follow the below steps to create a Git ignore file:

  1. Navigate to your Git repository.
  2. Enter "touch .gitignore" which will create a .gitignore file.

Add the following line to .git/info/exclude:
Hello.class


I had a similar issue with file "dump.rdb".
I tried adding this file in various ways to .gitignore file, but only one way worked.

Add your filename, at the end of .gitignore file

NOTE: Adding the file anywhere else didn't work.

For example, see: https://gitlab.com/gitlab-org/gitlab-ce/raw/b3ad3f202478dd88a3cfe4461703bc3df1019f90/.gitignore


This webpage may be useful and time-saving when working with .gitignore.

It automatically generates .gitignore files for different IDEs and operating systems with the specific files/folders that you usually don't want to pull to your Git repository (for instance, IDE-specific folders and configuration files).


I have tried the --assume-unchanged and also the .gitignore but neither worked well. They make it hard to switch branches and hard to merge changes from others. This is my solution:

When I commit, I manually remove the files from the list of changes

내가 전에 당겨 , 내가 숨겨 놓은 변경된 파일을. 그리고 당긴 후에는 숨김 팝을 합니다.

  1. 자식 숨김
  2. git pull
  3. git stash pop

3 단계는 때때로 병합을 트리거하고 해결해야하는 충돌을 초래할 수 있습니다. 이는 좋은 일입니다.

이를 통해 팀의 다른 사람들과 공유되지 않는 로컬 변경 사항을 유지할 수 있습니다.

참고 URL : https://stackoverflow.com/questions/4308610/how-to-ignore-certain-files-in-git

반응형