don't click here

How to move Git files into folder without losing history

Discussion in 'Technical Discussion' started by BenoitRen, Mar 17, 2024.

  1. BenoitRen

    BenoitRen

    Tech Member
    404
    180
    43
    A week ago, I made a Git repository on NotABug.org. Then I noticed I forgot to select a license. Hmm, looks like you can't select a license once it's created. So I deleted the repostory and recreated it, this time choosing a license.

    Today it hits me that, unlike my other repository, all of my files are in the root instead of being in a folder called src, there's no license, and no readme.md. Something must have gone wrong because the repository previously existed.

    I think I can rebase and add a new first commit to add the missing license, readme.md, and the src folder. But if I move the source code into the src folder, the history will be lost.

    Git experts, how do I move those files without losing this week's history? Bear in mind that I don't use the command line (I used to, though!), but Fork.
     
  2. Billy

    Billy

    RIP Oderus Urungus Member
    2,118
    178
    43
    Colorado, USA
    Indie games
  3. BenoitRen

    BenoitRen

    Tech Member
    404
    180
    43
    git mv does not let you keep the history. I mean, it's still there, but not attached to the file in the new location.
     
  4. MainMemory

    MainMemory

    Kate the Wolf Tech Member
    4,742
    338
    63
    SonLVL
    I mean the page literally says:
     
  5. BenoitRen

    BenoitRen

    Tech Member
    404
    180
    43
    Yes, but that's misleading. The history isn't gone, but the moved file will be considered a different file and not be linked to the previous history.
     
  6. MainMemory

    MainMemory

    Kate the Wolf Tech Member
    4,742
    338
    63
    SonLVL
    On GitHub at least, a file move looks like this:
    upload_2024-3-18_13-22-20.png
    Yeah, the whole file history isn't shown in one place but the move is recorded, and you can use that to go further back. It seems that 'git log --follow' can also detect moves and show the full history, some GUIs may do this automatically.
    Rewriting history so the previous commits use the new filename is inadvisable, if it's even possible.
     
  7. BenoitRen

    BenoitRen

    Tech Member
    404
    180
    43
    It's inadvisable if you're working with others on the same repository, but as I'm the only contributor right now, there's no harm.

    Luckily, it is possible using git-filter-repo. After finding readable documentation on how to use it to do what I want, I solved my problem. :)
     
  8. MainMemory

    MainMemory

    Kate the Wolf Tech Member
    4,742
    338
    63
    SonLVL
    Does that also fix the files themselves to refer to the changed name or do you now have a bunch of broken builds in your history?
     
  9. BenoitRen

    BenoitRen

    Tech Member
    404
    180
    43
    It doesn't touch the files themselves, no. If all I wanted to do was place all the files in /src, I wouldn't need to change anything. However, I took the opportunity to place the gost08-related files in /src/zone08, so I do need to fix their #include directives. :)