Various fixes for outside-of-Git directory reorganisations

23 minute read

Moving files around outside of Git in a Git-managed project will cause Git and humans to get confused. Why don’t my new files show up? Why are some files deleted? What did I do? If that happens, how do you tell Git about the file reorganisation and get things back on track? Prompted by a recent question about this, I thought I’d write up my approach, focusing on command line solutions.

Geordi La Forge (from Star Trek) meme avoiding `mv` and preferring `git mv`.
Use git mv instead of mv when reorganising files and directories in Git.

Update 2026-11-03

I mentioned at the end of this post that I had the nagging feeling there was an easier way. It turns out, yes there is. And the solution isn’t worth spending so many words on!

Not long after I published the post, I was contacted on Mastodon by @khleedril who described a much simpler solution, which boils down to:

$ git add <new-directory-name>
$ git commit -a

The git commit -a finds the deleted files and Git works out that they were moved. Quick and simple. No need to run git rm, and no need to juggle grep, cut, and xargs!

It looks like I took myself on a wild goose chase and in the end couldn’t see the wood for the trees! Oops!

Anyway, if you’re curious about the original, much more complex solution, continue reading. If not, this isn’t the post you were looking for. Move along.


Original post below


Consider this: you’re working on a project, and it’s slowly growing. Over time, you have files related to different topics, modules, namespaces, etc. Eventually, you’ll want to reorganise things to group related files together. Thus, you’ll want to put those files into subdirectories. If you manage your project with Git, you should use git mv to move files and directories. But what if you forget and simply move the files around as usual? Git will think that you’ve deleted a whole lot of files, whereas really you’ve moved and/or renamed them. Or, at least, that’s what you intended to do. How to fix the situation?

A friend asked me this question recently. My answer involved using Linux shell utilities, which wasn’t overly user-friendly, but certainly did the job. Being someone who likes to think about the various ways to solve a problem, I wondered how to perform the same task in other environments and operating systems.

This article describes the details of my command line-driven approach, which I then adapt to PowerShell. Ultimately, I end up wondering if there’s an easier, more elegant, and possibly more user-friendly way to repair such situations.

Setting the stage

To make this more concrete, let’s consider a reorganisation that I wanted to make in the Git course that I’m currently rebuilding. I’ve got a whole lot of figures related to a wedding photographer analogy that I’m using to explain what Git’s staging area is and why it can be useful. The files for these figures are currently clogging up the figures/ directory in the project. They’re located with other, unrelated files in that folder and are making the directory listing much longer than it needs to be. Bundling the files together into a well-named directory would improve the project structure and keep topics logically separate.

The right way to reorganise things from the get-go would be to create the directory and then use git mv to put everything where I want it. E.g.:

$ mkdir wedding-photographer-analogy
$ git mv <files> wedding-photographer-analogy/

Using git mv to move the files ensures that Git tracks this change to the project structure.

However, a common mistake people make is to simply move the files without using Git. And that’s completely understandable: after all, most of the time, you don’t need to use Git to move stuff around. The problem is, inside a Git-managed project, you do. This isn’t obvious to Git newcomers.

Either way, the effect is the same: Git sees deleted files and has no idea about their presence in the new location. How do we fix up a bungled bulk move and tell Git what really happened? That’s what this post is about. But before we can fix things, first we have to break them.

Let’s illustrate the problem by making the mistake in an example project.

Returning to the Git course situation I mentioned above,1 if we change into the figures/ directory and list its contents, we see lots of files:

$ ls *.tex
add-bridepair-wedding-scene.tex            initial-wedding-scene.tex
add-parents-bridepair-wedding-scene.tex    merge_simple_branch_tikz.tex
add-witnesses-bridepair-wedding-scene.tex  pull-request-from-fork.tex
bridepair-parents-wedding-scene.tex        pull-request-process.tex
bridepair-wedding-scene.tex                push-pr-branch-to-fork.tex
bridepair-witnesses-wedding-scene.tex      push-to-git-service.tex
CentralRepo_tikz.tex                       remove-parents-bridepair-wedding-scene.tex
clone-forked-repository.tex                simple_branch_tikz.tex
fork-upstream-repository.tex               wedding-scene-preamble.tex
github-like-pull-request-model.tex         working_dir_staging_repo.tex

