0

I created a repository on the web service 'GitHub', to which I uploaded files one by one or in a group via the web interface from the browser. The files are placed in folders of different nesting depth. The files have different extensions.

Now I needed to rename 157 files in the repository according to the following principle:

00-00_file-name.ext   # before
00-000_file-name.ext  # after, added '0'

These are templates. There can be different extensions instead of .ext, I use .cpp and .h. The file-name can be of different lengths. Instead of 00-00, there can be 00-01, 00-02, ..., 01-00, 01-01, ..., 02-00, 02-01, 02-02 and so on. That is, I needed in the second number (after the hyphen -) three digits instead of two.

I know how to rename files one by one through the web interface, but I don't know how to rename many files at once through the 'GitHub' web interface.

I cloned the repository to my computer. I wrote a PowerShell script to rename 157 files and renamed these files in the cloned repository with it. After that, I created one commit with all the renames and sent the changes to the original repository on 'GitHub'. Below I will briefly outline the details on the points. (I work on the 'Windows 10' operating system, I use the 'Git for Windows' program from the command line. I use 'PowerShell' version 7 as a shell. I use 'Windows Terminal' as a terminal emulator.)

  1. Cloning a repository to my computer:
PS C:\Users\Илья\source\repos> git clone https://github.com/ilyachalov/learncpp-com-examples
  1. PowerShell script (in the file rename-files.ps1, UTF-8 encoding without BOM, CRLF line endings) for renaming files:
Get-ChildItem -File -Recurse |
    Rename-Item -NewName {
        $_.Name -replace '^(\d\d)-(\d\d)_(.*)', '$1-0$2_$3'
    }

I also wrote a version of the script that does the same thing, but still outputs messages to the console about the progress of renaming and the final results:

$total = 0
$renamed = 0
Get-ChildItem -File -Recurse |
    Rename-Item -NewName {
        $old = $_.Name
        $new = $old -replace '^(\d\d)-(\d\d)_(.*)', '$1-0$2_$3'
        $script:total++
        if ($old -ne $new) {
            Write-Host "$old --> $new"
            $script:renamed++
        } else {
            Write-Host "$new"
        }
        $new
    }
"Total files checked: $total, including renamed: $renamed"

Starting renaming (before using the script, I put it in the target folder and it checks the subfolders in the target folder too; after renaming, the script from the target folder should be deleted so that it does not get into the commit):

PS ..\repos\learncpp-com-examples\chapters> .\rename-files
  1. Creating a commit:
PS ..\learncpp-com-examples> git add --all

As far as I understand, in this case, the git add --all command executes git rm 'old-name-file' and git add 'new-name-file' for each file, and the 'Git' system diagnoses file renaming. (There's a book about it.)

PS ..\learncpp-com-examples> git commit -m "Renaming a group of files (157)"
  1. Sending changes to the original 'GitHub' repository:
PS C:\Users\Илья\source\repos\learncpp-com-examples> git push origin main

In the end, I succeeded, but I'm curious if there are more optimal ways to do the same. After all, I'm new to 'GitHub' and may not know something.

My questions. Does GitHub have tools for group file processing via the web interface and through other interfaces? Is it possible to write a script for renaming files in PowerShell more optimally than I did? I couldn't use the git mv command, is it possible to use regular expressions in this command? What are more convenient programs (tools) for renaming files on 'GitHub'?

Ilya Chalov
  • 149
  • 2
  • 9
  • Just use whatever tool for renaming. Renaming (and copying for that matter) is not concepts which exist as part of gits commits. All renaming and copying is detected when a log/blame or similar command is run. – Kim Jun 30 '23 at 06:53
  • @Kim In my case, in step 3, the `git add -all` command executes `git rm 'old-name'` and `git add 'new-name'` for each file, and the 'Git' system diagnoses that the renaming has occurred (as I understand it). Is that what you meant? I'll add this to my question. – Ilya Chalov Jun 30 '23 at 20:47
  • @Kim You write "Just use whatever tool for renaming". I used a PowerShell script. But I wrote my question because I wanted to find out how other people do the same operation. Could you recommend certain tools? – Ilya Chalov Jun 30 '23 at 21:05

1 Answers1

0

I couldn't use the git mv command, is it possible to use regular expressions in this command?

I got a hint from the users of the 'Linux' system how to use regular expressions with the git mv command in this case, even though it does not support them. I rewrote the proposed method in PowerShell:

Get-ChildItem -File -Recurse |
    ForEach-Object {
        $old = $_.Name
        $new = $old -replace '^(\d\d)-(\d\d)_(.*)', '$1-0$2_$3'
        if ($old -ne $new) {
            $path = $_.DirectoryName
            Invoke-Expression "git mv $path\$old $path\$new"
        }
    }

After running this script, you do not need to form an index using the git add --all command, you can immediately create a commit using the git commit command. This is stated in the documentation of the git mv command.

Ilya Chalov
  • 149
  • 2
  • 9