Create, rename, move, delete
Beginner Git CLI VS Code UI IntelliJ UI GitLab UI GitHub UI
Why this matters
Documentation work is mostly moving things around: a new page, a renamed guide, an old file removed, a folder restructured. Git handles all of it, but it watches content, not your editor's actions, so a rename looks like a deletion plus a new file until you stage both. Knowing this makes reorganizations calm instead of alarming.
What Git notices
Git compares the working tree with the last commit. It notices:
- New file: exists on disk, not in the last commit → untracked until you
git addit. - Changed file: content differs → modified.
- Missing file: in the last commit, not on disk → deleted (not staged).
- Rename or move: a file disappeared and a new one with the same (or nearly the same) content appeared → shown as a rename once both sides are staged.
Git does not notice empty folders at all: it tracks files, and a folder exists in history only because a file is inside it.
Renaming and moving
Rename in your editor or file manager, and look:
$ git statusOn branch main
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: docs/notes.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
docs/scratch.mdStage the folder (or both files) and Git recognizes the rename:
$ git add docs/
$ git statusOn branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: docs/notes.md -> docs/scratch.mdgit mv does the rename and the staging in one step:
$ git mv docs/faq.md docs/questions.md
$ git status -sR docs/faq.md -> docs/questions.mdBoth routes give the same result. Moving to another folder is the same as renaming: git mv docs/faq.md docs/help/faq.md (the target folder must exist).
Deleting
Delete in the editor and Git reports deleted:; stage the deletion with git add <file> (yes, add stages deletions too) or use git rm, which removes the file from disk and stages the removal:
$ git rm docs/scratch.mdrm 'docs/scratch.md'$ git statusOn branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: docs/scratch.mdIf the file is already gone from disk, git rm <file> still works and stages the deletion. To stop tracking a file without deleting it (typically something that should have been ignored): git rm --cached <file>.
The deleted file is not lost: it is in every earlier commit, and lesson 12.5 brings it back.
Empty folders
$ mkdir docs/drafts
$ git status -sNothing. Git will not commit an empty folder. The convention is to put a placeholder file inside, usually named .gitkeep (any name works; .gitkeep signals intent):
$ touch docs/drafts/.gitkeep
$ git add docs/drafts/.gitkeepConversely, deleting the last file in a folder makes Git forget the folder; a colleague pulling the change sees the folder disappear.
In the tools
| Task | Command |
|---|---|
| New file | create it in the editor, then git add <file> |
| Rename or move | git mv <old> <new>, or rename in the editor and git add both paths |
| Delete | git rm <file>, or delete in the editor and git add <file> |
| Stop tracking, keep on disk | git rm --cached <file> |
| Whole folder | git add <folder>/, git rm -r <folder> |
| Undo a staged rename or delete before committing | git restore --staged <path> then git restore <path> |
Rename (F2), move (drag and drop) and delete (Delete) in the Explorer as usual. The Source Control view then shows the old path as D (deleted) and the new as U (untracked), or R once both are staged. Stage both with the + on each, or Stage All Changes; the commit will record a rename.
IntelliJ is smarter: Refactor → Rename (Shift+F6) or dragging a file in the Project view runs git mv for you when the file is tracked, and the Commit window shows it as a move. Deleting a file (Delete) asks whether to also remove it from Git; say yes. Creating a file offers to add it to Git; say yes for files that belong in the repository.
In the file view, Edit → Edit single file lets you change a file's path in the File path field at the top: that is a rename or move, committed directly to a branch. The Delete option is in the same menu. The Web IDE (several files at once) supports rename and delete from the file tree. Each action creates a commit on the branch you choose; for anything beyond a one-file fix, do it locally.
Open the file, click the pencil, and edit the file name at the top of the editor, including folder parts (typing help/faq.md moves it). The … menu offers Delete file. GitHub commits directly or proposes a new branch and pull request.
Common mistakes
- Renaming a file and only staging the new name. The commit adds a file and leaves the old one "deleted, not staged". Stage both paths, or use
git mv. - Committing a rename together with a rewrite. Two commits.
- Creating an empty folder and expecting colleagues to see it. Add a
.gitkeep. git rmon a file with uncommitted edits. Git refuses (has local modifications); decide whether to commit or discard those edits first.- Renaming only the letter case (
Readme.md→README.md) on macOS or Windows, where the file system ignores case. Usegit mv Readme.md README.md; Git handles it even where the OS pretends nothing changed.
Try it yourself
Goal: move a guide into a subfolder and delete a scratch file, ending with two readable commits.
- In the playground:
mkdir docs/help, thengit mv docs/faq.md docs/help/faq.md, thengit status -s. - Commit:
git commit -m "docs: move FAQ into docs/help". - Create
docs/scratch.mdwith a line,git addand commit it, then remove it withgit rm docs/scratch.mdand commit again. - Run
git log --oneline -3andgit log --follow --oneline docs/help/faq.md.
Expected result: status shows R docs/faq.md -> docs/help/faq.md; the log shows three new commits; --follow lists the FAQ's history including commits made when it was still docs/faq.md.
Show solution
git mv needs the target folder to exist, hence the mkdir. --follow tells git log to keep tracing the file across renames. To put the FAQ back where the other lessons expect it: git mv docs/help/faq.md docs/faq.md && git commit -m "docs: move FAQ back", or reset the playground.