git deletes code that has been submitted to the local warehouse

The usage scenario is as follows: In the development project, a function was written in the first version, but during the version iteration process, the function was dropped, which means that the core code of the function will be deleted. This part of the code has already been submitted to If you already have a local warehouse, how do you delete the code recorded in the local warehouse?

First of all, many people will think of deleting the local code directly, submitting it, and then pushing it. However, is this feasible?

experiment

首先将一个项目代码提交到本地仓库,再推送到远程分支,如下图:

项目路径是`app/customer/cmd/rpc/internal/logic`

包含了如下的文件:

Insert image description here

本地仓库和远程仓库是一致的

项目中使用`git ls-files -c`命令查看本地仓库文件

打印行数太多就不全部截图了(截取一部分)

Insert image description here
If you only delete the workspace files, the files in the following folder
Insert image description here
will look like the picture below. There are still files in the cache area and the local warehouse. When new code is submitted, only the new code will be merged to the branch. Those workspaces Deleted files still exist.

alt

And if you use the IDE of jetbrains, you will find that the problem is directly visualized, as follows

Insert image description here
These locally deleted files are grayed out and still exist in the cache area, and can even be submitted again, but the files cannot be modified because they are no longer available locally.

So how to delete it?

plan

Since the cache area and the local warehouse generally operate in cascade, you only need to use the git command to delete the contents of the local warehouse.

  • git git revertSwitch code version to achieve content deletion

Since git is a distributed multi-branch managed code warehouse, the git implementation can implement local iteration and switch between different versions. If git HEADpoints to a certain branch, the version label will also be set when the updated version is updated.

Insert image description here
As shown in the figure above, rolling back the version can delete the code submitted by the current version. Deleting certain files will revert to the previous version of the file. However, the problem faced in this way is that many codes for new functions and codes that have nothing to do with the files that need to be deleted have also been rolled back, and they need to be rewritten and resubmitted. It is even more troublesome if the code has been updated many versions. (This method is generally not recommended unless there are major problems)

  • git resetWithdraw document

This method directly requires the tracking status of git to withdraw the file to the state before git add, which is equivalent to never submitting it.

Insert image description here
During the withdrawal operation, the local warehouse and the cache area are concatenated, and they are automatically withdrawn together, directly returning to the state before git add. The local warehouse becomes a new warehouse without any content, and the cache area is also emptied. Now you just need to delete the files you need and don't need in the folder. Run git add and git commit again. At this time, the local warehouse has been updated.

Insert image description here
As shown in the picture above, it can also be seen from the record that the file originally submitted has been deleted. Check the local warehouse:

git ls-files -c

Insert image description here
As shown above, the original app/customer/cmd/rpc/internal/logicdirectory has become completely new content.

  • git rm --cachedDelete warehouse files

git rm --cachedThe command is similar to the git reset command, but logically different. rm directly deletes the git-related content of the specified file.

Insert image description here
The git rm command also deletes the specified files in the warehouse and cache area, returning to the state without git add.

git rmand git resetare two different commands in Git that differ in their operation:

  1. git rm is the command used to remove files from version control. It performs two operations: it deletes the file from the local file system and logs the deletion into Git's version history. This way the file will no longer appear in your code base and will not be recoverable after committing the changes. You can specify the files to delete using the git rm <filename> command.

  2. git reset is used to undo a commit or point HEAD to a different commit. It is used to change the state of the current working tree and staging area. git reset can be used to undo commits and discard their changes. It can also be used to move the branch pointer to a different commit, thus changing the current code base state.

  3. If you use the git reset command to specify a commit, HEAD will point to the commit and the current working tree and staging area will be restored to the state of that commit. If you use the git reset --hard command to specify a commit, it will point HEAD to that commit and completely discard the changes to the current working tree and staging area.

To summarize, git rm is used to delete files, while git reset is used to undo or change commits, and can be used to move branch pointers. These two commands play different roles in Git, depending on your needs, choose the appropriate command to perform the desired operation.

Guess you like

Origin blog.csdn.net/xwh3165037789/article/details/132121676