LoginSignup
0
0

More than 1 year has passed since last update.

[Git] 動作を試す 実行例18:異なるコミット位置の状態で、stashから異なる内容を戻すと壊れる

Posted at

Gitの動作を理解するために、Gitのコマンドを実際に試して、結果を見てみました。

1つの記事内で一連のGitコマンドが完結しているので、これら一連のコマンドを順に実行させて結果を見ることで、一連のGitの動作を実際に体感でき、一通り独習することが可能です。

※前回記事のリポジトリ状態からの続きになっています。

前回記事へ 目次へ:Git関連記事のまとめページ 次回記事へ

実行例

git log --oneline
↓
結果: 
94996a0 (HEAD -> master) message4
20bbab7 message3
00cad71 message2
4ace194 message1

git checkout 00cad71
git log --oneline
↓
結果: 
00cad71 (HEAD) message2
4ace194 message1

dir /b
↓
結果: 
test1.txt
test2.txt

type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12

git stash apply 0
↓
結果: 
HAuto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
HEAD detached at 00cad71
Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
        both modified:   test1.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test7.txt

dir /b
↓
結果: 
test1.txt
test2.txt
test7.txt

type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
<<<<<<< Updated upstream
=======
Sample-Added-13
Sample-Added-14
Sample-Added-15
>>>>>>> Stashed changes

※異なるコミットの所でstashから内容を戻すと、自動でマージが実施され、競合が発生。

-----

git checkout master
↓
結果: 
error: you need to resolve your current index first
test1.txt: needs merge

git stash -u -m "stash2"
↓
結果: 
test1.txt: needs merge

※マージを処置しないとstashが出来ない。

※マージを処置、内容を元に戻す。
echo Sample-Added-11 > test1.txt
echo Sample-Added-12 >> test1.txt
type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12

git status
↓
結果: 
HEAD detached at 00cad71
Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
        both modified:   test1.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test7.txt

no changes added to commit (use "git add" and/or "git commit -a")

git stash -u -m "stash2"
↓
結果: 
test1.txt: needs merge

※マージの処置を登録。
git add test1.txt

git status
↓
結果: 
HEAD detached at 00cad71
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test7.txt

nothing added to commit but untracked files present (use "git add" to track)

git stash -u -m "stash2"
↓
結果: 
Saved working directory and index state On (no branch): stash2

※マージを処置して、stashが成功。

※今までの乱暴な操作で、煩雑になってしまった(分からなくなった)変更を全てstashに入れて、
元のクリーンな(変更なしの)状態に戻す。

-----

git status
↓
結果: 
HEAD detached at 00cad71
nothing to commit, working tree clean

git stash list
↓
結果: 
stash@{0}: On (no branch): stash2
stash@{1}: On master: stash1

git log --oneline
↓
結果: 
00cad71 (HEAD) message2
4ace194 message1

git checkout master
↓
結果: 
Previous HEAD position was 00cad71 message2
Switched to branch 'master'

git log --oneline
↓
結果: 
94996a0 (HEAD -> master) message4
20bbab7 message3
00cad71 message2
4ace194 message1

git status
↓
結果: 
On branch master
nothing to commit, working tree clean

※これで全て元の状態に戻った。

環境

Windows 10、PortableGit-2.40.0-64-bitを使用、全てローカルPC上で実施、GitHub等は不使用。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0