0
0

Git

Last updated at Posted at 2021-09-04

リモートリポジトリをクローンし、masterブランチをチェックアウトする(ディレクトリ=プロジェクト名)

git clone https://github.com/foo/hoge.git

リモートリポジトリをクローンし、masterブランチをチェックアウトする(ディレクトリ≠プロジェクト名)

git clone https://github.com/foo/hoge.git fuga

ローカルリポジトリを作成する

git init

リモートリポジトリを表示する

git remote -v

リモートリポジトリoriginを登録する

git remote add origin https://github.com/ueki5/hoge.git

リモートリポジトリoriginのurlを変更する

git remote set-url origin https://github.com/ueki5/hoge.git

リモートリポジトリoriginの登録を取り消す

git remote rm origin

フォーク元のリポジトリをリモートリポジトリupstreamとして登録する

git remote add upstream https://github.com/foo/hoge.git

フォークしたプロジェクトを本家に追随

git fetch upstream
git checkout master
git merge upstream/master

ローカルブランチからリモートブランチへPUSHする(無ければ作る)

git push origin <local-branch>:<remote-branch>

ローカルブランチとリモートブランチの紐づけ(トラッキング)を確認

git status -sb
# 以下のコマンドと同じ
git branch -vv

ブランチを切り替える

git switch foo
# 以下のコマンドと同じ
git checkout foo

checkoutとの違い
switchコマンドは、ブランチの切り替えや新しいブランチの作成に特化している。
checkoutコマンドは、ブランチの切り替えや新しいブランチの作成に加えて、作業ディレクトリのファイル変更などの複数の機能を持ち、より汎用的である。
switchコマンドは、git mergeやgit rebaseなどの他のGitコマンドと組み合わせて使用することで、ブランチの統合やリベース作業を効率的に行うことができるはず?

現在のローカルブランチからローカルブランチfooを作成し、ブランチを切り替える。

git switch -c foo
# 以下のコマンドと同じ
git branch foo
git switch foo
# 以下のコマンドと同じ
git checkout -b foo

ローカルブランチfooを削除

git branch --delete foo

リモートブランチfooを削除

git push origin --delete foo


git push origin :foo

ブランチの名前を変更

git branch -m <ブランチ名> <変更後ブランチ名>

今いるブランチの名前を変更

git branch -m <変更後ブランチ名>

追跡ブランチの切り替え

リモートブランチを追跡する新しいローカルブランチを作成する

git switch --track origin/<ブランチ名>

リモートブランチと現在のローカルブランチの紐づけ(トラッキング)を行う

git branch -u origin/<remote-branch>

リモートブランチとローカルブランチの紐づけ(トラッキング)を行う

git branch <local-branch> -u origin/<remote-branch>

現在のローカルブランチの紐づけ(トラッキング)を外す。

git branch --unset-upstream

ローカルブランチの紐づけ(トラッキング)を外す。

git branch <local-branch> --unset-upstream

フォーク元の変更をマージ

git fetch upstream
git checkout master
git merge upstream/master

ブランチの一覧を表示する

git branch -a

タグの一覧を表示する

git tag

configの一覧を表示

git config --list
git config --local --list
git config --global --list
git config --system --list

configを表示(は項目名)

git config <name>

configを変更(は項目名、は項目値)

git config <name> <value>

configの設定を削除(は項目名)

git config --unset <name>

configの定義場所を表示(は項目名)

git config --show-origin --list
git config --show-origin <name>

リポジトリを別サーバへ移行

git clone --bare https://github.com/ueki5/hoge.git
cd hoge.git
git push --mirror https://xxxx.com/ueki5/hoge.git

※githubでは事前に同名の空プロジェクトを作成しておく必要がある。
その場合、移行先のmasterブランチのprotectを解除しておく事(設定→リポジトリ→Unprotect)

管理外ファイルを削除する(テスト実行)

git clean -dfn

管理外ファイルを削除する

git clean -df

過去履歴をgrep

git grep -w '検索ワード' $(git rev-list --all)

過去履歴をgrep(検索に拡張正規表現を使う)

git grep -E -w '検索ワード' $(git rev-list --all)

とりあえず入れとくやつ

git config --global user.name ueki5
git config --global user.email ueki005@gmail.com

githubでリポジトリを作ったら・・・

## …or create a new repository on the command line
echo "# rust_zero2" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/ueki5/rust_zero2.git
git push -u origin main
## …or push an existing repository from the command line
git remote add origin https://github.com/ueki5/rust_zero2.git
git branch -M main
git push -u origin main

submoduleの更新

git submodule update

親モジュールでbranchを変えてもsubmoduleのbranchは変更されない。(modifyした様に表示される)
branch内でsubmoduleの参照branchを変えている場合、手動で追随させる必要がある

以下のエラーが発生する場合

暫定対応:通常はエラー発生しないため設定を戻す(http.sslVerify true)こと

SSL certificate problem: self signed certificate in certificate chain

git config --global http.sslVerify false
0
0
1

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