Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
preface
Recently, I encountered a problem in the project: WHEN I committed code to Gitlab, I committed some unnecessary files to a remote branch. At first I thought it was mine. Gitignore didn’t filter out files that needed to be ignored. Later, IT was found that this was not the case. Because a project of our company was divided into PC terminal and mobile terminal, although both of our projects were in the same GITLab address, the configuration of the project was also configured separately. (I can’t carry this pot.)
The.gitignore file was handled, but the unnecessary files still existed in the remote branch, so I used the little-used -git rm command
Git interval concept
- Working Directory: Area for code editing.
- Stage/Index: An area where code changes and is temporarily committed to a local branch.
- Commit History: Stores committed data.
Git rm deletes workspace files
Git rm file_path deletes files from the workspace
Rm is followed by the address of the file or folder
As shown in the image above, there is a file named test.txt in the project and the file is now in the workspace and we will run the command to delete it
git rm test.txt
Copy the code
Git rm: the trace file has been changed to the staging area. The parentheses tell you to use –cached to hold the file in your workspace or use -f to force the file to be deleted. That’s what we’re saying. If the file you want to delete before you delete the code in the staging area has not been changed, it must be the same as the current version of the library
If you have to force it then you can force it as he said by adding -f to the end of the command
At this point we forcibly delete
After deleting it, we found that test.txt was deleted from your project.
At this point we have removed the useless files from the workspace
Delete the remote branch file
The.idea file is the configuration file of the compiler and is invalid for the project but we submitted it to GitLab at this point we deleted the.idea file
Enter git rm file_path -f to forcibly delete git branch files
File_path supports fuzzy matching, which assumes that there are multiple XML files in the IDEA folder
Git rm -f. Idea /*. XML will delete all XML files under idea
Git rm -f. idea will delete the files in the workspace
At this point you can use git status to check the file changes
We can see that the files in Git have changed and we just deleted them in the staging area without submitting them to the remote branch
// Commit the modified code
git commit -m 'delete idea files'
// Upload the code to the remote branch test
git push origin test
Copy the code
Then you can see that in the GitLab interface the.idea folder no longer exists so far we have removed unnecessary files from the remote branch
Deletes branch files but does not delete workspace files
git rm -f --cached file_path
Copy the code
We simply type –cached to delete the files in the remote branch while preserving the workspace.
The last
Thank you for watching this blog, if it is helpful to you, I hope to give 👍 comment collection three even!