nishimura.clubnishimura.club

Gitの使い方まとめ

作成日
2021-04-12
更新日
2022-09-16

Gitのバージョンアップ

git --version brew install git git --version

Gitのデフォルトブランチ名をmain変更

git config --global init.defaultBranch main

ユーザー設定

git config --global user.name "John Doe" git config --global user.email johndoe@example.com

alias設定

# system git config --system alias.co checkout git config --system alias.br branch git config --system alias.cm commit git config --system alias.st status # global git config --global alias.co checkout git config --global alias.br branch git config --global alias.cm commit git config --global alias.st status # local git config --local alias.co checkout git config --local alias.br branch git config --local alias.cm commit git config --local alias.st status

config確認

# local cat {repo}/.git/config # global /Users/user/.gitconfig # system cat /usr/local/etc/gitconfig

.gitconfigが無い場合

rm ~/.gitconfig touch ~/.gitconfig

リモートリポジトリの確認

git remote -v

追加

git remote add origin {url}

変更

git remote set-url origin {url}

ローカルの直前コミットを戻す

git reset --soft HEAD^

合わせて、ステージを戻す

git reset

リモートの直前コミットを戻す

git reset --hard HEAD^
git push origin +master git push origin +develop

上記は履歴が残らないので、履歴を残したい場合は、revertを使いましょう。

commitとは逆順番で実行する。

マージ済ブランチの削除

git branch -D `git branch --merged | grep -v \* | xargs`

特定のコミットを打ち消す

ログを調べて、コミットのハッシュを特定する。

git log tig
git revert {ハッシュ値} # merge commitのrevert git revert -m 1 {ハッシュ値}
git revert --continue

ブランチ名の変更

git branch -m <古いブランチ名> <新しいブランチ名> git push origin :<古いブランチ名> # ついでに削除

unstagedファイルを削除

git clean git clean -f # 強制 git clean -df # ディレクトリも含める

特定のファイルを特定のコミットに戻す

git checkout [コミット番号] [ファイルパス]

GitHub SSH接続

  • SSHキーを作成し、~/.ssh/configに記載、GitHubにSSH公開鍵を設定する。

GitHub でパスワードを変更した後

git config --global user.name <username> git pull # なんでも良い

を実行し、パスワードを聞かれるので、入力する。

Personal Access Token設定後のMac設定

$ git credential-osxkeychain erase ⏎ host=github.com ⏎ protocol=https ⏎
git fetch

How do you reset the stored credentials in 'git credential-osxkeychain'?

credential-osxkeychainの設定

git credential-osxkeychain

認証アシスタントがインストールされていない場合は、以下の内容が表示されます。

git: 'credential-osxkeychain' is not a git command. See 'git --help'.

この時点で、認証アシスタントを正しいパスにダウンロードすることができます。

curl -s -O http://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain chmod u+x git-credential-osxkeychain mv git-credential-osxkeychain /usr/local/bin

インストール後、オープン認証アシスタントの設定を行います。

git config --global credential.helper osxkeychain

GitHub for Mac error: git: 'credential-osxkeychain' is not a git command

Rebase

git rebase [つなぐ元にするブランチ名] git rebase origin/[つなぐ元にするブランチ名] # コンフリクト解消 git rebase --continue

元ブランチの確認

git show-branch

git blame

行ごとに差分を見る

git blame <paths>

大文字と小文字のファイル検知

$ git config -l --local | grep core.ignorecase core.ignorecase=true これをfalseにしてあげたらOK. $ git config core.ignorecase false

Tagの利用

git tag v1.0.0 git push --tags
git tag v1.0.0 git push origin v1.0.0

localにあるすべてのtagが更新されてしまうので、ちゃんと絞って実行する。

package.jsonのバージョンも変えないと、packageは、publishされない。その場合は、yarn versionコマンドを叩く。

yarn version | Yarn

yarn version # input next version (X.X.X) git push origin vX.X.X

git管理から削除

git rm client/.env

削除

git tag -d v2.1.0

直前のコミットの修正

git commit --amend -m "修正するコメント"

コンフリクトの解消

git merge --continue

rebaseの取り消し

git rebase --abort

git - Completely cancel a rebase - Stack Overflow

git commitをまとめる

git status -s

git commit --amendで3つのコミットを1つのコミットにまとめる - Qiita

git reset --soft @~2 git add . git commit --amend -m "fix ----" # or git commit --amend --no-edit

git--amend--no-edit拒否されたプッシュ-スタックオーバーフロー

pushがrejectされる場合

git push -f origin head

git show

git show

pullとpushが被ってしまった場合

git reset --hard HEAD~

特定のブランチから別のブランチにpushする

actionでデプロイする時とかに便利だったり

git co feature/xxx # deployしたいブランチ git push -f origin HEAD:feature/zzz

削除されたブランチも含めて

git fetch --prune

cherry-pick

git cherry-pick <HASH>

Related