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の操作方法まとめ

Last updated at Posted at 2022-12-15

gitリポジトリを初期化

git init

git の状態を表示

git status

ステージングツリーに追加

追加されたファイルは追跡される

git add {file}

変更されたファイルや追跡されていないファイルを全てステージングツリーに追加

git add .

スナップショットを撮る

変更を全て記録して履歴に残す

git commit -m {message}
(例)
git commit -m "This is my first commit!"
git commit -m "added a readme file and updated the index"

gitツリーからのファイルの削除

git rm {file}

追跡も解除する

git rm --cached {file}
git rm --cached -r {file}

※既にコミットされているファイルは、削除後に変更をコミットして状態を記録する必要がある

git rm {file}
git commit -m "removed the file {file}"

HEAD(現在のブランチ)を移動させる

別のブランチに移動

git checkout {branch}

別のcommitに移動

git checkout {commit}

※HEAD をブランチではなく commit に移動させた場合、「Detached HEAD」と呼ばれる状態になる。
この状態では、HEAD はブランチに接続されておらず、ただの commit を指す。
今 HEAD ツリーはこの commit になったので、この commit 内で追跡されているファイルは全て作業ツリーに反映される。
この状態で新しくcommitを作成すると、フローティングコミットになってしまう。

※作業ディレクトリが更新されるので、チェックアウトを使う前には必ず変更をcommitする。
Git は commit されていない変更点を全て置き換え、それらのファイルの内容を新しい Detached HEAD にあるファイルの内容に変更してしまう。

今いるブランチや Detached HEAD の履歴を見る

git log
git log --oneline

現在のリポジトリのグラフを出力する

git log --all --graph --oneline

別のブランチに切り替える

ブランチの場合はcheckoutではなくswitchを使う

git switch

現在の commit にタグ(commit 名の別名)をつける

git tag {tagname}

(例)チェックアウトできるようになる

git tag "v1.0"
git checkout v1.0

現在の commit の情報を表示

git show
git show --shortstat

※show を終了するには q キーを使う

新しいブランチを作成する

git branch {branch-name}

※「Detached HEAD」の状態で新しくcommitすると、フローティングコミットになってしまうため、新しくブランチを作成するようにする

ブランチの作成(git branch {branch})と移動(git switch {branch}) の両方を一度に実行する

git switch -c {branch}

2 つのブランチをマージ

git merge {branch}

(例)masterブランチとbranchBをマージ(masterに移動してからbranchBをマージする)

git switch master
git merge branchB

マージで作成される新しいcommitにメッセージを渡す

git merge {branch} -m {message}

不要になったブランチを削除する

git branch -d {branch} 

※マージした後は、片方のブランチからの commit は全て現在のブランチの commit 履歴に含まれるので安全。

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?