Git – File Operations


Normal file operations like deleting, moving, changing file name can be done in git.

Removing File

A tracked file can be removed liked below.

$ git rm LICENSE  
rm 'LICENSE' 
$ git status 
On branch master 
Changes to be committed: 
  (use "git reset HEAD <file>..." to unstage) 
 
        deleted:    LICENSE
  • rm removes file

After remove operation and commit the file will be not tracked and exist for the future commits but exist in the history.

If the file is edited we need to force removal with the following command.

$ git rm README.md     
error: the following file has local modifications: 
    README.md 
(use --cached to keep the file, or -f to force removal) 
$ git rm -f README.md 
rm 'README.md'
  • rm removes the specified file
  • -f forces for removal even if the file is edited

Moving Files

Unlike other vcs Git do not tracks files movements for reguler movement. To make  Git aware of the movement mv is used.

$ git mv main.py main2.py 
$ git status 
On branch master 
Changes to be committed: 
  (use "git reset HEAD <file>..." to unstage) 
 
        renamed:    main.py -> main2.py
  • mv moves main.py to main2.py

LEARN MORE  PHP Glob() Function To Match Path, Directory, File Names with Examples

Leave a Comment