Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .gitignore work on commit, push, or upon reaching the server?

Tags:

git

gitignore

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?

like image 790
SirPython Avatar asked Aug 23 '15 17:08

SirPython


People also ask

Does Gitignore need to be committed first?

The root of a project is the folder that is the parent for all the project files and subfolders. In order for a . gitignore file to work correctly, it must be committed to GitHub before we commit (by accident) any code we don't want to push to GitHub.

Should I include .gitignore In commit?

Normally yes, . gitignore is useful for everyone who wants to work with the repository. On occasion you'll want to ignore more private things (maybe you often create LOG or something. In those cases you probably don't want to force that on anyone else.

Where should .gitignore be placed?

A . gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .

Which comes first push or commit?

Basics. – Pushing comes after committing. Git commit records and tracks changes to the repository with each commit points to a tree object that captures the state of the repository at that moment the commit was performed, all in one complete snapshot.


2 Answers

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?

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 by git add and git rm (and other git 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 use git add to record its removal into the index.

I push my commits? Does git git only push files that aren't being described in the .gitignore?

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 too

If 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 use git 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.

It reaches the origin? Does the server that is holding the git repository keep out files based on it's .gitignore?

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 "its gitignore". The .gitingore file must be added to the repository (and committed every time it changes) in order to have effect for everybody.

like image 167
axiac Avatar answered Oct 13 '22 02:10

axiac


I commit/add my files locally?

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.

Does git git only push files that aren't being described in the .gitignore?

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.

Does the server that is holding the git repository keep out files based on it's .gitignore?

No: the server will just add the .gitignore to the repo, without any side-effect the other versioned files.

like image 34
VonC Avatar answered Oct 13 '22 04:10

VonC