(Things are in flux in this project at present, so it’s a bit chaotic right now).

Only a subset of these files relate to the wedding photographer analogy topic that I discuss in the course. Let’s create a directory to contain this subset:

$ mkdir wedding-photographer-analogy

To show the problem that appears if we don’t use Git to move the files, let’s only use mv to put the files into their new location, i.e.:

$ mv <files> wedding-photographer-analogy/

We can see the effect that this has by looking at the output of git status:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

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:    Makefile
        deleted:    add-bridepair-wedding-scene.tex
        deleted:    add-parents-bridepair-wedding-scene.tex
        deleted:    add-witnesses-bridepair-wedding-scene.tex
        deleted:    bridepair-parents-wedding-scene.tex
        deleted:    bridepair-wedding-scene.tex
        deleted:    bridepair-witnesses-wedding-scene.tex
        deleted:    initial-wedding-scene.tex
        deleted:    remove-parents-bridepair-wedding-scene.tex
        deleted:    wedding-scene-preamble.tex

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        wedding-photographer-analogy/

no changes added to commit (use "git add" and/or "git commit -a")

Oops! That doesn’t look right! We didn’t want to delete any files!

Ok, so, how to fix this?

Broadly speaking, our task is to tell Git to track the files in their new location and remove the tracking info it had about their old location. As with many things in life, there’s more than one way to do it.

Doing things the hard way

Of course, there’s always brute force. This is the simple (but hard) solution. For each file we moved, we need to run git add wedding-photographer-analogy/<filename> to tell Git to track the file in the new location. Then we need to run git rm <filename> to tell Git to stop tracking the file in the old location. Doing this for all files is a lot of repetitive work, and due to that repetitive nature, it’s boring and potentially error-prone. It’ll get the job done, but it doesn’t scale well.

However, if you know your way around the command line on Unix/Linux, there are better ways.

Bashing our way to a solution

I’ve been banging on keyboards attached to Unix-like systems since the late 90s.2 Hence, my gut instinct is to use the command line when solving these kinds of problems. Given this perspective, here’s what I’d do from within a Bash-like shell.

First up, I’d use git status -s to get more structured information. The -s option to git status gives output in the “short” format, which is easier to slice and dice with standard Unix shell tools than the more detailed “long” format. From that, I can extract the names of the files that Git thinks have been deleted. Then I can use this list of files to add the files’ new location and remove the references to their old location.

Running git status -s, we get:

$ git status -s
 D Makefile
 D add-bridepair-wedding-scene.tex
 D add-parents-bridepair-wedding-scene.tex
 D add-witnesses-bridepair-wedding-scene.tex
 D bridepair-parents-wedding-scene.tex
 D bridepair-wedding-scene.tex
 D bridepair-witnesses-wedding-scene.tex
 D initial-wedding-scene.tex
 D remove-parents-bridepair-wedding-scene.tex
 D wedding-scene-preamble.tex
?? wedding-photographer-analogy/

In this output, the D tells us that the file was deleted from the working copy (a.k.a. working tree or working directory). Since Git doesn’t know anything about the wedding-photographer-analogy/ directory, it’s untracked, and hence its entry shows two question marks (??).

Why two question marks? Well, it’s not immediately obvious, but the git status -s output actually begins with two columns of information. The first column tells us the file’s state in the index,3 the second column tells us the file’s state in the working copy. Since Git has no information in either of these locations, it shows a question mark in the first two columns.

This also explains why there’s a space before the D: it’s an entry in the second column. I.e. the files have been deleted from the working copy, but not from the index. Hence the first column in the output is empty.

What’s great about this output is we can grep for ' D' (note the leading space) to get the list of deleted files and then use cut to extract a list of their names. In other words:

$ git status -s | grep ' D' | cut -d' ' -f3
Makefile
add-bridepair-wedding-scene.tex
add-parents-bridepair-wedding-scene.tex
add-witnesses-bridepair-wedding-scene.tex
bridepair-parents-wedding-scene.tex
bridepair-wedding-scene.tex
bridepair-witnesses-wedding-scene.tex
initial-wedding-scene.tex
remove-parents-bridepair-wedding-scene.tex
wedding-scene-preamble.tex

