Renaming and Removing Files

So far we have only added and changed files in our version controlled working directory, and Git was clever enough to work out what it needed to do. However, Git does not know how to automatically work out when you have renamed or removed files.

Renaming Files

To rename a file, you have to use git mv. This is almost identical to the normal UNIX renaming (moving) command, except you have to add git in front. First, make sure you have attached the HEAD to the latest commit of the master branch. Do this by typing

git checkout master

A quick check of git status should show you that you are on branch master and the current working directory is clean.

We are going to rename the file something.md to background.md. To do this, type

git mv something.md background.md

If you now type ls you should see that something.md has been renamed (moved) to background.md.

Now, git status should show something like

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   renamed:    something.md -> background.md
#

To commit this change, use git commit -a as normal, remembering to add a suitable commit message, e.g.

git commit -a
Have renamed something.md to background.md to better reflect
the contents of the file

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   renamed:    something.md -> background.md
#

A quick check of git status should then show that your working directory is clean.

Removing files

To remove a file use git rm. This is almost the same as the normal UNIX remove command, except that you add git in front.

To remove the file background.md we just need to type

git rm background.md

If you now type ls you should see that background.md has been removed.

Now git status should show something like

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    background.md
#

Commit the change using git commit -a, ensuring that you add in a suitable commit message, e.g.

git commit -a
Removed background.md as it is no longer needed for this project.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    background.md
#

A quick check of git status should show you that your current working directory is clean.


Exercise

Change into your my-branch branch. When you type ls you should see that something.md and something-else.md are now back in your working directory.

Remove something.md using git rm and rename something-else.md to project.md using git mv. Use git commit -a to commit both changes in a single commit (i.e. use git commit just once, after running both git rm and git mv). Ensure you give a good commit message.

Use git status to double check that your working directory is clean after the commit.


Previous Up Next