Skip to content

git basic command

git init
リポジトリの初期化

git clone [URL] リポジトリのクローンを作成

git pull

リモートリポジトリから取得  
※警告が出る場合は、git config pull.rebase_false で一旦回避。ブランチ構成複雑な場合は下記参照。  

Git 2.27 での git pull 時の warning について
https://qiita.com/tearoom6/items/0237080aaf2ad46b1963

git checkout
ブランチの切替え

git checkout -b branchname
ブランチを新規作成してそのブランチに切り替える

git branch
ブランチの一覧を表示

git branch -m new_branch_name
現在のブランチ名を変更

git branch -m old_branch_name new_branch_name
ブランチ「old_branch_name」の名前を「new_branch_name」に変更

git branch -d branch_name ブランチの削除。ただし現在のブランチは削除できない。

git status
現在のブランチの状態を表示

git add
変更履歴にファイルを追加

git diff --cached
→ addした時点での変更点を見る

git diff HEAD^
→ 今回のcommitとの変更点を見る

git log
コミットログを表示

git log -S 'xxxxx' --all  
※過去のコミットから対象文字列を含むコミットを検索。該当文字列のみを検索するので速い?  

git log -p xxxxx.txt  
※ -p コミット間の差分も表示。遅い。rev-listと同じかも。  
git rev-list HEAD  

指定したブランチ(またはコミット)から親ブランチをたどって到達できるコミットオブジェクトを逆年代順にリスト表示させる。時間の指定も可能  
git rev-list --after='2022-10-10' HEAD  # 2022-10-10以降に更新された  
git rev-list --after='1 hour ago' HEAD  # 1時間前以降に更新された  

authorの指定も可能  
git rev-list --author=xxxxxxxx HEAD  # xxxxxxxxが作成した  

マージされたものだけ対象  
git rev-list --merges HEAD  

応用すると、下記のようにrev-listで最新コミットから遡れるcommitを対象にgrep検索できる。  
git rev-list HEAD | xargs git grep xxxxx  

git commit -m "message"
git push origin branchname
git push -f origin branchname

指定したブランチを現在のブランチにマージ
git merge branchname
※事前にマージ先のブランチにcheckoutしていること!

リモートブランチの削除 git push origin --delete RemoteBranchName

GithubでのWeb上からのマージの仕方3種とその使いどころ
https://qiita.com/ko-he-8/items/94e872f2154829c868df
「Create a merge commit」「Rebase and merge」「Squash and merge」の違い

Git merge(Atlassian)
https://www.atlassian.com/ja/git/tutorials/using-branches/git-merge
様々なマージ方法が記載されている

【初心者向け】git fetch、git merge、git pullの違いについて https://qiita.com/wann/items/688bc17460a457104d7d git fetch…リモートの「master」ブランチ → ローカルの「origin/master」ブランチ
git merge…ローカルの「origin/master」ブランチ → ローカルの「master」ブランチ
git pull…上のgit fetch、git mergeを同時に行うコマンド
=リモートの「master」ブランチ →(ローカルの「origin/master」ブランチ→)ローカルの「master」ブランチ

git 環境設定

確認

git remote -v

git remote set-url origin git@github.com:xxx/xxx/xxxxx.git

-- ローカルリポジトリ作成(カレントディレクトリ)
git clone git@github.com:xxx/xxx/xxxxx.git
git pull git@github.com:xxx/xxx/xxxxx.git

git show e92db224
特定コミット時点の差分を見る?

git rebase
(参考)「git rebaseのやり方」
https://qiita.com/sugurutakahashi12345/items/8aa50b0171ea21ab85e3

git rebase -i HEAD~[0-99]
 指定した数字の数だけ前のcommitに遡ってrebase

git rebase -i
 指定した commit番号 でrebase

git merge
(参考)「git mergeとrebaseの違いについて」
https://qiita.com/72_mikan/items/0171795e9f20fe95f165


git grep
git grep -G ## 標準正規表現
git grep -E ## 拡張正規表現
git grep -l 'xxxxx' | xargs git grep -l 'yyyyyy'

.gitのssh設定

git secret

参考)Gitリポジトリ暗号化のススメ - git-secret -
https://qiita.com/jqtype/items/9b0524baa4b7fe6dbde0

-- git secretにメールアドレスを登録
git secret tell mailaddress@xxxx.com

-- アクセス許可のある人のリスト
git secret whoknows

-- 暗号化対象ファイルの登録
git secret add xxxxxxxx.txt

-- 暗号化(暗号化対象として登録されているファイルを暗号化)
git secret hide

-- 復号化(指定したディレクトリ以下が対象)
git secret reveal -v ./

-- gpgキーの削除
git secret killperson mailaddress@xxxx.com