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

【Git】コマンドまとめ 初学者用

Last updated at Posted at 2020-05-03

対象の方

この記事はGitの基礎学習した人が復習に使う、もしくは軽くgitの概要を知りたい人向け。
自分。

なぜこの記事を書いたのか

以前まで、GUIに頼っていたので、CUIでバージョン管理ができるようになりたかった。
理由としては、CUIで操作できた方がより自由にgitを扱えるため。
(あと、CUIで扱っている方がかっこいいため)

コマンド

git config --global user.name "(your name)"
git config --global user.email "(your email)"
必須の設定、ユーザー名とメールアドレスの設定

git config --global color.ui true
色付けをして見やすくなる

git config --global alias.aliasname 元のaliasにしたいコマンド名
エイリアスを作る(ショートカットのこと)

git config --global alias.co checkout
// この後からgit checkoutはcoで発動させることができる

git config -l
設定の確認

git config --help
git help config
コマンドの確認

git init
リポジトリにGitを対応させる

git add
ステージングにあげる

git add .
全ての変更をいっぺんにステージングにあげる

git commit
リポジトリにあげる

git log
ログ確認(コミットの情報などが記載されている)

git log --oneline
短くログを表示

git log -p
コミットの変更された場所を確認

commit 549fd1b9db6d27e3d5d86d91667e7779f43cadf2 (HEAD -> master)
Author: Tomoaki Yoshinaga <naruto1439m@gmail.com>
Date:   Sun May 3 18:35:25 2020 +0900

    initial commit  //コミット名

diff --git a/index.html b/index.html
new file mode 100644
index 0000000..89b24ec
--- /dev/null   //何もないところから
+++ b/index.html //index.htmlが生成された
@@ -0,0 +1 @@   //変更された行
+line 1      //変更された箇所

git log --stat
どのファイルが何箇所変わったかを(簡略化して)見ることができる

commit 549fd1b9db6d27e3d5d86d91667e7779f43cadf2 (HEAD -> master)
Author: Tomoaki Yoshinaga <naruto1439m@gmail.com>
Date:   Sun May 3 18:35:25 2020 +0900

    initial commit

 index.html | 1 +
 1 file changed, 1 insertion(+)

git status
変更されたのみなのか
ステージングにいるのか
それともコミットした後なのかを見ることができる

git checktout -- filename
でファイルの変更を取り消すことができる

git diff
ステージングに上がっていない変更のみが見れる
 
git diff --cached
ステージングに上がっている変更のみ見れる

git rm
git mv
git管理下のファイルを移動し足り削除するときはlinuxコマンドでそのまま変更するのではなく、gitコマンドを使って行う必要がある

.gitignore
git管理下から外すために使うファイルのこと
.gitignore以下のディレクトリにのみ適応される

git commit -m "commit message"
ワンラインでコミットする

git commit --amend
直前のcommitと現在のステージングに上がっているものを同じコミットにまとめる、このコマンドを使うとエディタが開く

git commit -am "commit message"
git addとgit commitを同時に行う

git show
コミットの詳細情報を見ることができる

git reset --hard HEAD
以前のコミットした時の状態に戻る
HEADは直前のコミットのこと

git reset --hard HEAD^
HEADの一つ前に戻る

git reset --hard ORIG_HEAD
前回HEADだったコミット(今は違う)を指定して戻る
間違って必要なコミットを取り消してしまった時に使う

git branch
現在のブランチの確認

git branch branchname
ブランチの作成

git checkout branchname
ブランチの切替

git checkout -b branchname
branchを作った後に、作ったbrachに切り替える

git merge branchname
主にしたいブランチに切り替えた上で、統合したいブランチ名を指定する

git branch -d branchname
不要なブランチを削除

git branch --merged
既にmergeされたbranchの一覧を見ることができる

git branch --no-merged
mergeされていないbranch一覧

git stash
一時的に変更内容を退避することができるのがstash
まだ作業中なのでcommitしたくない、というときに使われる

$ git add filename
// ステージングにあげる
$ git stash save
// stashにステージングにある変更を退避させる
$ git stash list
// stashに保存されているもののリストを見る
$ git stash pop
// stashから戻す

git tag
タグ一覧

git tag tagname
コミットの一意の文字にわかりやすい名前をつける

git tag -d tagname
タグを消す(コミットを消すわけではない)

git clone url名
リモートリポジトリをローカルにコピーする

git push origin master
リモートリポジトリにローカルリポジトリの変更(commit等)を反映させ、リモート追跡ブランチの更新も行う

git pull origin master
リモートリポジトリの変更をリモート追跡ブランチに適応させ、現在いるブランチ(カレントブランチ)にのみその変更をmergeする

git fetch origin master
リモート追跡ブランチにリモートの変更を適応するもの。
これはローカルブランチに変更が加わるわけではないので注意

最後に

この記事は自分用でもあるので、説明は少しざっくりしている部分が多いです。ですので、他に気になったことや、別途知りたい事がありましたら、他の記事を参考にしていただけると嬉しいです。
また、表現の誤りや、コマンドのタイポがありましたら、報告してくださると助かります。

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?