Git - Moving Files

From EdWiki

GitLab - Moving Files

  • Goals
    Learn how to move a file within a repository.
  • Move the hello.c file into a src directory.

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

mkdir src
git mv hello.c src
git status

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

  1. That the file hello.c has been deleted.
  2. The file src/hello.c 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:

mkdir src
mv hello.c src
git add src/hello.c
git rm hello.c
  • Commit the new directory
    Let’s commit this move.
git commit -m "Moved hello.c to src"