git study notes - remove and move files

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 ):

 
  1. $ rm PROJECTS.md

  2. $ git status

  3. On branch master

  4. Your branch is up-to-date with 'origin/master'.

  5. Changes not staged for commit:

  6. (use "git add/rm <file>..." to update what will be committed)

  7. (use "git checkout -- <file>..." to discard changes in working directory)

  8.  
  9. deleted: PROJECTS.md

  10.  
  11. 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:

 
  1. $ git rm PROJECTS.md

  2. rm 'PROJECTS.md'

  3. $ git status

  4. On branch master

  5. Changes to be committed:

  6. (use "git reset HEAD <file>..." to unstage)

  7.  
  8. 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. .logSimilar 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:

 
  1. $ git mv README.md README

  2. $ git status

  3. On branch master

  4. Changes to be committed:

  5. (use "git reset HEAD <file>..." to unstage)

  6.  
  7. 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:

 
  1. $ mv README.md README

  2. $ git rm README.md

  3. $ git add README

 

Original link https://blog.csdn.net/skye_95/article/details/81261900

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324031611&siteId=291194637