0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Last updated at Posted at 2024-04-29

メモ用。都度追加する。

バージョン確認

git --version 

ユーザー名の確認

グローバル設定:システム全体に適用されるデフォルト設定

git config --global user.name 

ローカル設定:特定のリポジトリでのみ適用される設定

git config user.name

今いるブランチを確認

git branch

ブランチ作成

git checkout -b [新しいブランチ名]

リモートの特定のブランチから作りたいとき

git checkout -b [新しいブランチ名] [リモートリポジトリ名/リモートのブランチ名]

pull

git pull [リモート名] [ブランチ名]

ブランチ名を間違えた時

git branch -m worng_branch_name correct_branch_name

ファイルごとの差分を見たい時

git diff ファイルのパス

addした内容を消したい時

git checkout ファイルのパス

現在のブランチとリモートブランチの差分を確認する(どのコミットがpushされるか確認する)

git log origin/<ブランチ名>..HEAD

PR出したあとにコミットを取り消したい時

git reset {戻りたいコミット番号}

※「変更があるよ。」と怒られたら

git checkout -- . 

やりたい変更をやる

git add .
git commit -m ""

リモートにpush (だいたい「最新じゃないよ」と怒られるので、-f で 強制push)

git push -f origin ブランチ名

PR出したあとにコミットを修正したい時

修正したいファイルを編集し、内容を保存

git add ファイル名

コミットを上書きする

git commit --amend

修正したコミットを強制push

git push origin ブランチ名 --force

短縮コマンドを作成する

git config --global alias.(設定したい省略系) (元のコマンド)
#例
git config --global alias.co checkout
git config --global alias.bv ‘branch -vv

コミットメッセージを変更したい時

git add -A
git commit -m "htmlを修正"

#今のコミット、cssの修正だった!書き直したい…!
git commit --amend -m "cssを修正"

stash 作業内容を退避したい

現在の変更を退避する

git stash 

# 名前をつけたい時
git stash save "最強のmemo" 

退避した作業一覧を確認する  

git stash list

新しいものから上に表示される。

stash@{0}: WIP on master: 36af2d1 added index.txt 
stash@{1}: WIP on master: f0d73fe added readme
stash@{2}: WIP on master: 3faa214 feat readme

対比した作業を元に戻す

git stash pop

# 指定する
git stash pop stash@{1}

ブランチ切って作業始めるとき

git fetch 
git pull upstream master  
git checkout -b new_branch

ローカルで他人のPRを動作確認したい時

git fetch upstream pull/{PR番号}/head:{ブランチ名}
git co {ブランチ名}

revert

過去のコミットを打ち消す新しいコミットを作る

git revert abc1234
  • コミット abc1234 を打ち消す新しいコミットを作成
  • 履歴を残しつつ、変更を取り消す
  • 変更を元に戻す「逆の操作」をする新しいコミットの追加

reset

コミット履歴自体をなかったことにする

git reset --hard HEAD~1
  • コミット履歴を書き換える
  • ローカルでしか使わないのが基本(force push しないと共有リポジトリに反映できない)
  • -hard は要注意!消える!

cherry-pick

ある特定のコミットだけを、今のブランチに取り込む

git cherry-pick <コミットハッシュ>

使いどころ

  • 他のブランチの特定の修正だけを取り込みたい

  • リリースブランチに、特定のバグ修正だけ反映したい

  • 間違って別のブランチにコミットしてしまった内容を移したい

0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?