質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

Stash

Gitリポジトリを管理するサービス。 プロジェクト規模・ユーザー数などの増加に伴い、管理が難しくなるリポジトリを、サーバー上で一元的かつセキュアに管理できるサービスです。

Q&A

解決済

1回答

1412閲覧

Github checkoutするとcheckout前のコードが引き継がれる

smilax

総合スコア23

GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

Stash

Gitリポジトリを管理するサービス。 プロジェクト規模・ユーザー数などの増加に伴い、管理が難しくなるリポジトリを、サーバー上で一元的かつセキュアに管理できるサービスです。

0グッド

0クリップ

投稿2020/07/24 01:47

編集2020/07/24 01:49

Githubでcheckoutするとコードが引き継がれる現象が一定の確率で起きます(毎回ではない)。

例)

masterからfeatureブランチを作成してチェックアウト

aファイルを追加

masterブランチに戻りたくなって現状をstashで退避

masterにチェックアウト

aファイル出現!!

明らかにリモートのmasterとは違うのにpullしてもresetしても何も変わらずaファイルが存在したままになります。

という感じになります。なぜこの現象が起こるのか分かる方がいれば教えていただきたいです。

また、こういった現象が起きた時の対処法も教えていただきたいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

a ファイルをファイルとして追加しただけで、 Git 管理に追加していなければ、
通常の stash コマンドではファイルは stash 領域に待避されません

実験

Git 管理に追加されていないファイルは
stash しても stash 領域に待避されません:

console

1$ git status 2On branch master 3Untracked files: 4 (use "git add <file>..." to include in what will be committed) 5 test.txt 6 7nothing added to commit but untracked files present (use "git add" to track) 8 9$ git stash 10No local changes to save 11 12$ git status 13On branch master 14Untracked files: 15 (use "git add <file>..." to include in what will be committed) 16 test.txt 17 18nothing added to commit but untracked files present (use "git add" to track)

Git 管理に追加すると stash は成功します:

console

1$ git add test.txt 2 3$ git stash 4Saved working directory and index state WIP on master: 68829db First commit 5 6$ git status 7On branch master 8nothing to commit, working tree clean 9 10$ git stash list 11stash@{0}: WIP on master: 68829db First commit 12 13$ git stash show 0 14 test.txt | 1 + 15 1 file changed, 1 insertion(+)

この場合は checkout してもファイルはワーキングツリーではなく
stash 領域に存在します:

console

1$ git checkout -b feature 2Switched to a new branch 'feature' 3 4$ git status 5On branch feature 6nothing to commit, working tree clean 7 8$ git stash list 9stash@{0}: WIP on master: 68829db First commit 10 11$ git stash show 0 12 test.txt | 1 + 13 1 file changed, 1 insertion(+)

もし、Git 管理されていないファイルがワーキングツリーにある状態で checkout が成功すると、
ファイルはワーキングツリーに残ったままになります:

console

1$ git status 2On branch master 3Untracked files: 4 (use "git add <file>..." to include in what will be committed) 5 test.txt 6 7nothing added to commit but untracked files present (use "git add" to track) 8 9$ git checkout -b feature 10Switched to a new branch 'feature' 11 12$ git status 13On branch feature 14Untracked files: 15 (use "git add <file>..." to include in what will be committed) 16 test.txt 17 18nothing added to commit but untracked files present (use "git add" to track)

投稿2020/07/24 01:51

編集2020/07/24 02:11
y_shinoda

総合スコア3272

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

smilax

2020/07/24 01:54

なるほど、そうなんですね。 git管理に追加されていないため、切り替えをしてもファイルが残り続けてしまっているという認識であっていますか?
y_shinoda

2020/07/24 01:54

その通りです!
smilax

2020/07/24 02:26

理解できました。ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問