git: hide/remove files never to be versioned

2019-04-19 17:13发布

I'm trying to get started using git and tortoise-git.

Is there a way to hide files that should never be tracked completely? Currently, all temporary build files are in the same "Not Versioned" list as new files when I commit a change.

5条回答
对你真心纯属浪费
2楼-- · 2019-04-19 17:52

You need to investigate .gitignore files.

git help gitignore
查看更多
女痞
3楼-- · 2019-04-19 17:58

TortoiseGit can add files to ignore list with many options.

Right click -> TortoiseGit -> Add to ignore list -> by filename / by extension

A dialog shows to choose options:

Ignore type

This file only (match absolute name),

Recursive (match same name)

Ignore file

.gitignore in root dir (need to commit)

.gitignore in included dir (need to commit)

.git/info/exclude (like a config file, stored locally)

查看更多
再贱就再见
4楼-- · 2019-04-19 18:03

In case you have files already added to repository, but need to remove/ignore them. Follow these steps:

  1. Move those files somewhere out of repository.
  2. Make a commit, which would delete those files from repository
  3. Move those files back to repository to the place where they have been
  4. Make a commit, but now instead of adding those files to repository right click on those files. Select "Add to ignore list"->"By file name". The option to which .gitignore you add those is irrelevant, but you can look up the difference in the internet.
查看更多
再贱就再见
5楼-- · 2019-04-19 18:05

Since one of the benefits of TortoiseGit is to use the UI, especially if you have used other products like TortoiseSVN and TortoiseHg, here are some UI ways to achieve this:

File not already added

An easy way to add files to the .gitignore exclusions is when you are selecting files to be staged when you do a git commit through Tortoise - Tortoise will list all non-ignored files which aren't currently in your repo under the Not Versioned Files box at the bottom of the commit screen:

Adding file exclusions to .gitignore during commit

Unwanted file already added by mistake

If you've already committed or pushed the undesirable file, another way in newer (circa 1.8.14) versions of TortoiseGit to remove an existing file from your repo branch and add it to one of the .gitignores is hidden under the right mouse function Delete and add to ignore list:

Removing versioned file from Repo and adding to GitIgnore

You then have several .gitignore options to elect either on whether to add just this file or wildcard matches, and to which .gitignore to add it to - local folder, repo root, or your `.git/info/exclude'.

However, right at the last minute, you are given the opportunity to keep the local copy of the file:

Deleted From Repo But Kept Locally

查看更多
聊天终结者
6楼-- · 2019-04-19 18:07

Create a text file called .gitignore in your root folder and add lines like the following to exclude files:

*.obj
test.c

Then add .gitignore to your git repository and commit:

$ git add .gitignore
$ git commit .gitignore
查看更多
登录 后发表回答