背景
- 今まで存在は知っていて便利そうだなとは思っていたが調べると設定やらコマンドやら多くてなかなか踏み出せていなかった。
- 職場で私以外が利用している状況となり、使う決心がついた。
- 職場では共同で開発をするというより、個人のリポジトリでスクリプトを共有をするという使い方。
- 個人で利用することに特化してまとめる。
環境
OS:ubuntu
用語
ファイル保存領域
名称 | 説明 |
---|---|
ワーキングツリー | ユーザの作業ディレクトリ領域 |
ステージング | ワーキングツリーとリポジトリの中間領域 |
リポジトリ | ファイルやディレクトリを管理する領域 |
その他
名称 | 説明 |
---|---|
HEAD | ブランチの最新のコミット |
リモートリポジトリの設定
githubにアカウント登録する
- https://github.com/ にアクセスして登録する
ためしにgithubリポジトリを作成する
- リポジトリはフォルダみたいなもの
- rootフォルダごとに管理していく
- リモートリポジトリと呼ぶ
作成手順
- https://github.com の右上の「+」をクリック
- 「New repository」をクリック
- 「Repository name」を入力
- 「Initialize this repository with a README」にチェックを入れる
- 「Create repository」をクリック
- Privateは有料会員のみ
- 「Initialize this repository with a README」にチェックを入れないと以下コマンドを打つことになるのでちょっとめんどくさい
$ touch README.md
$ git init
$ git add README.md
$ git commit -m "first commit"
$ git remote add origin https://github.com/<user name>/test.git
$ git push -u origin master
ローカルポジトリの設定
ubuntuにgitをインストール
$ sudo apt-get install -y git
初期設定
ユーザネームとメールアドレスを設定する
$ # ユーザネーム
$ git config --global user.name "ユーザ名"
$ # メールアドレス
$ git config --global user.email メールアドレス
$ # コマンドの出力を読みやすくする
$ git config --global color.ui auto
設定を確認する
$ git config --list
リモートリポジトリに編集内容を反映させる
流れ
- リモートリポジトリを取得する(git clone)
- ワーキングツリーで編集する
- ステージングエリアに追加する(git add)
- コミットをする(git commit)
- リモートリポジトリに反映させる(git push)
リモートリポジトリを取得する(git clone)
- リモートリポジトリをローカルにcloneして同一の環境を持ってくる
$ git clone https://github.com/ユーザー名/リポジトリ名
ワーキングツリーで編集する
ファイルを作成する
$ vim test.txt
ファイルの状態を確認する
- addとcommitされていない状態
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
ステージングエリアに追加する(git add)
- 少しコメントが変わる
$ git add test.txt
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: test.txt
コミットをする(git commit)
- 記録を残す
- その際にコメントを残す
-
commit
をしないとgit push
できない
1行だけの場合は「-m」オプションを付ける
$ git commit -m "first commit"
master c2f36b2 first commit
1 file changed, 1 insertion(+)
create mode 100644 test.txt
複数行のコメントを残す場合はオプションを付けない
$ git commit
状態を確認する
- 最新の状態となっている
$ git status
On branch master
nothing to commit, working directory clean
リモートリポジトリに反映させる
- ユーザ名とパスワードを入力する
$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.
In Git 2.0, Git will default to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Username for 'https://github.com': ユーザー名
Password for 'https://ユーザー名@github.com':
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 263 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local objects.
To https://github.com/ユーザー名/リポジトリ名
05af7fb..c2f36b2 master -> master
※git pushの際認証を省略させる方法
~/.netrc
に以下を記載する
machine github.com
login <username>
password <password>
リモートリポジトリの変更を取り込む
$ git pull
エリアとの差分を確認する
ローカルリポジトリとリモートリポジトリの差分を確認する
$ git fetch origin
$ git diff origin/master
ステージンエリアとワーキングツリーの差分を確認する
$ git diff
ファイルを削除する
$ git rm
ファイルを移動、リネイムする
$ git mv
ユーザ名、パスワードを省略する
/.netrc
machine github.com
login username
password ***