Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie we're going to talk about how to move and rename files. And like we just saw with deleting files, there's two ways that we can do it. The first, is that we can just go into the regular operating system, and do all of our moving and renaming, and then come back and tell Git about those changes, and tell it to stage those changes. The second, is that we can do it from Git, and let Git handle working in the operating system for us, just like it did with delete. So let's look at both of these. First, let's look at renaming. So, let's say we've got first_file.txt.
Let's go in here, and let's just change it to primary_file.txt. So, we've given it a rename, and we did it from the file system. Let's switch back over to Git and ask Git what's the status now. Notice that it didn't tell us that it was a renamed file, it tells us that it was a file that was deleted, and then, there is a new file that's untracked. That's how it sees what happened. It sees the fact that the file that it was expecting to find is not there, and now I have a new file there instead.
Let's try adding those changes to the staging index, git add primary_file, that's the new one, and git remove first_file.txt. So now, it has both of those changes in its staging index, git status. But now, it says ah, I noticed that now they're renamed. So, once it gets to the staging index, it actually compares the data between the two and says oh, these are pretty close. It doesn't have to be exactly the same, some changes could have taken place at the same time.
I think the threshold is about 50%. As long as the data is about 50% the same between the two of them, it says oh, okay, this is the same file. So you've renamed it from first_file.txt to primary_file.txt. And now, of course, we know how we can commit that change just using a regular commit. Let's take a look at how we're going to have Git do the process for us. So, if we do another rename, let's do git--and a rename and a move are the same thing in Git. It's similar to the way it works in Unix. So, if we want to rename the second file as secondary file, let's git move second_file.txt to secondary_file.txt.
So, we're moving second_file.txt to secondary_file.txt, which is essentially the same thing as a rename. We tell Git to do it though. If we come over here, and we look in the operating system, you see that it took care of changing it for us there. And if we do git status, you'll notice that it went ahead and added it to the staging index for us just like delete did. It said, all right, I'm going to change it in the file system, and add it to the staging index all in one step. So, I think the lesson from both deleting and the moving and renaming is that it's easier to have Git handle it because Git will take care of adding it to the staging index for you right away.
Now, if you're doing lots and lots of changes and moving things around in the file system, it maybe better for you to go there first and come back and deal with git later. But if you really are just changing one thing, a lot of times it's better just do it here and let Git go ahead and add it to the staging index for you. Now, I said moving was the same process, we don't have anywhere that we can actually move things to yet. Let's create a new folder here, and let's call this first_directory, and let's move our third file into that directory.
So let's do git move, and we're going to do third_file.txt, and we're going to move it into first_directory/third_file.txt. Now, we can rename it at the same time, because like I said, move and rename are basically the same thing, and now it's moved it there, git status, and you can see that it tells us that it's been renamed, but the rename was actually moving it to this new location. So like I said, move and rename are synonymous, most of the time it's better to do your moving and renaming from inside of Git instead of from the file system.
Let's go ahead last of all, and let's commit those, commit, and let's say Reorganized file structure by moving files. There we are. And you can see that it lists off those renames there, the git status shows us that our working directory is clean, and our working directory is now the same state as our repository.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105433 Viewers
56 Video lessons · 117121 Viewers
71 Video lessons · 86347 Viewers
131 Video lessons · 41262 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.