可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a repository with a file Hello.java
. When I compile it an additional Hello.class
file is generated.
I created an entry for Hello.class
in a .gitignore
file. However the file still appears to be tracked.
I want to know how to make git ignore Hello.class
.
回答1:
The problem is that .gitignore
ignores just files that weren't tracked before (by git add
). Run git reset name_of_file
to unstage the file and keep it. In case you want to also remove given file from the repository (after pushing), use git rm --cached name_of_file
.
回答2:
How to ignore new files
Globally
Add the path(s) to your file(s) which you would like to ignore to your .gitignore file (and commit them). These file entries will also apply to others checking out the repo.
Locally
Add the path(s) to your file(s) which you would like to ignore to your .git/info/exclude file. These file entries will only apply to your local working copy.
How to ignore changed files (temporarily)
In order to ignore changed files to being listed as modified, you can use the following git command:
git update-index --assume-unchanged <file>
To revert that ignorance use the following command:
git update-index --no-assume-unchanged <file>
回答3:
Add the following line to .gitignore:
/Hello.class
This will exclude Hello.class from git. If you have already committed it, run the following command:
git rm Hello.class
If you want to exclude all class files from git, add the following line to .gitignore:
*.class
回答4:
1) create a .gitignore
file, so to do that, you just create a .txt
file and change the extention as following:
Then you have to change the name writing the following line on the cmd:
rename git.txt .gitignore
Where git.txt
is the name of the file you've just created.
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 your git bash you have to write the following line:
git config --global core.excludesfile ~/.gitignore_global
If the respository 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 hole route of the files that you would like to add.
Hope it helps!
回答5:
To ignore:
git update-index --assume-unchanged <path/to/file>
To undo ignore:
git update-index --no-assume-unchanged <path/to/file>
回答6:
You can use below methods for ignoring/not-ignoring changes in tracked files.
- For ignoring:
git update-index --assume-unchanged <file>
- For reverting ignored files:
git update-index --no-assume-unchanged <file>
回答7:
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
回答8:
You should write something like
*.class
into your .gitignore
file.
回答9:
From 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 .
回答10:
add to .gitignore , which files you want to ignore
*.class
*.projects
*.prefs
*.project
回答11:
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?
回答12:
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
回答13:
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).
回答14:
Add the following line to .git/info/exclude:
Hello.class
回答15:
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
回答16:
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
Before I pull, I stash the changed files. And after pull, I do a stash pop.
- git stash
- git pull
- git stash pop
Step 3 sometimes will trigger a merge and may result in conflict that you need to resolve, which is a good thing.
This allows me to keep local changes that are not shared with others on the team.
回答17:
If you have already committed the file and you are trying to ignore it by adding to .gitignore file git will not ignore it for that first you have to do below things
git rm --cached FILENAME
if you are staring the project freshly and you want to add some files to git ignore follow below steps to create git ignore file
1.Navigate to your git Repo.
2.Enter touch .gitignore which will create .gitignore file.