Since we want to refer to this list again later, it’s handy to assign it to a variable:

$ moved_files=$(git status -s | grep ' D' | cut -d' ' -f3)

Now we can iterate over this list, using git add to tell Git about the files’ presence in the new directory. Later, we’ll use git rm to remove Git’s references to the original files.

One way to add the files in their new location would be with a for loop:4

$ for file in $moved_files
do
git add wedding-photographer-analogy/$file
done

Looking at the repository status, we see that Git now knows about the files in their new location:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   wedding-photographer-analogy/Makefile
        new file:   wedding-photographer-analogy/add-bridepair-wedding-scene.tex
        new file:   wedding-photographer-analogy/add-parents-bridepair-wedding-scene.tex
        new file:   wedding-photographer-analogy/add-witnesses-bridepair-wedding-scene.tex
        new file:   wedding-photographer-analogy/bridepair-parents-wedding-scene.tex
        new file:   wedding-photographer-analogy/bridepair-wedding-scene.tex
        new file:   wedding-photographer-analogy/bridepair-witnesses-wedding-scene.tex
        new file:   wedding-photographer-analogy/initial-wedding-scene.tex
        new file:   wedding-photographer-analogy/remove-parents-bridepair-wedding-scene.tex
        new file:   wedding-photographer-analogy/wedding-scene-preamble.tex

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:    Makefile
        deleted:    add-bridepair-wedding-scene.tex
        deleted:    add-parents-bridepair-wedding-scene.tex
        deleted:    add-witnesses-bridepair-wedding-scene.tex
        deleted:    bridepair-parents-wedding-scene.tex
        deleted:    bridepair-wedding-scene.tex
        deleted:    bridepair-witnesses-wedding-scene.tex
        deleted:    initial-wedding-scene.tex
        deleted:    remove-parents-bridepair-wedding-scene.tex
        deleted:    wedding-scene-preamble.tex

Great! That’s the first step in the process done.

If you want to be a bit tricky, it’s also possible to avoid the for loop by using xargs instead, giving this one-liner:

$ git status -s | grep ' D' | cut -d' ' -f3 | xargs -I{} bash -c 'git add wedding-photographer-analogy/{}'

The {} part in the call to xargs is a placeholder for the current list item, and in the context here represents one of the deleted filenames. Thus, xargs replaces {} in the bash command with a string for each filename in the list that cut passed to it.

You don’t have to use two characters as a placeholder, though. I’ve seen other code online where this was a single character, such as %. That makes the call above look like this:

$ git status -s | grep ' D' | cut -d' ' -f3 | xargs -I% bash -c 'git add wedding-photographer-analogy/%'

That variation works the same as the version using {}.

I think the reason that the {} syntax is more common is that {} is often used in find for a similar purpose. Also, xargs is often used in combination with find, so it makes sense to reuse the concept in such situations.

Now that we’ve told Git about the files in their new location, we need to tell it to stop tracking the files in their old location. We do this by using git rm, which will remove the tracking information Git has about these files. Once we’ve done this, Git is clever enough to work out that the files have actually been moved (it’ll say “renamed”, but it means the same thing).

It’s important to realise that even though the files no longer exist at their original location on the filesystem, running git rm with those names won’t throw an error. Git simply does the Right Thing™. Normally, when deleting a file, Git will remove both its reference to the file and the file itself. In our case here, because the file’s already gone from the filesystem, Git doesn’t need to worry about the extra deletion step.

Ok, getting back to the task at hand, we now run git rm on the list of moved files:

$ git rm $moved_files
rm 'git/figures/Makefile'
rm 'git/figures/add-bridepair-wedding-scene.tex'
rm 'git/figures/add-parents-bridepair-wedding-scene.tex'
rm 'git/figures/add-witnesses-bridepair-wedding-scene.tex'
rm 'git/figures/bridepair-parents-wedding-scene.tex'
rm 'git/figures/bridepair-wedding-scene.tex'
rm 'git/figures/bridepair-witnesses-wedding-scene.tex'
rm 'git/figures/initial-wedding-scene.tex'
rm 'git/figures/remove-parents-bridepair-wedding-scene.tex'
rm 'git/figures/wedding-scene-preamble.tex'

