githubについて
githubのコマンドを纏めました。
コマンド一覧
clone
リポジトリをコピー
git clone https://github.com/fugafuga/hoge-mapred.git(リポジトリのURL)
fetch
ローカルにリモートリポジトリの情報を反映する。
git fetch
push
リモートリポジトリにローカルブランチの情報を反映する。
※新規ブランチをリモートに反映させる場合はtoをorigin
git push origin feature/fugafuga
branch
ブランチ情報の確認する。
今のローカルブランチを確認
git branch
全てのブランチを確認する
git branch -a
ブランチを削除する
git branch -D feature/hogefugabra(ブランチ名)
checkout
ローカルブランチを変更する
git checkout master
新規のローカルブランチを作成し、そのブランチに変更する
git checkout -b feature/fugafuga
stash
現在の状態を保存する
git stash
保存状態をリストに残したまま最新の保存状態を復元する
git stash apply
log
コミットログを参照する
git log
add
変更をコミット対象に追加
git add src/main/java/com/hogefuga/batch/mapreduce/hogehoge/hogeTransform.java 新規ファイルも同様
status
ローカルブランチの状態を表示する
※どのブランチにいるか、変更ファイル名を表示
git status
diff
インデックスとワーキングツリーの差異を表示
git diff
ブランチ間差分を表示
git diff feature/fugafuga feature/fugafuga_chk
commit
インデックスに追加されたファイルをコミットする
※ -m “[コメント]”でコミットメッセージを追加
git commit -m “[コメント]”
Git を使った開発作業
普段の開発作業は基本この流れになるので繰り返して覚えましょう。
1. masterブランチに移動する
git checkout master
2 リモートの master 最新状態をローカルの master に引っ張ってくる
git pull
3.新たなブランチを作成しつつ、それに移動する
git checkout -b ブランチ名 (最新の master の頂点を根にして)
4.コミットの大きさはプロジェクトとかの文化や作業者の好みによる。
※「その作業に名前がつけられる」まで作業したらコミットすると良い
ブランチ名=プロジェクト名、コミット=タスク
タスクレベルの作業をする
5.余計なものいじってなければプロジェクトのルートディレクトリ以下の変更をすべて stage していいはず
git add .
6.stage した変更を現在のブランチに積む。-m を忘れると vi や nano 開いて右往左往するので、忘れない要注意。
git commit -m "変更についての説明"
7.4〜6 を繰り返す
8.今いるローカルブランチ の commit をリモートの同名ブランチに反映する。初回は -u つけないと push できない
git push -u origin HEAD
9.4〜6 を繰り返す
10.今いるローカルブランチ の commit をリモートの同名ブランチに反映する。
git push
豆知識
gitの差分比較時の注意点
gitのdiffには1点落とし穴があり、差分比較の際に改行コードの変更が表示されないケースがある。
⇒防止策としては、比較ブランチ(コミット)について、順番を入れ替えて2パターンとってみると出るはず。
また、windows上で差分を見てしまうと、何かしらのツールがご親切に改行コードを変えて下さることがあるため要注意。
Githubのtips
差分比較のURLのリクエストパラメータに「w=1」を追加すると、
「インデント(空白、タブ)のみの差分」「改行コードの差分」を無視して表示してくれる。
ロジックのみの差分を見たい場合には使える。
pushができない!!!
githubにほかの人のアカウントでアクセスしようとしている可能性がある。「~/.netrc」にgithubのID/PWを設定してあげればイケることもあるかもしれない。
(これでだめだったら鍵周り?)
git ローカルの変更を元に戻したい
・ローカルを色々弄ったけど元に戻したい。
・リモートリポジトリの内容をマージしたいのに、マージできない。
・リモートのmainリポジトリ内容をまるっとローカルのmainリポジトリに反映しようとしてエラーが出た!
等が、発生した場合は、以下のコマンド。
git checkout <filename>
特定のファイルではなく、全て元に戻したい場合
git checkout .
パーミッションを変更する。(ブランチ作成~リモートリポジトリアップまで)
# git cloneする
[euser@pd-in1 work_rei]$ pwd
/home/euser/work_rei
[euser@pd-in1 work_rei]$ git clone https://github.com/hogefuga/mae-ushiro.git
Cloning into 'mae-ushiro'...
Username for 'https://github.com': hoge-fuga-rei
Password for 'https://hoge-fuga-rei@github.com':
remote: Counting objects: 42120, done.
remote: Compressing objects: 100% (44/44), done.
remote: Total 42120 (delta 26), reused 19 (delta 9), pack-reused 42067
Receiving objects: 100% (42120/42120), 70.91 MiB | 1.06 MiB/s, done.
Resolving deltas: 100% (15676/15676), done.
Checking connectivity... done.
[euser@pd-in1 work_rei]$ ll
total 4
drwxrwxr-x 15 euser euser 4096 Jan 16 01:20 mae-ushiro
[euser@pd-in1 mae-ushiro]$ pwd
/home/euser/work_rei/mae-ushiro
[euser@pd-in1 mae-ushiro]$ ll dataspider/custom/*/*
-rwxrwxr-x 1 euser euser 16672 Jan 16 01:20 dataspider/custom/1003/saison_etl.sh
-rwxrwxr-x 1 euser euser 8565 Jan 16 01:20 dataspider/custom/1003/saison_na.sh
-rwxrwxr-x 1 euser euser 13236 Jan 16 01:20 dataspider/custom/1013/hogehoge_sftp_get.sh
-rw-rw-r-- 1 euser euser 9302 Jan 16 01:20 dataspider/custom/1043/fugafuga_sftp_get.sh
# branch作成
[euser@pd-in1 mae-ushiro]$ git branch feature/23262_custom_1043_fugafuga
# 作成したbranchに切替
[euser@pd-in1 mae-ushiro]$ git checkout feature/23262_custom_1043_fugafuga
Switched to branch 'feature/23262_custom_1043_fugafuga'
# 現状のパーミッションを確認
[euser@pd-in1 mae-ushiro]$ ll dataspider/custom/*/*
-rwxrwxr-x 1 euser euser 16672 Jan 16 01:20 dataspider/custom/1003/saison_etl.sh
-rwxrwxr-x 1 euser euser 8565 Jan 16 01:20 dataspider/custom/1003/saison_na.sh
-rwxrwxr-x 1 euser euser 13236 Jan 16 01:20 dataspider/custom/1013/hogehoge_sftp_get.sh
-rw-rw-r-- 1 euser euser 9302 Jan 16 01:20 dataspider/custom/1043/fugafuga_sftp_get.sh
# パーミッション変更
[euser@pd-in1 mae-ushiro]$ chmod 775 dataspider/custom/1043/fugafuga_sftp_get.sh
# パーミッション変更結果の確認
[euser@pd-in1 mae-ushiro]$ ll dataspider/custom/*/*
-rwxrwxr-x 1 euser euser 16672 Jan 16 01:20 dataspider/custom/1003/saison_etl.sh
-rwxrwxr-x 1 euser euser 8565 Jan 16 01:20 dataspider/custom/1003/saison_na.sh
-rwxrwxr-x 1 euser euser 13236 Jan 16 01:20 dataspider/custom/1013/hogehoge_sftp_get.sh
-rwxrwxr-x 1 euser euser 9302 Jan 16 01:20 dataspider/custom/1043/fugafuga_sftp_get.sh
# gitのuser情報がまだ設定されていないことを確認
[euser@pd-in1 mae-ushiro]$ git config --list
user.name=tk-sai
user.email=tk.sai@hogefuga.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/hogefuga/mae-ushiro.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.develop.remote=origin
branch.develop.merge=refs/heads/develop
# gitのuser情報を設定
[euser@pd-in1 mae-ushiro]$ git config --global user.name "hoge-fuga-rei"
[euser@pd-in1 mae-ushiro]$ git config --global user.email "rei.7010@hogefuga.com"
# gitのuser情報が設定されたことを確認
[euser@pd-in1 mae-ushiro]$ git config --list
user.name=hoge-fuga-rei
user.email=rei.7010@hogefuga.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/hogefuga/mae-ushiro.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.develop.remote=origin
branch.develop.merge=refs/heads/develop
# git commit
[euser@pd-in1 mae-ushiro]$ git commit -a -m "23262 fugafuga-col permission change"
[feature/23262_custom_1043_fugafuga 0e3a672] 23262 fugafuga-col permission change
1 file changed, 0 insertions(+), 0 deletions(-)
mode change 100644 => 100755 dataspider/custom/1043/fugafuga_sftp_get.sh
# git push
[euser@pd-in1 mae-ushiro]$ git push origin feature/23262_custom_1043_fugafuga
Username for 'https://github.com': hoge-fuga-rei
Password for 'https://hoge-fuga-rei@github.com':
Counting objects: 5, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 453 bytes | 0 bytes/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/hogefuga/mae-ushiro.git
* [new branch] feature/23262_custom_1043_fugafuga -> feature/23262_custom_1043_fugafuga
# git pullしてpermissionが変更されたことを確認するために、別ディレクトリでgit cloneして確認
[euser@pd-in1 mae-ushiro]$ cd ..
[euser@pd-in1 work_rei]$ mkdir git_pull_test
[euser@pd-in1 work_rei]$ cd git_pull_test/
[euser@pd-in1 git_pull_test]$ git clone https://github.com/hogefuga/mae-ushiro.git
Cloning into 'mae-ushiro'...
Username for 'https://github.com': hoge-fuga-rei
Password for 'https://hoge-fuga-rei@github.com':
remote: Counting objects: 42125, done.
remote: Compressing objects: 100% (47/47), done.
remote: Total 42125 (delta 28), reused 21 (delta 10), pack-reused 42067
Receiving objects: 100% (42125/42125), 70.91 MiB | 1.97 MiB/s, done.
Resolving deltas: 100% (15678/15678), done.
Checking connectivity... done.
[euser@pd-in1 git_pull_test]$ cd mae-ushiro/
[euser@pd-in1 mae-ushiro]$ pwd
/home/euser/work_rei/git_pull_test/mae-ushiro
[euser@pd-in1 mae-ushiro]$ ll dataspider/custom/*/*
-rwxrwxr-x 1 euser euser 16672 Jan 16 01:31 dataspider/custom/1003/saison_etl.sh
-rwxrwxr-x 1 euser euser 8565 Jan 16 01:31 dataspider/custom/1003/saison_na.sh
-rwxrwxr-x 1 euser euser 13236 Jan 16 01:31 dataspider/custom/1013/hogehoge_sftp_get.sh
-rw-rw-r-- 1 euser euser 9302 Jan 16 01:31 dataspider/custom/1043/fugafuga_sftp_get.sh
[euser@pd-in1 mae-ushiro]$ git checkout feature/23262_custom_1043_fugafuga
Branch feature/23262_custom_1043_fugafuga set up to track remote branch feature/23262_custom_1043_fugafuga from origin.
Switched to a new branch 'feature/23262_custom_1043_fugafuga'
[euser@pd-in1 mae-ushiro]$ ll dataspider/custom/*/*
-rwxrwxr-x 1 euser euser 16672 Jan 16 01:31 dataspider/custom/1003/saison_etl.sh
-rwxrwxr-x 1 euser euser 8565 Jan 16 01:31 dataspider/custom/1003/saison_na.sh
-rwxrwxr-x 1 euser euser 13236 Jan 16 01:31 dataspider/custom/1013/hogehoge_sftp_get.sh
-rwxrwxr-x 1 euser euser 9302 Jan 16 01:32 dataspider/custom/1043/fugafuga_sftp_get.sh
間違ったリモートリポジトリを削除する。
#gitがないと言われたら
[rei@prd ~]$ git init
#origin情報を確認
git remote -v
#不要なoriginは消す
[rei@prd ~]$ git remote rm origin
#なりたいoriginになる
[rei@prd ~]$ git remote add origin https://github.com/hogefuga/ganon-ingest.git
#origin情報を確認
[rei@prd ~]$ git remote -v
origin https://github.com/hogefuga/ganon-ingest.git (fetch)
origin https://github.com/hogefuga/ganon-ingest.git (push)
#間違ったリモートリポジトリを消す
[rei@prd ~]$ git push origin :feature/57831_customizedtags_20180510
#名前聞かれる
Username for 'https://github.com': rei
#パスワード聞かれる
Password for 'https://rei@github.com':
To https://github.com/hogefuga/ganon-ingest.git
- [deleted] feature/57831_customizedtags_20180510
##実例↓↓
[rei@prd ~]$ git init
Reinitialized existing Git repository in /home/rei/.git/
[rei@prd ~]$ git remote -v
origin git@github.com:hogefuga/ganon-ingest.git (fetch)
origin git@github.com:hogefuga/ganon-ingest.git (push)
[rei@prd ~]$ git remote rm origin
[rei@prd ~]$ git remote -v
[rei@prd ~]$ git remote add origin https://github.com/hogefuga/ganon-ingest.git
[rei@prd ~]$ git remote -v
origin https://github.com/hogefuga/ganon-ingest.git (fetch)
origin https://github.com/hogefuga/ganon-ingest.git (push)
[rei@prd ~]$ git tag -d feature/57831_customizedtags_20180510
error: tag 'feature/57831_customizedtags_20180510' not found.
[rei@prd ~]$ git push origin :feature/57831_customizedtags_20180510
Username for 'https://github.com': rei
Password for 'https://rei@github.com':
To https://github.com/hogefuga/ganon-ingest.git
- [deleted] feature/57831_customizedtags_20180510
参考資料
pullがfetchとmergeの両方を組み合わせたコマンド
http://qiita.com/osamu1203/items/cb94ef9da02e1ec3e921
git reset
http://d.hatena.ne.jp/dentaq/20120419/1334835718
git Untracked files(未追跡ファイル)を1発で消し去る(git clean)
http://qiita.com/harapeko_wktk/items/f329ba818690bdee7dd8
作業退避したい 退避した作業を操作したい(git stash)
http://www.backlog.jp/git-guide/reference/stash.html
デフォルトのブランチを設定する。
https://qiita.com/unsoluble_sugar/items/0603187d5a732f91d502