Git cherry pick 文件,从A目录到B目录

标签 git

我有一个项目,一直在进行一些实验性更改。到目录 A 中的子项目。但是,在项目的 master 分支中,子项目已移动到一个单独的目录,目录 B。

我有大约 10 个提交我想有效地挑选到 master 中,但是我如何告诉 git 将旧目录放入新目录?

稍后
我试图挑选,因为我只想介绍我的更改并保持历史清洁。这不仅仅是文件移动。我也添加了新类(class)。它也没有检测到所有的 Action 。(想到的第一个文件是pom.xml)

最佳答案

您可以将这些提交转换为补丁,手动编辑补丁(用新路径替换旧路径),然后应用。

$ git init
Initialized empty Git repository in /tmp/gitt/.git/
$ mkdir olddir
$ cp /home/vi/code/_/ucontext.cpp olddir/ucontext.cpp
$ git add .
$ git commit -m "initial commit"
[master (root-commit) c0bf371] initial commit
 1 files changed, 97 insertions(+), 0 deletions(-)
 create mode 100644 olddir/ucontext.cpp

$ printf '22\na\n// Sup, /git/!\n.\nw\nq\n' | ed olddir/ucontext.cpp
1983
    if (!stacks) { stacks = stacks3; }
1998
$ git commit -am 'sample commit'
[master 83940c9] sample commit
 1 files changed, 1 insertions(+), 0 deletions(-)
$ printf '33\nd\nw\nq\n' | ed olddir/ucontext.cpp
1998
    }else{
1990
$ git commit -am 'sample commit 2'
[master 6599932] sample commit 2
 1 files changed, 0 insertions(+), 1 deletions(-)

$ git format-patch HEAD~2 --stdout > saved-commites.patch
$ git reset --hard HEAD~2
HEAD is now at c0bf371 initial commit
$ git mv olddir newdir
$ git commit -m 'rename dir'
[master bd8a77e] rename dir
 1 files changed, 0 insertions(+), 0 deletions(-)
 rename {olddir => newdir}/ucontext.cpp (100%)

$ sed -i.bak 's!\([ab]/\)olddir/!\1newdir/!g' saved-commites.patch
$ git am saved-commites.patch
Applying: sample commit
Applying: sample commit 2

关于Git cherry pick 文件,从A目录到B目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9544801/

相关文章:

git - 难道我的 GitHub SSH key 不应该让我每次都免于编写凭据吗?

android - 您应该将 R.java 文件 checkin VCS 吗?

linux - git 说一切都是最新的

git - 如何在执行 git pull 时显示 git 差异?

git - 如何使用git管理一个代码库但有不同的环境

windows - 没有 Cygwin 的 Windows 上的 Git?

Git 克隆在 Windows bash 中挂起

WSL 上的 Git 克隆给出 "permission denied"错误 [WSL1]

来自 github 空绿色文件夹的 Git 存储库

node.js - Git 忽略 mongod.log