ファイルまたはディレクトリのチェックアウトに関するすべてgit
1. 別のブランチから 1 つまたは複数のファイルまたはディレクトリをチェックアウトする方法、または現在チェックアウトされているブランチにハッシュをコミットする方法:
# check out all files in <paths> from branch <branch_name>
git checkout <branch_name> -- <paths>
ソース: http://nicolasgallagher.com/git-checkout-specific-files-from-another-branch/ .
も参照してくださいman git checkout
。
例:
# Check out "somefile.c" from branch `my_branch`
git checkout my_branch -- somefile.c
# Check out these 4 files from `my_branch`
git checkout my_branch -- file1.h file1.cpp mydir/file2.h mydir/file2.cpp
# Check out ALL files from my_branch which are in
# directory "path/to/dir"
git checkout my_branch -- path/to/dir
指定しない場合、branch_name
it は自動的に であると見なされますHEAD
。これは、現在チェックアウトされているブランチの最新のコミットです。したがって、これを実行して「somefile.c」をチェックアウトし、ローカルのコミットされていない変更を上書きすることもできます。
# Check out "somefile.c" from `HEAD`, to overwrite any local, uncommitted
# changes
git checkout -- somefile.c
# Or check out a whole folder from `HEAD`:
git checkout -- some_directory
2. さらに先へ: 任意のブランチから任意のファイルをチェックアウトする方法、またはコンピューター上の任意の場所にハッシュをコミットする方法 (非常に便利です!):
# General form
git show my_branch_or_commit_hash:my_file.cpp > any/path/my_file.cpp
# Example: check out `main.cpp` from 3 commits ago in your currently-checked-out
# branch (3 commits prior to `HEAD`, or `HEAD~3`) into a temporary directory
mkdir ../temp
git show HEAD~3:main.cpp > ../temp/main_old.cpp
私がこれを学んだソース: @Jakub Narębski's answer to: git-checkout old Revision of a file under a new name
3. git merge
、git cherry-pick
、git rebase
、またはgit revert
変更を解決している最中の場合はどうなりますか?
その場合は、次のようにするとよいでしょう。注: どのコミット ハッシュまたはブランチが各コンテキストにあるかを知るには、こちらの回答を参照してください: --theirs
Gitによると、"私たち" とは誰ですか? --ours
:
# Keep `--theirs` for all conflicts within this file
git checkout --theirs -- path/to/some/file
# OR: keep `--ours` for all conflicts within this file
git checkout --ours -- path/to/some/file
また:
# Keep `--theirs` for all conflicts within files inside this dir
git checkout --theirs -- path/to/some/dir
# OR: keep `--ours` for all conflicts within files inside this dir
git checkout --ours -- path/to/some/dir
checkout
それが本当にやりたいことでない限り、この前に前のセクションの通常のフォームを実行しないでください。上記の私の回答の「警告警告警告」セクションを参照してください: Git によると、「私たち」とは誰ですか? .
エラーのpath does not have our version
処理path does not have their version
:
次のようなエラーが表示された場合:
error: path 'path/to/some/dir/file1.cpp' does not have our version
# OR
error: path 'path/to/some/dir/file1.cpp' does not have their version
...上記のコマンドを実行するときは、最初にそれらのファイルにアクセスしてから、 orコマンドを再git rm
試行するだけです。エラーが発生したファイルを自動的に見つけて削除するフォームを含む、これらのコマンドの詳細な説明については、こちらの回答を参照してください: git checkout --ours when file spec includes deleted file。git checkout --ours
git checkout --theirs
4. 特定のファイルまたはディレクトリをリセットして、別のコミットまたはブランチでのそのファイルまたはディレクトリの状態と正確に一致させたい場合はどうすればよいですか?
この場合、 ではgit checkout my_branch -- some_file_or_dir
不十分です。現在チェックアウトされているブランチまたはコミットに存在するが に存在しないファイルが指定されたディレクトリにある場合、my_branch
それらをローカルで削除したいが、削除しgit checkout
ないためです。ローカルに存在するが指定されたコミットには存在しないファイル。むしろ、指定されたコミットのバージョンでローカルにファイルを上書きするだけです。そのため、そこにあるはずのないファイルもローカルで削除して、ローカルで最終的には にあるものの正確なコピーになるようにするには、次の手順を実行する必要があります。my_branch
git reset my_branch -- path/to/some/file_or_dir
git checkout-index -fa
git clean -fd
git commit -m "hard reset path/to/some/file_or_dir to its state \
as it was at my_branch"
詳細については、こちらの私自身の回答を参照してください:なぜ git can't do hard/soft resets by path?
以下も参照してください。
git checkout
ここでの私の回答で、これらの例をさらにいくつか示します。 Gitによると、「私たち」とは誰ですか? .
- [ 「パスで --soft または --hard git リセットを行う方法」に関する私の回答]なぜ git はパスでハード/ソフト リセットを実行できないのですか?
- git-checkout 古いリビジョンのファイルを新しい名前でチェックアウトする
- [私の回答] git checkout --ours ファイル スペックに削除されたファイルが含まれている場合
- [私の回答] git を使用して、作業ツリー (ローカル ファイル システムの状態) をインデックス (「ステージングされた」ファイル) の状態にリセットするにはどうすればよいですか?