How to ignore certain files in git?

2019-01-02 16:23发布

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.

标签: git gitignore
17条回答
余生请多指教
2楼-- · 2019-01-02 17:00

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>
查看更多
与君花间醉酒
3楼-- · 2019-01-02 17:00

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.

  1. git stash
  2. git pull
  3. 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.

查看更多
泛滥B
4楼-- · 2019-01-02 17:05

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.
查看更多
深知你不懂我心
5楼-- · 2019-01-02 17:06

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).

查看更多
谁念西风独自凉
6楼-- · 2019-01-02 17:06

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

查看更多
登录 后发表回答