假设我有一个项目,我已经工作了一段时间, 然后我想重新开始使用这些文件的子集并保留这些文件的历史记录。
那么有没有办法可以手动将一个文件合并到新的空分支上? 或者我是否需要合并所有文件,然后删除我不想要的文件?
我正在尝试这个工作流程(但它确实不起作用)
$> git init
$> vi file1.c
$> git add file1.c
$> git commit -m 'first commit'
$> vi file2.c
$> git add file2.c
$> git commit -m 'added file2'
$> git log
commit 3788d18e62812d43f6b745f66fdab77081d79711
commit 3ab6959385c09fe2e254104a319a553ee58b198a
$> git checkout @{0}
Note: checking out '3ab6959385c09fe2e254104a319a553ee58b198a'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 3ab6959... first commit
$> git branch -a
* (no branch)
master
$> git branch new_branch
$> git branch
* (no branch)
master
new_branch
$> git checkout new_branch
Switched to branch 'new_branch'
$> git mergetool master file1.c
merge tool candidates: opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse ecmerge p4merge araxis emerge vimdiff
master: file not found
Continue merging other unresolved paths (y/n) ? n
为什么我会收到“master:file not found”?
该合并应该如何?
或者如何将特定提交的特定文件合并到新分支?
答案 0 :(得分:0)
您可以尝试将要迁移的文件提交到旧分支,切换到新分支,然后选择提交到新分支。以下说明假设您已经创建了两个分支:oldBranch和newBranch。
在分支oldBranch上,对要迁移的文件进行修改,然后......
git add fileForOtherBranch.txt
git commit fileForOtherBranch.txt -m "I want to migrate this file to my other branch."
git status
(get the SHA number: will be a mess of 40 characters but you only need the first 7 or so, like ae09g4w)
git checkout newBranch
git cherry-pick ae09g4w
注意:newBranch中的工作目录必须干净才能使其正常工作。
答案 1 :(得分:0)
有一种方法可以做到这一点
$> git checkout new_branch
$> git merge old_branch
$> git rm -rf *
$> git commit -m 'new try'
$> git checkout old_branch file1.c
$> git commit -a -m 'Fetch file1'
然后我们只有file1并保留了file1:s的历史记录。