【Git】Understanding and use of .gitignore files

background

Recently, I encountered a problem with git. When I pulled the code from the remote to the local, it was prompted that the DS_Store file was modified, and the pull failed.
insert image description here

The git status view is indeed that the file has been modified,
insert image description here

reason

First of all, you need to know the difference between pull and fetch. Pull can be understood as fetch+merge. fetch will only update the remote new submission record to the local library, but will not automatically merge with the local record, that is, it will not change the files in the local working directory. ; while pull will not only update the remote new submission record to the local library, but also merge with the local record to change the files in the local working directory. For this point, you can refer to https://www.cnblogs.com/FengZeng666/p/15640934.html

About merge, you can refer to the Git Chinese document "Creating and Merging Branches" , the default merge strategy of git is recursive, recursive three-way merge, you can refer to http://www.javashuo.com/article/p-gisxwblx-ck.html

When the same file is modified remotely and locally, according to the description in the above algorithm, the two files that need to be merged are modified relative to the file of the common ancestor. Git does not know how to discard it, that is, a conflict occurs. The above This problem occurs because of this reason.

solve

Configure git's ignore file .gitignore. How to configure can refer to the ignore file section in "Record Every Update to Warehouse" in the document . GitHub also provides templates for different languages. The template address is https://github.com/github/gitignore

In response to the above problem, the following two lines of configuration information are added to the .gitignore file,

# 忽略所有项目文件夹中的.DS_Store文件
/**/.DS_Store

# 忽略所有目录下名为.idea的文件夹
.idea/

After setting it up, I found that it still didn’t work. After a bit of Baidu, it said that the .gitignore file is only effective for files that have not been included in the track, that is, those files that have not been added by git. Which files are included in the track can be viewed through the command, so you git ls-filesneed Remove the management of files that need to be ignored locally and remotely. You can first clone a complete copy of the remote warehouse, and then push it to the remote after removing the management locally.

Specific steps:

  1. clone remote complete warehouse
  2. Modify the .gitignore file in the clone to the local warehouse and configure the ignore rules
  3. Execute the following commands in sequence

    git rm -r --cached . # Move all managed files out of management
    git add . # Include all files in the directory that do not conform to .gitignore rules into git management
    git commit -m 'update .gitignore' # Submit to local warehouse

  4. push local warehouse to remote

The above is to operate on the entire project folder, because some project subfolders also have .DS_Store files. If you just ignore a certain specific file, it is better to operate on a single file.

Tips

1. What is the meaning of the . parameter in the above command? What is the difference between . and *?

. Indicates all files that are or should (filtered by gitignore files) be included in the management
* Indicates all files in the local directory

2. Under what circumstances will conflicts occur in merge?

Conflict occurs only when the same file is modified, and it does not affect different file modifications

3. Under what circumstances will the pull command fail to execute when there is a merge conflict, and under what circumstances will it be successfully executed and the conflict will be written to the file for processing?

When the same file has been modified both at the remote end and at the local end, if the local modification has been committed, the command can be executed when pulling, and the conflict will be written to the file, and the file can be committed again after manual processing. If the local modification has not been committed, the pull command will directly fail to execute. It is also easy to understand that the merging of git is essentially the merging of commit and commit. If a file is modified and not committed, it means that the content of the file corresponding to the commit before it is modified is merged, which will lead to existing modifications. The loss of information, so git prevents this from happening.

4. What does the gitignore file do?

It only takes effect for files that are not managed by git, and can be git rm -r --cached xxxmanaged by removing the specified file

5. What is the default location and scope of the gitignore file?

By default, it is in the root directory of the project and takes effect for all directories under the project. If there is also a .gitignore file configuration in the subdirectory, the subdirectory will use its own configuration first

6. Under what circumstances will push delete remote files?

After git rm -r --cached xxxremoving the specified file from management, commit to the local, and then push to the remote, the remote will delete the file

7. What is the difference between downloading via clone and downloading in ZIP format by clicking on the page?

What is downloaded in ZIP format is the current latest source file of the project, which is just a file.
The clone mode download is to download the latest source files and all historical records of the remote warehouse, including the .git folder

8. What is the difference between creating a new file in the local warehouse and creating a new file in the remote warehouse?

Files created locally will not be automatically managed by git, and need to be added manually. Files created on the remote warehouse are automatically included in the management

emm...Let’s talk about it after thinking about it. The above are some of my questions at the beginning. I have also tested what I said before. Please correct me if my understanding is wrong.

Guess you like

Origin blog.csdn.net/atwdy/article/details/126537529