Git File Operations - 14 days of Git
So far on my 14 days of Git learning journey I have dug into:
- What is version control?
- What is Git?
- Installing Git
- Basic Git commands to get started
- Inspect a Git repository
Now I want to look at file operations with Git.
File Operations with Git
There are often times you need to rename a file or move it to another directory. An if you are using an integrated development environment (IDE) or graphical editor, those are simple operations. However, I wanted to see what options were available to me if I was using the console and if Git can help do those types of operations.
Rename a file
Git mv is the command that can help you with renaming. We do need to understand what this command does though.
Git mv is the equivalent to the following three commands:
- mv old_file.md new_file.md
- git add new_file.md
- git rm old_file.md
Let's break that down a little.
The mv command is a Unix/Linux command that is used to change the file name. The git add command is used to stage the new version of the file. The last part git rm removes the old file from being tracked.
Ultimately using git mv simplifies the renaming of a file, but it's good to understand what it is doing under the hood.
Let's see it in action.
I have a file called "blogimage.png" I want to rename. Within my command line I type:
git mv blogimage.png generic-blogimage.png
I can then do a git status to check what has happened and we can see that the change is waiting to be committed.
Moving a file
Git mv is also the command I can use to move a file from one location to another within my repository.
I want to move that image file from the root directory to a directory I have for images. To do that I can type the command:
git mv .\generic-blogimage.png .\images\generic-blogimage.png
That moves the file for me and stages the change ready to be committed.
Let's take a closer look at what other options we have using git mv.
Git mv options
Git mv is a great command on its own but if we look at the documentation there are some options, we can use with the command to help it do what we really want it to.
Git mv -f
With the -f option we can tell Git we are okay to overwriting the destination with our new file. It's basically forcing any renaming or move you want to happen. Be cautious with this as you could overwrite something you need.
Git mv -k
The -k option allows Git to skip over any erroneous conditions resulting from a git mv call. For example, if you are trying to move a file to another location and that file already exists the command would error out. If you don't want to see that error and have Git move onto your next instruction you should use the -k option.
Git mv -n
The -n option is actually short for --dry-run. It won't actually carry out the move or rename, it will just show you what would happen if you did perform the command.
Git mv -v
The last option is the verbose option. Using this option will give you more information and feedback when you execute the command.
14 days of Git
It's been an interesting day learning about the git mv command. I mostly do file renaming within the editor or even file explorer, but it's great to learn how it can be done with a command.
The next step in my 14 days of Git learning journey is to look at undoing commits and changes, which should be interesting! Be sure to subscribe and join us for that step in the learning journey!
You can follow along here: https://github.com/weeyin83/14daysofgit