1680

私は Git を使用しており、masterブランチで作業しています。このブランチには、app.js.

experimentたくさんの変更と大量のコミットを行ったブランチがあります。app.jsここで、すべての変更をfromexperimentからmasterブランチにのみ適用したいと考えています。

それ、どうやったら出来るの?

繰り返しますが、マージは必要ありません。すべての変更をブランチapp.jsからブランチに持ち込みたいだけです。experimentmaster

4

13 に答える 13

2101
git checkout master               # first get back to master
git checkout experiment -- app.js # then copy the version of app.js 
                                  # from branch "experiment"

git 1 つのファイルの変更を元に戻す方法も参照してください。


2019 年 8 月更新、Git 2.23

newコマンドgit switchgit restoreコマンドを使用すると、次のようになります。

git switch master
git restore --source experiment -- app.js

デフォルトでは、作業ツリーのみが復元されます。
インデックスも更新する場合 (ファイルの内容を復元し、1 つのコマンドでインデックスに追加することを意味します) :

git restore --source experiment --staged --worktree -- app.js
# shorter:
git restore -s experiment -SW -- app.js

Jakub Narębskiがコメントで言及しているように:

git show experiment:path/to/app.js > path/to/app.js

ただし、SO の質問「Git で特定のリビジョンから単一のファイルを取得する方法は?」で詳しく説明されているように、リポジトリのルート ディレクトリからのフル パスを使用する必要があります。
したがって、Jakub が例で使用する path/to/app.js です。

フロスティがコメントで言及しているように:

app.js の最新の状態のみを取得します

ただし、git checkoutorについては、SO の質問「 git checkout Revision of a file in git guigit showに示されているように、実際には必要なリビジョンを参照できます。

$ git show $REVISION:$FILENAME
$ git checkout $REVISION -- $FILENAME

$FILENAME がバージョン管理されたファイルのフル パスであることは同じです。

$REVISIONに示すようにすることができますgit rev-parse

experiment@{yesterday}:app.js # app.js as it was yesterday 
experiment^:app.js            # app.js on the first commit parent
experiment@{2}:app.js         # app.js two commits ago

等々。

schmijosはコメントに次のように追加します。

スタッシュからこれを行うこともできます:

git checkout stash -- app.js

これは、2 つのブランチで作業していて、コミットしたくない場合に非常に便利です。

于 2010-03-02T15:23:21.933 に答える
493

すべてがはるかに簡単です。そのためには git checkout を使用してください。

ブランチを仮定するとyou're on master、ブランチを取得するには次のようにapp.js from new-featureします。

git checkout new-feature path/to/app.js

// note that there is no leading slash in the path!

これにより、目的のファイルの内容が表示されます。いつものように、新しい機能のブランチ名の代わりに sha1 の一部を使用 して、その特定のコミットにあったファイルを取得できます。

:リモート ブランチではなく、ローカルnew-featureブランチである必要があります。

于 2012-04-05T08:48:00.570 に答える
46

VonCとchhhの回答の補足:

git show experiment:path/to/relative/app.js > app.js
# If your current working directory is relative, then just use
git show experiment:app.js > app.js

また

git checkout experiment -- app.js
于 2015-12-08T02:22:34.423 に答える
42

ファイルまたはディレクトリのチェックアウトに関するすべて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_nameit は自動的に であると見なされます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 mergegit cherry-pickgit rebase、またはgit revert変更を解決している最中の場合はどうなりますか?

その場合は、次のようにするとよいでしょう。注: どのコミット ハッシュまたはブランチが各コンテキストにあるかを知るには、こちらの回答を参照してください: --theirsGitによると、"私たち" とは誰ですか? --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 filegit checkout --oursgit 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?

以下も参照してください。

  1. git checkoutここでの私の回答で、これらの例をさらにいくつか示します。 Gitによると、「私たち」とは誰ですか? .
  2. [ 「パスで --soft または --hard git リセットを行う方法」に関する私の回答]なぜ git はパスでハード/ソフト リセットを実行できないのですか?
  3. git-checkout 古いリビジョンのファイルを新しい名前でチェックアウトする
  4. [私の回答] git checkout --ours ファイル スペックに削除されたファイルが含まれている場合
  5. [私の回答] git を使用して、作業ツリー (ローカル ファイル システムの状態) をインデックス (「ステージングされた」ファイル) の状態にリセットするにはどうすればよいですか?
于 2020-12-11T21:22:13.243 に答える
8

または、別のブランチからすべてのファイルが必要な場合:

git checkout <branch name> -- .
于 2017-02-02T18:51:26.413 に答える
1

別のブランチからファイルをチェックアウトするには、単純な 1 行のコマンドです。

git checkout branch C:\path\to\file.cs

複数のファイルをご希望の場合

git checkout branch C:\path\to\file1.cs C:\path\to\file2.cs
于 2021-09-07T19:12:48.250 に答える