Here we see Git reporting that it’s removing the files, even though they don’t exist on the filesystem. While this may seem confusing, Git is really only informing us that it is removing the tracking information for each file.

Now git status shows us what we expect: the files were moved into the new directory. Since moving and renaming are (for the computer) the same operation, Git marks the staged changes as renamed.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    Makefile -> wedding-photographer-analogy/Makefile
        renamed:    add-bridepair-wedding-scene.tex -> wedding-photographer-analogy/add-bridepair-wedding-scene.tex
        renamed:    add-parents-bridepair-wedding-scene.tex -> wedding-photographer-analogy/add-parents-bridepair-wedding-scene.tex
        renamed:    add-witnesses-bridepair-wedding-scene.tex -> wedding-photographer-analogy/add-witnesses-bridepair-wedding-scene.tex
        renamed:    bridepair-parents-wedding-scene.tex -> wedding-photographer-analogy/bridepair-parents-wedding-scene.tex
        renamed:    bridepair-wedding-scene.tex -> wedding-photographer-analogy/bridepair-wedding-scene.tex
        renamed:    bridepair-witnesses-wedding-scene.tex -> wedding-photographer-analogy/bridepair-witnesses-wedding-scene.tex
        renamed:    initial-wedding-scene.tex -> wedding-photographer-analogy/initial-wedding-scene.tex
        renamed:    remove-parents-bridepair-wedding-scene.tex -> wedding-photographer-analogy/remove-parents-bridepair-wedding-scene.tex
        renamed:    wedding-scene-preamble.tex -> wedding-photographer-analogy/wedding-scene-preamble.tex

In the discussion above, I showed the add and rm steps separately so we could see the details of the effect of each step. However, that’s not what you’d do in practice. Normally, we’d run both commands, e.g. as part of a for loop:

$ for file in $moved_files
do
git add wedding-photographer-analogy/$file
git rm $file
done

Similarly, our one-liner from earlier can also run both add and rm steps in the xargs call:

$ git status -s | grep ' D' | cut -d' ' -f3 | xargs -I{} bash -c 'git add wedding-photographer-analogy/{}; git rm {}'
rm 'git/figures/Makefile'
rm 'git/figures/add-bridepair-wedding-scene.tex'
rm 'git/figures/add-parents-bridepair-wedding-scene.tex'
rm 'git/figures/add-witnesses-bridepair-wedding-scene.tex'
rm 'git/figures/bridepair-parents-wedding-scene.tex'
rm 'git/figures/bridepair-wedding-scene.tex'
rm 'git/figures/bridepair-witnesses-wedding-scene.tex'
rm 'git/figures/initial-wedding-scene.tex'
rm 'git/figures/remove-parents-bridepair-wedding-scene.tex'
rm 'git/figures/wedding-scene-preamble.tex'

Again, Git reports that it’s removing files, but it’s really only the tracking information that is being deleted.

So, whether you choose to iterate over the list of files with a for loop or string everything together in a series of pipes in a one-liner depends upon which technique feels most comfortable or logical to you.

With our staging area now in the correct state, we can commit the change and continue with our day:

$ git commit
[master 4e54f91] Move wedding photographer analogy figures into their own dir
 10 files changed, 0 insertions(+), 0 deletions(-)
 rename git/figures/{ => wedding-photographer-analogy}/Makefile (100%)
 rename git/figures/{ => wedding-photographer-analogy}/add-bridepair-wedding-scene.tex (100%)
 rename git/figures/{ => wedding-photographer-analogy}/add-parents-bridepair-wedding-scene.tex (100%)
 rename git/figures/{ => wedding-photographer-analogy}/add-witnesses-bridepair-wedding-scene.tex (100%)
 rename git/figures/{ => wedding-photographer-analogy}/bridepair-parents-wedding-scene.tex (100%)
 rename git/figures/{ => wedding-photographer-analogy}/bridepair-wedding-scene.tex (100%)
 rename git/figures/{ => wedding-photographer-analogy}/bridepair-witnesses-wedding-scene.tex (100%)
 rename git/figures/{ => wedding-photographer-analogy}/initial-wedding-scene.tex (100%)
 rename git/figures/{ => wedding-photographer-analogy}/remove-parents-bridepair-wedding-scene.tex (100%)
 rename git/figures/{ => wedding-photographer-analogy}/wedding-scene-preamble.tex (100%)

