1

しばらく取り組んできたプロジェクトがあり、ファイルのサブセットからやり直して、それらのファイルの履歴を保持したいとします。

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の2つのブランチをすでに作成していることを前提としています。

ブランチ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 に答える