20. Moving files


  • To learn how to move a file within the repository.

01 Move the hello.html file to the lib directory

Now we will create the structure of our repository. Let us move the page in the lib directory


mkdir lib
git mv hello.html lib
git status


$ mkdir lib
$ git mv hello.html lib
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#	renamed:    hello.html -> lib/hello.html

Moving files with git, we notify git about two things

  1. The hello.html file was deleted.
  2. The lib/hello.html file was created.

Both facts are staged immediately and ready for a commit. Git status command reports the file has been moved.

02 One more way to move files

A positive fact about git is that you don’t need to remember about version control to the moment when you need to commit code. What could happen if we were using the operating system command line instead of the git command to move files?

The following set of commands turned out to be identical to our last actions. It requires more work with same result.

We can do:

mkdir lib
mv hello.html lib
git add lib/hello.html
git rm hello.html

03 Commit new directory

Let us commit this movement.


git commit -m "Moved hello.html to lib"
© 2011-2014 Git How To. All rights reserved / Contact us