hiron0424
1/23/2020 - 6:32 AM

よく使うgitコマンドまとめ

init

# create a new repository
echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin <repo_url>
git push -u origin main

# push an existing repositor
git remote add origin <repo_url>
git branch -M main
git push -u origin main

remoteの別名ブランチにpush

$ git push origin HEAD:pushしたいブランチ名

git rebase

// masterブランチに移動
$ git checkout master

// リモートのマスターと同期
$ git pull origin master
// 若しくは
$ git fetch origin master
$ git merge origin master

// 作業ブランチに移動
$ git checkout -b hogehoge

// 作業ブランチにてリベース実行(リモートと同期させたローカルのマスターからrebase)
$ git rebase master

2回目以降のgit add を取り消す

  • 全てのファイルを取り消し
git reset HEAD
  • 特定のファイルのみ取り消し
git reset HEAD file_name

git管理しているトップディレクトリを表示

git rev-parse --show-superproject-working-tree --show-toplevel | head -1

リモートリポジトリの追加、変更

  • 追加
# git remote add リポジトリ名 追加するリポジトリ
$ git remote add origin hogehoge
  • 変更
$ git remote set-url origin (new url)

リモートマスターからリベース

$ git fetch && rebase origin/master

間違ってpushした際

# コミットID確認
git log

# revert
git revert <commit-id>

# push
git push orgin hogehoge

commit日時指定

git commit -m "hoge" --date="Sep 9 17:00:30 2021 +0900"

空commit

git commit --allow-empty -m "fuga"

リモートブランチ削除

git push --delete origin branch_name

# もしくは
git push origin :branch_name

リモートで削除されたブランチ情報の同期

git fetch -p

addを取り消し(2回目以降add)

git reset HEAD

### ファイル名指定
git reset HEAD file_name

ファイル追加を取り消し

git clean -df .

日本語ファイル名を表示

# 使用リポジトリのみ
git config --local core.quotepath false

# お好みでglobalに
git config --global core.quotepath false