LoginSignup
0
0

More than 3 years have passed since last update.

git command

Posted at

●gitをダウンロードした後に、下記設定をする
git config --global user.name [username]
git config --global user.email [mailaddress]

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●ローカルリポジトリには三つのエリアがある
・ワークツリー
↓add
・ステージ
↓commit
・ローカルリポジトリ
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●ローカルリポジトリの新規作成
cd [path]
git init

●リモートリポジトリからローカルに落とす
例えば
git clone "https://github.com/atom/atom"
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●ステージに追加する(=次回のコミット対象に追加する)
git add [file name]
git add [directory name]

全てのファイル変更をステージする
git add .

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
コミットする
git commit
・オプション
[-m]editorを立ち上げず、直接メッセージ付きで変更する
[-v]変更箇所を表示させる

コメント追記のルール
一行目:変更箇所
三行目:変更の目的

ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●git status
・ワークツリーからステージ間の変更(ファイル新規、変更、削除)を表示させる
・ステージからローカルリポジトリ間の変更を表示させる
ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●git diff(git addとgit commitする前にやる)
・git addする前の変更差分(ファイル変更、削除)(ワークツリーとステージの間)
git diff
git diff [ファイル名]
※新規のファイルは比較対象外

・git addした後の変更差分(ステージとローカルリポジトリの間)
git diff --staged
ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
リポジトリにコミットした履歴を表示させる
●git log

・オプション
[--oneline] 一行だけを表示させる
[-n 3] 最新の3世代を表示させる
[-p] 変更詳細を表示させる
[ファイル名] 指定のファイルだけを表示させる

ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●管理したくないファイルをgitから除外する
.gitignore というファイルで管理する(直接ファイル名を指定)
windowsでは「.gitignore」を作れないので、「.gitignore.」を作る
※コミットするまで除外できる

ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●git rm
コミットしたファイルを管理から外す
・ファイルも一緒に削除する
git rm [file name]
git rm -r [directory name]
・ファイルを残し、管理だけを外すとき
git rm --cached [file name]
あと、.gitignoreに追加する必要がある

ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●git mv
・改名
git mv [旧ファイル名] [新ファイル名]

・移動
git mv [ファイル名] [パス]
相対パスも絶対パスもOK

ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●コマンドに別名をつける
git config --global alias.[別名] [コマンド名]
例えば:
git config --global alias.ci commit

ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●ワークツリーの変更を取り消し
git checkout [--file name] ステージの状態に戻す

●ステージした変更を取り消す
git reset HEAD [--file name] コミットしたブランチの状態に戻す

●直前のコミットをやり直す(ローカルリポジトリ限定)
ファイル変更、ステージした後に、下記コマンドを実行
git commit --amend

ーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●プッシュ
まず、リモートリポジトリ新規作成
git remote add origin https://github.com/xiewenna123/intro_repo.git
※origin:リモートリポジトリ名、自分で定義する
 https://github.com/xiewenna123/intro_repo.git リポジトリ

あと、リモートリポジトリのマスタにプッシュ
git push origin master
もし、branch_aにプッシュする場合:
git push origin branch_a
git push [リポジトリ名][ブランチ名]

●git remote
リモート名一覧を表示する
●git remote -v
リモートリポジトリも表示したい時

●git remote show [リポジトリ名]
より詳細のリポジトリ情報を取得できる

●git remote rename [旧リポジトリ名] [新リポジトリ名]

●git remote rm [リポジトリ名]

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
リモートリポジトリからローカルに情報を反映させたい方法二つある
●フェッチする
①git fetch [リポジトリ名] リモートリポジトリの情報をダウンロードする
 
②git branch -a ブランチ名リストを表示
 
③git checkout [branch name] ブランチを切り替える(このステップはやらなくてもいい)
 
④git merge [リポジトリ名]/[ブランチ名] 指定したブランチと現在にいるブランチと統合させる

●プルする(上記の①+④と同じ意味)
git pull [リポジトリ名][ブランチ名]

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●github workflow
①ローカルリポジトリに新しいブランチを作成し、チェックアウトする。
git checkout -b new_branch
②資源を変更し、コミットする
git add [file_name]
git commit -v
③変更をプッシュする
git push [ローカルリポジトリ名] new_branch
④github画面でpull requestを新規し、レビューしてもらう
⑤github画面で「merge pull request」ボタンを押し、新ブランチを削除する
⑥ローカルの跡片付け処理をする
git checkout master
git pull origin master
git branch -d new_branch

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●git tag
すぐ切り戻しできるようにに、重要なコミットの後に付く(よくリリース時に使う)
git tag -a "tag name" -m "comment message"

タグ一覧を表示させる:
git tag

タグの詳細を表示させる
git show [tag_name]
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
●stash(一時退避する)
・作成(現在ワークツリーとステージにある変更ファイルを一時避難する、つまり、変更を隠すこと)
git stash

・一覧
git stash list

・復元
git stash apply

ステージの内容も復元する
git stash apply --index

特定のstashを復元する
git stash apply [stash name]

・削除
git stash drop

特定のstashを削除する
git stash delete [stash name]

全部削除
git stash clear

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