Lesson 6 of 7 in Basics

Ignoring, moving and removing files

Story: Pim keeps the archive lean: nothing goes on record that doesn't belong there, and nothing stays that's done with.

Some files should never go into a commit: logs a tool writes while it runs, backup copies your editor makes, passwords. And some files you've already committed need a new name, a new folder, or no place in the project at all. This Lesson covers all of these.

Ignoring files: .gitignore

A file called .gitignore tells git which untracked files to leave alone. Each line is a pattern:

*.log
backups/
  • * matches any run of characters, so *.log matches every file whose name ends in .log, in this folder and in every folder inside it.
  • A pattern ending in / matches a folder, and everything in it.
  • A line starting with # is a comment.

git leaves ignored files out of git status, and git add . skips them. If you name an ignored file in git add, git refuses and tells you it's ignored.

Write the patterns with echo or edit. Put a pattern with a * in quotes, as in echo "*.log" > .gitignore: on a real computer, the shell would otherwise swap *.log for the names of the matching files. Then commit .gitignore like any other file, so the rules stay with the project.

.gitignore only affects files git doesn't track yet. A file that's already in a commit stays tracked, even when a pattern matches it.

Removing a file: git rm

git rm <file> deletes the file and stages the deletion, so your next commit no longer has it:

$ git rm style.css
rm 'style.css'

The commits you made before still have the file. git rm refuses to delete a file with changes you haven't committed, so you can't lose them by accident.

Untracking a file but keeping it: git rm --cached

git rm --cached <file> takes the file out of the staging area only. Your copy stays in the working tree, and git now lists it as untracked. Name it in .gitignore too, so it stays out of git status and git add . from then on.

That's how you stop tracking a file you committed by mistake. The commits you made before still have it, though: if it held a password, change the password.

Moving and renaming: git mv

git mv <old> <new> renames a file and stages the change. git status shows it as a rename:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	renamed:    style.css -> site.css

To move a file into a folder, the folder must exist: make it with mkdir first, then git mv about.html pages/. If the folder is missing, git mv about.html pages/ fails, and git mv about.html pages renames the file to pages.

A commit holds files, not how they got there. When a file disappears and a new one with the same content appears, git calls it a rename. So you can also move a file by hand: write the new file, git rm the old one, and git add the new one. The commit is the same.

Command What it does
.gitignore A file of patterns: untracked files that match stay out of git status and git add ..
git rm <file> Deletes the file and stages the deletion.
git rm --cached <file> Stops tracking the file, but keeps it in your working tree.
git mv <old> <new> Renames or moves the file, and stages the change.

New commands

  • git rm
  • git rm --cached
  • git mv

Explainer: Leaving files out, moving them and removing them

project: your repo, you are here37d7cf7 Add a README37d7cf7ab89288 Add the home pageab89288a4b8d97 Style the headinga4b8d97ebe8093 Add a draft page and the server logebe8093be9da18 Stop tracking server.logbe9da189edb082 Rename the stylesheet and delete the draft9edb082HEAD → main
The repository at the end of the Explainer.
Graph as text

project: your repo, you are here

  • HEAD → main at 9edb082 "Rename the stylesheet and delete the draft"

Branches and tags

  • main at 9edb082 "Rename the stylesheet and delete the draft"

Commits, newest first

  1. 9edb082 "Rename the stylesheet and delete the draft"; parent be9da18; refs: HEAD → main
  2. be9da18 "Stop tracking server.log"; parent ebe8093
  3. ebe8093 "Add a draft page and the server log"; parent a4b8d97
  4. a4b8d97 "Style the heading"; parent ab89288
  5. ab89288 "Add the home page"; parent 37d7cf7
  6. 37d7cf7 "Add a README"; no parent

Challenge 1 of 4

Story: Scratch paper doesn't go in the archive, Agent. Tell git to look the other way.

A tool you ran wrote two log files into your project, server.log and error.log. Make git ignore them, and commit that rule so it stays with the project. Keep the log files in your folder.

Commands 0 of Par 3

Goal

  • Met: HEAD is on branch main
  • Not met yet: Branch main is at the goal's commit
  • Not met yet: The history of main matches the goal
  • Not met yet: The working tree matches the goal
  • Not met yet: main has one new commit
  • Not met yet: The new commit adds a .gitignore for the logs
  • Met: The new commit leaves server.log out
  • Met: The new commit leaves error.log out
  • Not met yet: git status lists nothing: both logs are ignored, and the rule is committed

Hints

Live Graph

project: your repo, you are here37d7cf7 Add a README37d7cf7ab89288 Add the home pageab89288a4b8d97 Style the headinga4b8d97HEAD → main

Terminal

~/project (main) $