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

不想你离开。 提交于 2019-12-22 09:39:56

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/32169238/does-gitignore-work-on-commit-push-or-upon-reaching-the-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!