0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Git] 動作を試す 実行例16:checkoutで履歴中の前後のコミット位置へ移動する

Last updated at Posted at 2023-05-08

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

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

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

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

実行例

(※始めに内容を確認)
type test1.txt
↓
結果: 
Sample-Added-11
Sample-Added-12
Sample-Added-13
Sample-Added-14

(※始めにコミットSHA値を調べる)
git log --oneline
↓
結果: 
94996a0 (HEAD -> master) message4
20bbab7 message3
00cad71 message2
4ace194 message1

git checkout 20bbab7
↓
結果: 
Note: switching to '20bbab7'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 20bbab7 message3

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

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

-----

git checkout master
↓
結果: 
Previous HEAD position was 20bbab7 message3
Switched to branch 'master'

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

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

-----

※untrackedなファイルがある場合に移動すると、そのまま引き継がれて残る。
echo Sample-Added-11 > test101.txt
git checkout 00cad71
↓
結果: 
Previous HEAD position was 94996a0 message4
HEAD is now at 00cad71 message2

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

-----

※untrackedなファイルと移動先のファイルで名前が同じになる場合に移動すると、移動中止になる。
echo Sample-Added-**41 > test4.txt
git checkout 94996a0
↓
結果: 
error: The following untracked working tree files would be overwritten by checkout:
        test4.txt
Please move or remove them before you switch branches.
Aborting

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

※untrackedなファイルを全て削除。
git clean -f
↓
結果: 
Removing test101.txt
Removing test4.txt

git checkout 94996a0
↓
結果: 
Previous HEAD position was 00cad71 message2
HEAD is now at 94996a0 message4

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

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

環境

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?