Level 16

Remove & rename

Remove a file Git tracks, rename another, and commit both so the history records them.

Welcome to THE FINAL LEVEL. In this last huzzah you will remove a file from your repository and rename another one.

At the start of this journey you REALLY needed hello.py. Now that you are a total Git pro, you have no more use for it. This happens regularly in software projects: files end up no longer needed, and removing them keeps the codebase neat and tidy.

Step 1 of 7
0 xp

Tell Git about it

So can't you just delete files willy-nilly, without telling Git? NO. If you delete a tracked file by hand, Git WILL notice, since it keeps track of every tracked file in the working directory. The key is to tell Git about the deletion so it becomes part of the history.

git rm <filename>

That deletes the file AND stages the deletion in one fell swoop.

Remove a tracked file

Remove it:

Type this into the terminal below
git rm hello.py

Good work. git rm deleted hello.py from your table. So you are done now, right? WRONG.

git rm is similar to git add in that it stages a change for the next commit. The difference is that instead of staging a new or modified file, you are staging the deletion of a file. Look at the Staged files column: there it is.

caution Deletes 1 tracked file(s) from the working tree and index.

Your turn: record it

The deletion is staged. Which command turns it into a page in the book?

Renaming

Moving on to another common operation: renaming a file. Since Git tracks files by their names and paths, you must tell Git when you intend to rename a tracked file:

git mv <old-name> <new-name>

For practice, rename brew.py to coffee.py. Every earlier page still holds hello.py, by the way. It has not vanished from history, only from the table and from new pages.

Rename a tracked file

Rename it:

Type this into the terminal below
git mv brew.py coffee.py

git mv took care of both renaming the file and telling Git about it. The rename is staged, and it needs committing just like the deletion did.

safe 'mv' renames tracked files (content kept, recorded in the index).

Commit the rename

Commit the rename:

Type this into the terminal below
git commit -m "Rename brew.py to coffee.py"

As you probably guessed, committing a rename also results in a brand new page at the front of the active branch. Hover it to see how Git records the change: brew.py became coffee.py.

safe Creates a new commit; nothing at risk.

Nothing is really lost

Here is a cool detail. If you rename a file on your operating system, forget to tell Git, and then stage the new name with git add, Git is smart enough to detect the rename anyway. It compares the content of the old file and the new one, and if they match closely enough it treats the operation as a rename.

And if you delete or rename a file using Git, any earlier page still holds the old version. Open the book to a commit before the change, and there it is. So do not be too afraid of losing your work. Git keeps every version of every file safe and sound in the history.

git-sim
Initial CommitDevlands

The graph appears here when the first command runs. The simulations are drawn by git-sim from a real repository.

The scrapbook · one picture for every Git idea

The scrapbook picture appears as the story is told.