World map

 

BM: Baca dalam Bahasa Melayu

World 1: Save Points

Remove and rename

You will learn: how to delete, untrack and rename files the Git way.

The problem

Four files wobble. Three of them glow: old-notes.txt, aboutme.html and debug.log.

Your diary needs a clean-up. An old notes file. A page with a clumsy name. A log you saved by mistake. How do you tell Git?

Watch

git rm old-notes.txt prints rm old-notes.txt. git status -s then shows a green D in front of old-notes.txt. Terminal output: $ git rm old-notes.txt rm 'old-notes.txt' $ git status -s D old-notes.txt

git rm deletes a file and puts the deletion in the box. Your next commit saves it as gone.

Try it

Delete old-notes.txt the Git way.

Working folder
  • Empty
Staging area
  • Empty
Repository
  • 3 save points
  • Ignore log files
  • Add diary pages
  • Add README

Predict

You run git rm --cached debug.log. Is debug.log still in your folder?

Pick one answer.

Watch

git rm --cached debug.log. Status shows a green D for debug.log, but ls still lists debug.log in the folder. Terminal output: $ git rm --cached debug.log rm 'debug.log' $ git status -s D debug.log $ ls aboutme.html debug.log index.html old-notes.txt README.md

git rm --cached means: stop tracking, but keep the file. Perfect for things you saved by mistake. Your .gitignore keeps it hidden after.

Watch

git mv aboutme.html about.html. Status shows R aboutme.html -> about.html in green. Terminal output: $ git mv aboutme.html about.html $ git status -s R aboutme.html -> about.html

git mv renames a file and tells Git in one step. The R in status means renamed.

Try it

Rename aboutme.html to about.html. Then commit it.

Working folder
  • Empty
Staging area
  • Empty
Repository
  • 3 save points
  • Ignore log files
  • Add diary pages
  • Add README
6d3a9b1abe5c276bfa053

You did it!

You can now delete, untrack and rename files without confusing Git.

Go deeper (optional)

Deleted or renamed a file without Git? No problem. Run git add -A. Git puts the deletion in the box. It usually spots a rename too. For a whole folder, use git rm -r and the folder name.