Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Just as the "git rm" command lets you remove files from a repo, the "git mv" command lets you move (or rename) files within a repo.
- We just created an HTML file for our silver medals page, here in
silver.txt
. - Let's stage the file for committing:
git add silver.txt
- And then we'll commit it:
git commit -m "Add silver medals"
- Unfortunately, only now do we realize that we needed that file name to be saved with a
.html
extension, not a.txt
extension. - But we've already committed the file under the wrong name! How can we fix this?
The git mv
command
- Git offers the
git mv
command to let you move files around. - After you type
git mv
, you need to provide the name of the file you want to move,silver.txt
, and the file name you want to move it to,silver.html
.
git mv silver.txt silver.html
- If we run
ls
now, we'll see that thesilver.txt
file has been moved tosilver.html
in our working directory. - Now let's try running
git status
... - We'll see "renamed: silver.txt -> silver.html" in the "Changes to be committed" section.
- That means the file renaming has been staged.
- Now we just need to commit the change:
git commit -m "Rename silver.txt to silver.html"
$ git mv silver.txt silver.html
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: silver.txt -> silver.html
#
$ git commit -m "Rename silver.txt to silver.html"
[master 9505066] Rename silver.txt to silver.html
1 file changed, 0 insertions(+), 0 deletions(-)
rename silver.txt => silver.html (100%)
-
0:00
We just created an HTML file for our silver medals page, here in silver.txt.
-
0:06
With our work saved, we can close our editor.
-
0:09
Let's click back in the console,
-
0:11
make sure that we're in our repository directory, cd medals.
-
0:15
And let's save the file for committing, git add silver.txt.
-
0:20
And then, we'll commit it, git commit -m "Add silver medals".
-
0:30
Unfortunately, only now do we realize that we needed that file name
-
0:35
to be saved with a .html extension, not a .txt extension.
-
0:40
But we've already committed the file under the wrong name.
-
0:42
How can we fix this?
-
0:44
Git offers the git move command to let you move files around.
-
0:48
It's typed as git mv, but the mv is pronounced as move.
-
0:54
Like git rm, the name and syntax of the git mv command
-
0:58
is based on an important Unix command that does the same thing, but outside of git.
-
1:03
After you type git mv, you need to provide the name of the file you want to move,
-
1:08
silver.txt, and the name of the file you want to move it to, silver.html.
-
1:12
Hit Enter to run the command.
-
1:16
There won't be any output.
-
1:17
As usual, that's a good thing.
-
1:19
It means the command worked.
-
1:21
If we run ls now, we'll see that the silver.txt file
-
1:25
has been moved to silver.html in our working directory.
-
1:29
Now, let's try running git status.
-
1:32
We'll see, renamed,
-
1:34
silver.txt to silver.html in the changes to be committed section.
-
1:40
That means the file renaming has been staged.
-
1:43
Now, we just need to commit the change,
-
1:49
git commit -m "Rename silver.txt to silver.html".
-
1:58
Let's run git status again.
-
2:02
And we'll see there are no uncommitted changes.
-
2:05
If we run ls, we'll see the file is still named silver.html.
-
2:08
And when our git repository is cloned,
-
2:11
that's the file name that will be in the clone as well.
You need to sign up for Treehouse in order to download course files.
Sign up