How is renaming done efficiently in git?
$ pwd
$ ll
$ cd git-rainbow/
$ ll
$ cd mtxcrm/
$ ls -la
All the work is first operated in the workspace (or working directory), then added to the staging area, and then submitted by means of commit. Is renaming the same step?
First, rename the file in the working directory, let's try whether this method is feasible, or if there is a problem if it is feasible.
$ touch readme
$ git add readme
$ ls -la
For example, if you want to rename the readme file to readme.md, use the following command:
$ mv readme readme.md
At this point, let's check the status of git and use the following command:
$ git status
The prompt information returned by the above screenshot indicates that the newly added readme.md file has not been submitted. What should we do in this situation? Use the following command to add the newly added readme.md file to the staging area:
$ git add readme.md
$ git status
At the same time, the readme file needs to be deleted. The specific command is as follows:
$ git rm readme
Then check the git status again:
To sum up, the core steps of renaming files are as follows:
$ mv readme readme.md
$ git add readme.md
What are the steps to operate directly with git?
We first restore to the previous state, that is, the file name readme has no extension. How to achieve it, use the following command:
$ git reset --hard
This operation is a relatively risky behavior, why do you say this?
Because once this command is executed, all changes in the working directory of the temporary storage area will be cleaned up, but when we just want to clean up the contents of the working directory of the temporary storage area, this operation is no longer dangerous, but instead is a useful operation.
In this way, our working path is clean, that is, our staging area does not have any files or directories that need to be submitted.
We can also view the history of git using the following command:
$ git log
$ touch readme
$ git add readme
Below we directly use only the git command to change the file name, the specific command is as follows:
$ git mv readme readme.md
Then check the git status again:
Summary: It used to be two steps to put the renamed file in the staging area. Now you can do it with the following command~
$ git mv readme readme.md
If you need to change the file name in the future, use the git mv command directly. Then what are we going to do? Submit the content in the staging area with a note stating that the file readme is renamed readme.md
$ git commit -m’Move readme to readme.md’
Then let's view the history commits of git, as shown in the following screenshot:
It can be seen that in git, renaming files is simpler than linux operations.