1

假设我有一个项目已经工作了一段时间,然后我想从文件的一个子集重新开始,并保留这些文件的历史记录。

那么有没有一种方法可以手动将一个文件合并到新的空分支上?还是我需要合并所有文件,然后删除我不再需要的文件?

我正在尝试这个工作流程(但它并没有真正起作用)

$> 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

为什么我得到“主:找不到文件”?

合并应该是什么样子?

或者如何将特定提交中的特定文件合并到新分支?

4

2 回答 2

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 中的工作目录必须是干净的才能正常工作。

于 2012-09-27T14:21:32.727 回答
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 的历史记录。

于 2012-09-28T09:38:04.400 回答