10 Moving Files in Git

Performing tasks such as reorganizing files, changing file formats, and so on, requires that files and sometimes entire directories get moved. git mv handles this for you.

You provide it with two options: the name of the original file and the new name. This works on files, directories, or symlinks—anything Git can track.

You can move files, directories, and symlinks into other directories as well. Provide git mv a directory as the second option, and you’re set.

Git stages the change for you after you call git mv. You must call git commit after git mv to make the move permanent.

git mv won’t overwrite an existing file; it displays an error instead. You can override this behavior by providing --force (or -f). Be careful, though, because this makes Git overwrite the existing file. That’s dangerous if the existing file you’re overwriting isn’t tracked by Git. You have no way of getting that file back.

What To Do...
  • Move a file or directory.

    For example, to move README.md to README.rst, use this:

     
    prompt>​ git mv README.md README.rst
     
    prompt>​ git commit -m "Changed README from Markdown to ReSTructured text"
     
    [master f810d86] Changed README from Markdown to ReSTructured text
     
    1 files changed, 0 insertions(+), 0 deletions(-)
     
    rename README.md => README.rst (100%)
  • Move a file or directory into another directory.
     
    prompt>​ git mv README.rst docs/
     
    prompt>​ git commit -m "Moved README into docs/ directory"
     
    [master 99a0de8] Moved README into docs/ directory
     
    1 files changed, 0 insertions(+), 0 deletions(-)
     
    rename README.rst => docs/README.rst (100%)

Related Tasks

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.26.138