Search code examples
gitgit-rebasegit-remotegit-cherry-pickgit-mv

Should I git mv or just mv the files I'm cherry-picking / rebasing from another directory?


I have some csv files that I modify on a regular basis, they don't really contain any sensitive information, they are just used as imports into a program, and I modify them as requested.

Lately I've found what may be a more efficient way to process the changes, but I tested it on a temporary git repository outside of the project.

Since this was successful I now want to git cherry-pick the range of changes to it, so first I added the temporary git repo as a remote to my project.

# In my project I added a remote of the test repo with 
# a copy of the csv's in the HEAD of my main project.
git remote add tempchanges <path-to-test-repo>

# Created a new branch in my project to test this out in.
git checkout -b new/9_30_2019

# Cherry-picked the changes from the remote test repo, but they ended up in the root of the project.
git cherry-pick <start-commit-in-test-repo>..<head-commit-in-test-repo>

# Started a rebase
git rebase -i <start-commit-in-test-repo>~1

# (`edit`ed each commit in the rebase)

(In hindsight, I think this was a mistake, I should have just cut another branch in my existing project, but I digress)

The temporary repository only has a master branch, and everything has been done in the root of the project across a range of commits.

When I cherry-pick the changes into my project, and into the local branch, the csv files all end up in the root directory of my project but they belong in the src/csv file of my project instead, so I've decided to do an interactive rebase from the start of the range of commits (minus 1 commit) and edit each so that the changes show up in my project's repository in the src/csv directory instead of in the root.

But my question is, if I want to do this, should I use git mv to move the files or just a standard bash mv command and then reapply each of the changes with the rebase with the corresponding commit message added to the temp project.

So far I've tried the git mv -f to move the files on the first commit and the changes weren't even mentioned when I went to commit them before running git rebase --continue.

Is there anything else I should be doing to make this process go smoother, and I'm assuming I should be using mv instead of git mv -f as the changes appear to show up that way prior to the commit.

I'd also like to preserve the commit messages without having to copy them from the logs in the old repo.


Solution

  • git mv does nothing magical. Git does not remember renames, rather it deduces them from the contents of the files. You can use git mv or you can use mv and then git add your changes.

    git mv some.csv src/some.csv
    

    Is equivalent to

    mv some.csv src/some.csv
    git add some.csv src/some.csv
    

    It's a little weird to "add" a removed file. You're "adding" your changes to the staging area, this change happens to be removing a file.


    The changes you're making, moving files to a subdirectory, can be done all at once with git filter-branch.

    git filter-branch --index-filter 'git mv *.csv src/' <range of changes>