1つのファイルのバージョンを1つのgitブランチから別のブランチにコピーするにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/307579

質問

完全にマージされた2つのブランチがあります。

ただし、マージが完了すると、1つのファイルがマージによって台無しにされていることに気付きます(他の誰かが自動フォーマットを行いましたgah)。ブランチ、それから私の1行の変更をブランチに持ち込んだ後、再度挿入します。

では、gitでこれを行う最も簡単な方法は何ですか?

役に立ちましたか?

解決

ファイルを終了するブランチからこれを実行します:

git checkout otherbranch myfile.txt

一般式:

git checkout <commit_hash> <relative_path_to_file_or_dir>
git checkout <remote_name>/<branch_name> <file_or_dir>

いくつかのメモ(コメントから):

  • コミットハッシュを使用すると、任意のコミットからファイルをプルできます
  • これはファイルとディレクトリに対して機能します
  • ファイル myfile.txt および mydir
  • を上書きします
  • ワイルドカードは機能しませんが、相対パスは機能します
  • 複数のパスを指定できます

代替:

git show commit_id:path/to/file > path/to/file

他のヒント

似たような検索でこの質問に行き着きました。私の場合、別のブランチから現在の作業ディレクトリにファイルの元の場所とは異なるファイルを抽出しようとしていました。 回答

git show TREEISH:path/to/file >path/to/local/file

checkoutコマンドの使用はどうですか:

  git diff --stat "$branch"
  git checkout --merge "$branch" "$file"
  git diff --stat "$branch"

madlepの回答に続いて、ディレクトリblobを使用して別のブランチから1つのディレクトリをコピーすることもできます。

git checkout other-branch app/**

opの質問に関して、そこにあるファイルを1つだけ変更した場合、これはうまく動作します^ _ ^

1)ファイルのコピーが必要なブランチにいることを確認します。 たとえば、マスターにサブブランチファイルが必要なので、チェックアウトする必要があるか、マスター git checkout master

にする必要があります

2)次に、特定のファイルのみをサブブランチからマスターにチェックアウトします

git checkout sub_branch file_path/my_file.ext

here sub_branch は、そのファイルがある場所の後にコピーする必要があるファイル名が続くことを意味します。

受け入れられた回答では、最初のオプションは他のブランチからのファイル全体を stages git add ... などが実行されていた)、そして2番目のオプションでは、ファイルがコピーされますが、変更はステージングされません(ファイルを手動で編集したばかりで、顕著な違いがあったかのように)。

ステージングせずに別のブランチからGitコピーファイル

段階的な変更(例: git add filename)

$ git checkout directory/somefile.php feature-B

$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   directory/somefile.php

未解決の変更(ステージングまたはコミットされていない):

$ git show feature-B:directory/somefile.php > directory/somefile.php

$ git status
On branch feature-A
Your branch is up-to-date with 'origin/feature-A'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   directory/somefile.php

no changes added to commit (use "git add" and/or "git commit -a")
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top