I actually wrote a longer commit message expanding upon why this change was necessary. The extra context doesn’t appear here because Git only shows the commit’s subject line when committing.

Great! All done!

Considering other systems

So, that’s how I’d do things on the Linux/Unix command line with Bash or a Bash-equivalent shell. The question now becomes: how to do the same thing on other systems?

Well, since macOS is a BSD, and hence a Unix system, the above shell incantations should work fine there. Unfortunately, I don’t have a system on which I can test this, but I’m very sure you’ll be ok.

Similarly, if you’re using the Windows Subsystem for Linux (a.k.a. WSL), that quite literally is a Linux system, hence there’s no difference to what I showed above.

But, on plain Windows, there are more options than only WSL. It’s time to turn our attention to Git Bash, PowerShell, and SourceTree.

Windows

What about Windows? How do things work there? Since there are many ways to use Git on Windows, there is more than one answer to that question. Below is not an exhaustive list: for instance, I’ve not discussed systems such as TortoiseGit or looked into many different Git GUIs. Even so, I thought it’d be nice to show how to use Git Bash, PowerShell, and SourceTree to solve our bungled file reorganisation.

A bulk move with Windows Explorer

As with Linux, we need to set the stage before we can see how the different solutions work. To get things set up, I’ve cloned a copy of the Git in detail course onto my test Windows system. Here’s the figures/ folder open in Windows Explorer:

Windows Explorer view of list of files in figures/ directory

We now create the wedding-photographer-analogy folder:

Windows Explorer view of figures/ directory after subdirectory creation

and move the files from the figures/ folder into the subfolder we just created:

Windows Explorer view of figures/ directory after bulk file move into
subdirectory

Looking in our new subfolder, we see the files are there where we’d like to have them:

Windows Explorer view of new subdirectory after bulk file move

Nice! Everything set up again. :slightly_smiling_face:

Git Bash

So, what does Git Bash have to say about the situation? Starting the application, changing into the figures/ subdirectory and running git status, we see some familiar output:

Git Bash status output in figures/ directory after bulk file move

Since this is Bash, the standard Unix tools that we used above should also work here. Let’s try it out and see:

Git Bash output after running grep, cut, xargs one-liner

That looks pretty good. What’s the status now, Kenneth?

Git Bash status output in figures/ directory after xargs one-liner

We’ve seen that before! From Git’s point of view, the files have been moved (renamed) as we wanted in the beginning. Cool!

So, we now know that Git Bash on Windows works the same as normal Bash on Unix/Linux. Admittedly, that was to be expected, but it’s good to know we can use familiar Unix tools out of the box in a Windows environment.

PowerShell

PowerShell is a common sight these days on many Windows systems and, as far as I can tell, has largely replaced the old DOS-like cmd.exe program for sysadmins. How do we fix the situation caused by our bulk move from within PowerShell?

Returning the project to the state just after having erroneously moved our files into the wedding-photographer-analogy subfolder, we see the now familiar Git status in PowerShell:

PowerShell status output in figures/ directory after bulk file move

Apart from the colours not being as bright as in Git Bash, this is the same list of files as we had before after having moved the files around.

Let’s tentatively check if our Bash one-liner from earlier can work. My guess is that it won’t work, but ya never know.

The first filter is the grep for ' D' to select only the deleted files from the git status -s output. Trying this out gives:

Unknown command grep error in PowerShell

Oh. It looks like we can’t use that then. Well, I guess that’s to be expected. Upon closer inspection, it turns out that none of the Linux utilities work in this context. So, what can we use?

