Moving Files

Goals

  • Learn how to move a file within a repository.

Move the hello.rb file into a lib directory.

We are now going to build up the structure of our little repository. Let’s move the program into a lib directory.

Execute:

  1. mkdir lib
  2. git mv hello.rb lib
  3. git status

Output:

  1. $ mkdir lib
  2. $ git mv hello.rb lib
  3. $ git status
  4. On branch master
  5. Changes to be committed:
  6. (use "git reset HEAD <file>..." to unstage)
  7. renamed: hello.rb -> lib/hello.rb

By using git to do the move, we inform git of 2 things

  1. That the file hello.rb has been deleted.
  2. The file lib/hello.rb has been created.

Both of these bits of information are immediately staged and ready to be committed. The git status command reports that the file has been moved.

Another way of moving files

One of the nice things about git is that you can forget about source control until the point you are ready to start committing code. What would happen if we used the operating system command to move the file instead of the git command?

It turns out the following set of commands is identical to what we just did. It’s a bit more work, but the result is the same.

We could have done:

  1. mkdir lib
  2. mv hello.rb lib
  3. git add lib/hello.rb
  4. git rm hello.rb

Commit the new directory

Let’s commit this move.

Execute:

  1. git commit -m "Moved hello.rb to lib"