2
2

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 5 years have passed since last update.

Git でブランチを切り替えると、それに応じてファイルが変更されます

2
Last updated at Posted at 2015-09-25

常日頃Gitを使っている人には当たり前のことなのだろうけど・・・。
ブランチを切り替えると、それに応じてファイルが変更される仕組みになっているんです!

実験

適当なディレクトリで、Gitを初期化します。

git init

適当なテキストファイルを作成します。ここでは端末からコマンドによりファイルの編集をしていますが、エディタを使っても同じことができます。

echo テストファイルです > test.txt
cat test.txt
# テストファイルです

この状態で一度コミットします。

git add .
git commit -m 'initial commit'

新しいブランチへ切り替えて、先のファイルを編集してコミットします。

git checkout -b newbr
echo 別のブランチで編集しています >> test.txt
cat test.txt
# テストファイルです
# 別のブランチで編集しています
git add .
git commit -m 'edited on new branch'

次のようにすると、ブランチを切り替えると同時にそれぞれにおける最新状態に変更されていることが確認できます。

git checkout master
cat test.txt
# テストファイルです

git checkout newbr
cat test.txt
# テストファイルです
# 別のブランチで編集しています
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?