How to finally ignore a file in all sub-directories via .gitignore

558 Views Asked by At

I know there are lots of duplicate questions but I'm fully confused. I should admit I'm completely new to Git. I'm using RabbitVCS, However, I edit the .gitignore file using gedit manually. That's what I have done:

  1. I have created a repository in Github.
  2. I cloned it in my local computer using RabbitVCS.
  3. I have pasted all files and folders of project into the repository.
  4. I have created a .gitignore file in the root of repository which includes this pattern: *~.

I haven't commited anything yet.

I want to ignore all ~ and such other temp files in root and sub-directories. It ignores all files in root, but has no effect in sub-directories. When I want to commit using RabbitVCS, I can see them in the commit window.

I'm confusing because in Bazaar, we first add files, then commit them. But it Git, it seems we should just commit to add new files! Is it right?

What am I doing wrong? should I commit first?

1

There are 1 best solutions below

3
mzedeler On

From the gitignore manual:

A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".

As Jan Hudec writes, you can just use *~:

$ mkdir test
$ cd test
$ git init
Initialized empty Git repository in /tmp/test/.git/
$ echo '*~' > .gitignore
$ mkdir -p some/deep/directory
$ touch some/deep/directory/test.txt
$ touch some/deep/directory/test.txt~
$ git add some/deep/directory/test.txt*
The following paths are ignored by one of your .gitignore files:
some/deep/directory/test.txt~
Use -f if you really want to add them.
fatal: no files added