Say I have a .gitignore
to ignore all .class
files.
Are these files kept out of the remote origin when...
I commit/add my files locally? Does my
git
look for a.gitignore
when add/commit is used, and based on what it says, removes things from the commit?I push my commits? Does
git
git only push files that aren't being described in the.gitignore
?It reaches the origin? Does the server that is holding the git repository keep out files based on it's
.gitignore
?
Yes. A
.gitignore
is applied as soon as it is exists, for all files not yet added to the index.If an ignored file was already added to the index (before the .gitignore was created/updated), then that file will be part of the commit. You would need to remove it manually first:
git rm --cached -- afileToIgnore
Don't forget to add and commit the
.gitignore
file itself, for those ignore rules to be valid not only on your local repo, but on any clone of that repo as well.Yes, unless that file was added before the
.gitignore
, or if it was force added.A
git add --force -- afile
will add a file to the index even if it is supposed to be ignored.No: the server will just add the
.gitignore
to the repo, without any side-effect the other versioned files.git add
ignores files and directories based on the rules expressed in.gitignore
.git commit
doesn't care about your.gitignore
. It creates a new commit in the local repository using the information already prepared bygit add
andgit rm
(and othergit
commands) into the index.git rm
is the command you can use to remove a file and record into the index you want to stop tracking it. If you have already removed the file from the working tree you can also usegit add
to record its removal into the index.git push
also doesn't care about your.gitignore
because it doesn't work with files. It sends to the remote repository information that already exists in the local repository. If the file you want to ignore is already there then it will go to the remote repository tooIf you have files in the repository and you want them to not be tracked any more you have to add their names (or wildcards that match their names) into
.gitignore
and usegit rm
to add the removal request into the index, to be processed on the next commit.The versions of those files that already exist in the repository (local and remote) are not deleted. You have to rewrite the history for that but rewriting history on a repository used by many persons is not recommended.
Everything that's in the commits you push reach the remote repository. But in order for
git
to respect.gitignore
when your co-workers check the project out on their computers, you have to add the.gitignore
file to the repository; and commit, and push, of course, to let the.gitignore
file find its way to their systems. There is no "my.gitignore
" and "itsgitignore
". The.gitingore
file must be added to the repository (and committed every time it changes) in order to have effect for everybody.