[RUBY] [Commentary with images] Github beginners summarized gitignore

Overview

Recently, when I asked an engineer to review the code of the deliverable, he told me that "there are too many files pushed to github and I can't check them! If you want to jointly develop, you should use gitignore". However, what is gitignore? I was in a state, so I investigated and summarized what I learned. I will share it with the hope that it will lead to the improvement of skills of those who do not know yet.

What is .gitignore

A file that can declare files/directories that are not managed by the remote repository on Github. The files/directories specified in the .gitignore file will now be excluded from the git add command and will no longer be sent to the Github remote repository when pushed.

What are the benefits?

(1) Since unnecessary data is not pushed, it becomes easier for a third party to check the code when viewing it. (2) Security can be improved by excluding information that you do not want to publish from Git management.

What should I use it for?

-Automatically generated in the OS and local environment (ex. Mac .DS_store file) -Used in the local environment but not required in the remote environment (ex. Images used only in testing) -Data and executable files created when compiling and building (ex. Gem files installed for each project) -Terminal-specific setting information (ex. Environment variable file containing login information)

etc. It's important to manage properly with gitignore because it's an environment that many people see!

I actually tried

Execution environment

· MacOS: Mojave 10.14.6 ・ Ruby: v2.5.1 ・ Ruby on Rails: v5.2.4 -Text editor: Atom

This time, we will verify the gem file installed by the existing rails project as an example. First of all, I will push to Github without using gitignore. (It is assumed that you have already registered a github account and created a repository)

On the rails project branch, run bundle install --path vendor/bundle Install gem (--path option specifies the gem installation destination to vendor/bundle)

Use the git status command to check the status of Git management.

git status

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	vendor/bundle/

nothing added to commit but untracked files present (use "git add" to track)

When you run it, you will see that it says "untracked files". To understand the above correctly, we have summarized the "tracking" that shows the status status of files as follows.   -Tracking status: Files that have already been registered in the staging area and have not been committed -Untracking status: Files that have not yet been registered in the staging area

This means that the installed gem is not yet registered in the staging area. The git add command is a command that prepares to send to a remote repository by registering files and directories in the staging area and collecting them (changing them to the tracking state).

  1. Execute the git add command Here is the state of the file after execution.
git add vendor/bundle   # vendor/Specify files under the bundle directory
git status

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   vendor/bundle/ruby/2.5.0/bin/bootsnap
	new file:   vendor/bundle/ruby/2.5.0/bin/byebug
	new file:   vendor/bundle/ruby/2.5.0/bin/chromedriver-helper
	new file:   vendor/bundle/ruby/2.5.0/bin/chromedriver-update
	new file:   vendor/bundle/ruby/2.5.0/bin/listen
	new file:   vendor/bundle/ruby/2.5.0/bin/nokogiri
	new file:   vendor/bundle/ruby/2.5.0/bin/puma
	new file:   vendor/bundle/ruby/2.5.0/bin/pumactl
	new file:   vendor/bundle/ruby/2.5.0/bin/rackup
	new file:   vendor/bundle/ruby/2.5.0/bin/rails
・
・
The description of the gem file group continues below

As you can see from "changes to be committed", you can see that the commit is ready (= tracking state).

  1. Commit, push and send the contents of the local repository to the remote repository.
git commit -m "gem installation 1"
git push

git1.png

Naturally, a lot of gem file diffs are saved, resulting in more than 5,000 [file changed] fields.

Try using .gitignore

Now let's use gitignore to prevent the files inside vendor/bundle from being saved in the remote repository!

Let's examine the work of installing pry-byebug additionally to the gem file earlier as an example.

Execute gem install pry-byebug to install

Open the .gitignore file located in the root directory and write it. In the case of my execution environment, it was automatically generated in the project, but if it does not exist, create a file with touch .gitignore.

The table below shows how to write .gitignore.

Thing you want to do How to specify Example
File with the same name/Exclude all directories File or directory name file.rb
Exclude everything under the directory with the same name At the end/Put on directory/
Exclude certain files or directories Other than the end/Put on /directory/file.rb
Of the directory specified by the relative path
Exclude all contents
At both the beginning and the end
/Put on
/dirctory/
Exclude certain extensions At the beginning*Put on *.txt
Set for a specific file or directory At the beginning!Put on !dirctory/file.rb
Add a comment At the beginning#Put on #comment

This time, I want to exclude all the contents of vendor/bundle, so write as follows.

/vendor/bundle/

In this state, specify the vendor/bundle bus and execute git add

git add vendor/bundle

The following paths are ignored by one of your .gitignore files:
vendor/bundle
Use -f if you really want to add them.

.ignore told me to exclude that path. Re-execute changes to the entire project with git add -A for staging

git add -A

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   .gitignore

The code-changed .gitignore is registered in the staging area, but vendor/bundle is not registered either.

git commit -m "Install gem 2"
git push

result gitignore.png

I was able to exclude vendor/bundle and make the remote repository cleaner!

What to do if you have already pushed to Github

The point of using .gitignore is that if it is already saved in the remote repository, writing it to .gitignor as it is will not work. If you want to exclude from the middle such as an existing project, you need to delete the cache on the remote repository. (Cache = data for reusing the content in the response the next time a similar request occurs)

Execute either of the following to delete the cache. -Delete only the data on the remote repository

git rm -r --cached vendor/bundle

-Delete both remote repository and local repository data

git rm -r vendor/bundle

You can exclude it by writing .gitignore after deleting the cache and following the steps of add, commit, push again.

result vendor:bundle.png

The cache was deleted from the remote repository and I was able to set the vendor/bundle not to be saved after pushing!

Finally

It's hard to realize if it's just personal development, but I was able to build up another foundation as a technology to improve efficiency/security. I heard that it is often used in practice, so I would like to actively use it. Also, I hope it will help the same beginners as me. If you find any mistakes, please let us know in the comments.

Recommended Posts

[Commentary with images] Github beginners summarized gitignore
[Rails] Beginners can resize images with image_tag (groping)
[Must-see for beginners] Compile settings with IntellJ [Many images]