Let’s recreate the one-liner from earlier step-by-step in PowerShell. The first thing we need to do is filter git status -s output for lines matching ' D'. The grep alternative in PowerShell is the Select-String cmdlet,5 and to filter on a given string, we use its -Pattern option. Thus, to select lines matching the string ' D', we use:

Select-String -Pattern ' D'

This gets us a bit closer to our goal of filtering for the deleted files:

PowerShell output after filtering git status with Select-String

That’s nice, although the ←[7m and ←[0m stuff is a bit weird. It looks like the output includes ANSI colour codes. That’s different to how shells on Unix behave. Hrm, interesting. Another thing learned today!

Since cut and xargs also don’t work out of the box on PowerShell, the next question becomes: what can we use instead? From what I’ve found, there isn’t a direct equivalent. But one can use the ForEach-Object cmdlet to loop over all input values, processing them further within the loop body. Since both cut and xargs process lists, using a for loop-like construct here makes sense.

To reproduce the effect of the cut call, we need to split the input on spaces (what the -d' ' does) and take the third element of the resulting list (what -f3 does). In PowerShell, we can use the .Split() method for this task, e.g.:

.Split(' ')[2]

The single space string argument to .Split() splits its input on spaces. The [2] part selects the third element of the resulting array since arrays are zero-indexed in PowerShell. See also the article PowerShell: How to Use ‘cut’ Equivalent for more information.

Putting these pieces together, we get this code as the next processing step:

ForEach-Object { $_.Split(' ')[2] }

The bit inside the curly braces needs a bit of explanation. The $_ is a variable representing the current list item value. As with the {} syntax as discussed earlier, when the loop processes each item in the list, $_ is set to the item’s value so that one can carry out other operations on it. As the ForEach-Object name suggests, each item it processes is an object; hence we can call methods on it. Thus, we call .Split() on the current list item contained in $_.

To be honest, I did a bit of a double take when I saw the $_ variable. As someone with Perl experience, I immediately recognised it as the default iterator variable in things like foreach, grep and map. Here, in ForEach-Object, this variable has the same function, so–to me at least–it was intuitively obvious what the variable does. Also, I found it amusing that this syntax got reused in PowerShell.

Unfortunately, the ForEach-Object loop as written above doesn’t work. This is because the text stream from Select-String contains ANSI character codes, and PowerShell can’t call .Split() on that data. It barfs with the following error:

PowerShell error output after trying to invoke .Split() on ANSI character codes

The solution seems to be to filter only for data of type string from the input stream. In other words, we replace the plain $_ with ([string]$_) to select only the string component of the value in the iterator variable. That will let us access the filenames, and we avoid trying to process ANSI character codes. Note that the parentheses are necessary so that the result is an object that we can call methods on. At least, that’s the way I understood that syntax. I stumbled across this solution in the blog post cat, grep, cut, sort, uniq, sed with PowerShell, which has many more helpful tips for Unix/Linux old hands such as myself.

So, if we now use this:

ForEach-Object { ([string]$_).Split(' ')[2] }

we get:

PowerShell output after successfully filtering git status -s

Which is the list of files that we’re interested in. Nice!

Since ForEach-Object iterates over a list of objects, it performs much the same role as xargs does in a Unix-like environment. Hence, we can attach another ForEach-Object call to our pipeline to add the files in the wedding-photographer-analogy/ folder to Git. We can also remove the old entries within the figures/ folder in the same step.

Putting these ideas into code, we get this:

ForEach-Object { git add wedding-photographer-analogy\$_; git rm $_ }

And yes, the backslash is supposed to be there. And no, it’s not escaping the next character. This is Windows after all. :wink:

Giving that code a go, we get the output from Git that we expect:

PowerShell output from grep, cut, xargs, git add/rm equivalent command pipeline

Now, I realise I could probably have put the string-splitting and Git-running operations within a single ForEach-Object loop. I felt that that would have been even less readable and understandable, so I avoided it.

After running that transformation, git status shows what we expect:

PowerShell git status output after running one-liner

Great stuff! We’ve fixed the erroneous bulk move and are only a short git commit away from saving the corrected state. Cool!

GUI solutions

This entire discussion has been great for command line fanatics, erm, I mean, fans like myself. However, not everyone wants to juggle all these complex and esoteric commands to fix a Git problem.

If you were using a GUI such as Windows Explorer to move files around, then you’re also likely to be using a GUI to interact with Git. How, then, should you fix the bulk file move bumble from above with a GUI? The answer, rather unfortunately, is: it depends. In particular, it depends upon your Git GUI. If you’re using SourceTree or TortoiseGit or GitKraken (or whatever), then how exactly you solve this problem will be different from GUI to GUI.

To give a sense of how one could solve this in a graphical environment, I’m going to go through the same process as above using SourceTree as an example.

Let’s assume that you’re already working with this project in SourceTree. Thus, you’ll see the git_course repository in an open tab when the app starts.

SourceTree window with the git-course project open in a tab showing various unstaged files

As you can see, SourceTree shows the erroneously deleted files and the unknown files within the “Unstaged files” window. The deleted files have a grey minus sign next to them; a purple question mark icon denotes the files unknown to Git.

These are all the files we want to work on, so the simplest thing to do is click on the “Stage All” button to stage all changes. My gut feeling is that doing so will delete the files marked as deleted and add the files marked as untracked. Let’s give it a go.

Click on the “Stage All” button. You’ll now see the files appear in the upper window with a blue “R” symbol next to them. They’re all marked as renamed! In one step!

SourceTree git-course tab with renamed files in the staged files window

Wow. Just wow. This really is a first for me. I think this is the first time in my experience that a GUI has made a Git-related operation easier than doing the corresponding thing on the command line. I’m a bit flabbergasted, to be honest. It was that simple. Wicked!

Now all you need to do is commit the change, and you’re done. Great! :slightly_smiling_face:

There is, however, a devil lurking in the details. If the working directory isn’t clean (i.e. there are unstaged and/or untracked files not part of the bulk move), then there will be more work involved when marking the files to stage. You’ll need to click on and stage each entry in need of repair individually. In other words, it’s not as simple as clicking “Stage All” in that case. Still, it seems to be pretty easy in SourceTree to fix this issue and mark files as renamed even if the working directory isn’t 100% clean.

As a separate experiment, I tried doing something similar with Git Cola to see how a different Git GUI handles the situation. It turned out not to be as simple as in SourceTree. Maybe that’s an edge that SourceTree has over other GUIs? Since my Git experience has been almost exclusively on the command line, I can’t really comment. Sorry. :confused:

Where to from here?

In each case I’ve discussed above, the process has been the same:

  • Get the output from git status.
  • Filter that output to get a list of the affected filenames.
  • Use git add and git rm with the list of filenames to achieve what git mv would have done.

The only difference between doing things by hand, or in Bash, or in PowerShell (or even in SourceTree) was in the details.

The thing is, I have the nagging feeling that I’m missing something. Like, is there a better way to do this on the command line? I’m fairly good at wrangling stuff on the command line,2 yet can’t seem to spot a simpler solution. Am I missing something?

I did search for a more elegant solution, but without much success. One thing I did learn was that one can use git add -u to mark the inadvertently deleted files from the working copy as having been deleted in Git. The -u flag updates the index (a.k.a. staging area) to match the working copy. However, this is effectively the same as running git rm on the deleted files, so it doesn’t simplify the process very much. After all, one still has to run git add on the files in the new location for Git to recognise that they were moved.

I’d be interested to know if anyone else knows of a cleaner, more elegant solution.

While this post might have been much ado about nothing for an experienced Git user and command line wrangler, I hope that for Git newcomers it was helpful in some way. I definitely learned something! :slightly_smiling_face:

  1. And assuming that we have a clone of the project checked out. ↩

  2. Do you need an experienced developer with a broad skill set and a tenacious approach to solving problems? Ping me at paul@peateasea.de to find out more. ↩ ↩2

  3. Also called the staging area. Also called the cache. Git often has many names for the same concept. That can be rather confusing at times. Sorry about that! ↩

  4. Full points if you winced when you noticed I used file as a loop variable. ↩

  5. cmdlet is PowerShell parlance for “PowerShell native commands”. So really it’s just a command. ↩

Support

If you liked this post, please buy me a coffee!

buy me a coffee logo

Categories:

Updated: