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 in 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 #
By moving files with git, we notify git about two things
hello.htmlfile was deleted.
lib/hello.htmlfile 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 think about version control the moment when you need to commit code. What would happen if we were using the operating system command line instead of the git command to move files?
The following set of commands have the same result as the ones we used above, but the ones below require a little more work.
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"