【问题标题】:Should I git mv or just mv the files I'm cherry-picking / rebasing from another directory?我应该 git mv 还是只 mv 从另一个目录中挑选/变基的文件?
【发布时间】:2020-01-31 21:06:07
【问题描述】:

我有一些csv 文件,我会定期修改,它们实际上不包含任何敏感信息,它们只是用作程序的导入,我会根据要求对其进行修改。

最近我发现了处理更改的更有效方法,但我在项目外部的临时 git 存储库上对其进行了测试。

由于这很成功,我现在想将git cherry-pick range of changes 添加到它,所以首先我将临时 git repo 作为远程添加到我的项目中。

# 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)

(事后看来,我认为这是一个错误,我应该在现有项目中削减另一个分支,但我离题了)

临时存储库只有一个 master 分支,并且所有内容都在项目的根目录中完成了一系列提交。

当我将 cherry-pick 更改到我的项目并进入本地分支时,csv 文件最终都在我的项目的根目录中,但它们属于我的项目的 src/csv 文件,所以我决定从提交范围的开始(减去 1 次提交)开始进行交互式 rebase 并编辑每个,以便更改显示在我的项目存储库中的 src/csv 目录而不是根目录中。

但我的问题是,如果我想这样做,我应该使用 git mv 移动文件还是只使用标准 bash mv 命令,然后使用 rebase 重新应用每个更改并添加相应的提交消息到临时项目。

到目前为止,我已经尝试使用 git mv -f 在第一次提交时移动文件,并且在运行 git rebase --continue 之前提交它们时甚至没有提及更改。

我还应该做些什么来使这个过程更顺利,我假设我应该使用mv 而不是git mv -f,因为在提交之前更改似乎以这种方式显示。

我还想保留提交消息,而不必从旧仓库的日志中复制它们。

【问题讨论】:

    标签: git git-rebase git-remote git-cherry-pick git-mv


    【解决方案1】:

    git mv 没有什么神奇的。 Git 不记得重命名,而是从文件的内容中推断出它们。您可以使用git mv,也可以使用mv,然后使用git add 进行更改。

    git mv some.csv src/some.csv
    

    相当于

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

    “添加”已删除的文件有点奇怪。您正在将更改“添加”到暂存区域,此更改恰好是删除文件。


    您所做的更改,将文件移动到子目录,可以使用git filter-branch 一次性完成。

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

    【讨论】:

    • git filter-branch (在rebase -i 内,或者我应该说期间)?
    • @leeand00 不,代替变基。它使您在交互式 rebase 中手动进行的编辑自动化。
    猜你喜欢
    • 1970-01-01
    • 2017-12-25
    • 2010-11-08
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 2011-04-22
    相关资源
    最近更新 更多