ずっと避けてたgitに挑戦。
ベアーリポジトリ(共有)を作成
1 |
git init --bare --shared=true |
※ベアーリポジトリはワークツリーを持たない。
naokirin.hatenablog.com/entry/20111201/1322576109
リポジトリからcloneを作成する
1 |
git clone [repo_dir|url] |
※ベアーリポジトリからcloneするとワークディレクトリが自動作成される。
1 |
git remote -v |
1 |
git remote set-url origin https://gitlab.com/aaa/bbb.git |
ワークツリーのファイルをステージングエリアに追加
1 2 |
git add [filepattern] // ファイルを指定 git add . // すべてのファイル |
ステージングエリアの情報をリポジトリに登録する
1 2 |
git commit -m "コメント文" // -mオプションでエディタを起動しないでコメントを入力できる git commit -a // 変更が加えられたファイルを自動検出してコミット。ただ、新規ファイルは対象外。 |
ステージングエリアをgitリポジトリの状態に戻す
1 |
git reset HEAD [file] |
ワークツリーをステージングエリアの状態に戻す
1 |
git checkout -- [file] |
ファイルを削除する
1 |
git rm [file] |
transitive.info/article/git/command/rm/
ファイルやディレクトリを移動する
1 |
git mv file.txt dir |
リネーム
1 |
git mv old.txt new.txt |
ファイルの変更点を確認
1 |
git diff [file] |
コミット間の変更ファイル一覧を出力する
1 |
git diff --stat [commit] [commit] |
データ領域を指定コミット状態に戻す
1 |
git reset --hard HEAD^ |
ワークツリーの状態を見る
1 |
git status |
コミットログを見る
1 |
git log |
プル
1 |
git pull origin master |
マージ
1 |
git merge origin master |
ローカルブランチを削除
1 |
git branch -d BRANCH_NAME |
リモートブランチを削除
1 |
git push origin :BRANCH_NAME |
remoteブランチに関するtrack情報を消す 参考
1 2 |
git remote prune origin --dry-run //対象ブランチの確認 git remote prune origin // 実際に消す |
リモートブランチが表示されないときは下記を実行
1 |
git fetch origin |
リモートブランチをローカルにチェックアウトする
1 |
git checkout -b LOCAL_BRANCH_NAME origin/BRANCH_NAME |
プッシュ
git push [リポジトリ] [ブランチ名]
push に -u オプションを付けるとpush やpullでリポジトリを省略できる
gitignoreでgit管理から除外するファイルを指定
qiita.com/rohinomiya/items/3629eafcd3786a973391
github.com/github/gitignore
空のディレクトリをgit管理の対象にする
ディレクトリに.gitkeepを作成しておく。
中は空でOKで、ファイル名は.gitkeep以外でもOK。
リモートリポジトリとの差分を見る
tm.root-n.com/unix:command:git:operation:diff_from_remote_to_working
1) リモートからデータ取得する(ワークツリーにマージはしない)
1 |
git fetch origin |
2) 1で取得したデータとワークツリーの差分をみる
1 |
git diff HEAD..FETCH_HEAD |
新しいブランチの作成
ローカルブランチ作成する。
1 |
git branch NEW_BRANCH_NAME |
現在のbranchの確認
1 |
git branch -av |
リモートブランチに追加されたか確認
1 |
git branch -av |
ブランチの切替
1 |
git checkout NEW_BRANCH_NAME |
リモートブランチにNEW_BRANCH_NAMEを登録する。
1 |
git push origin NEW_BRANCH_NAME |
.gitignoreを反映させる
1 2 3 |
git rm -r --cached . git add . git commit -m ".gitignore is now working" |