1. Remove files
To remove a file from Git, it must be removed from the tracked file list (specifically, removed from the staging area), and then committed. This can be done with a git rm
command, and along with it, removes the specified file from the working directory so that it won't appear in the list of untracked files later.
If you simply manually delete the file from the working directory, git status
you will see at runtime in the "Changes not staged for commit" section (aka the unstaged manifest ):
-
$ rm PROJECTS.md
-
$ git status
-
On branch master
-
Your branch is up-to-date with 'origin/master'.
-
Changes not staged for commit:
-
(use "git add/rm <file>..." to update what will be committed)
-
(use "git checkout -- <file>..." to discard changes in working directory)
-
deleted: PROJECTS.md
-
no changes added to commit (use "git add" and/or "git commit -a")
Then run the operation that git rm
records this removal of the file:
-
$ git rm PROJECTS.md
-
rm 'PROJECTS.md'
-
$ git status
-
On branch master
-
Changes to be committed:
-
(use "git reset HEAD <file>..." to unstage)
-
deleted: PROJECTS.md
On the next commit, the file is no longer in version control. If the deletion has been modified before and has been placed in the staging area, you must use the forced deletion option -f
(Annotation: the first letter of force). This is a safety feature to prevent accidental deletion of data that has not yet been added to the snapshot, which cannot be recovered by Git.
Another situation is when we want to delete a file from the Git repository (ie, remove it from the staging area), but still want to keep it in the current working directory. In other words, you want to keep the file on disk, but you don't want Git to keep track of it. This is especially useful when you forget to add .gitignore
a file, accidentally adding a large log file or a bunch of .a
these build-generated files to the staging area. To do this, use the --cached
options:
$ git rm --cached README
git rm
The name of the file or directory can be listed after the command, or a glob
pattern can be used. for example:
$ git rm log/\*.log
*
Note the backslash before the asterisk \
, because Git has its own way of matching file pattern expansions, so we don't use the shell to help with the expansion. This command deletes all files log/
with the extension in the directory. .log
Similar example:
$ git rm \*~
This command deletes ~
all files ending with .
2. Move files
To rename a file in Git, do this:
$ git mv file_from file_to
View the status information at this time, and you will also see the instructions about the renaming operation unmistakably, as shown in the following figure, rename README.md to README:
-
$ git mv README.md README
-
$ git status
-
On branch master
-
Changes to be committed:
-
(use "git reset HEAD <file>..." to unstage)
-
renamed: README.md -> README
In fact, running git mv
is equivalent to running the following three commands, renaming README.md to README, removing README.md from the staging area, and adding README to the staging area:
-
$ mv README.md README
-
$ git rm README.md
-
$ git add README
Original link https://blog.csdn.net/skye_95/article/details/81261900