5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[基礎]Gitコマンドについてまとめてみた①

Last updated at Posted at 2021-06-07

はじめに

実は共同開発は愚か、GithubDesktopを使用していたため、あまりコマンド操作になれてなかったり、リベース周りに慣れていなかったりするのでこちらにアウトプットしていきます。
入社までに完璧にするぜ!

見づらさはご了承ください

基本的なコマンド

ローカルリポジトリの新規作成

.gitディレクトリが作成される。

  • 圧縮ファイル
  • ツリーファイル
  • コミットファイル
  • インデックスファイル
  • 設定ファイル
terminal
git init

リモートリポジトリのコピーを作成

terminal
git clone リポジトリ名

変更をステージに上げてからコミット

terminal
git add ファイル名
git add ディレクトリ名

git commit -m(-v) "コミットメッセージ" 

差分を調べる

terminal
# git addする前の変更分

git diff
git diff ファイル名

# git add した後の変更分
git diff --staged

変更履歴の確認

terminal
git log

git log --oneline #1行で表示する

git log -p index.html #ファイルの変更差分を表示する

git log -n コミット数 #表示数の制限

ファイルの削除

terminal
# リモートもローカルでも消す場合
git rm ファイル名
git rm -r ディレクトリ名

# リモート側のみでローカルには残したい時
git rm --cached

ファイルの移動を記録する

terminal
git mv 旧ファイル 新ファイル

# 上記コマンドは以下をまとめたもの
mv 旧ファイル 新ファイル
git rm 旧ファイル
git add 新ファイル 

リモートリポジトリを新規追加する

慣例的にoriginにすることが多いが、当然それ以外でもOK

terminal
git remote add origin https://github.com/~~~~

プッシュする

terminal
git push リモート名 ブランチ

git push origin master 

コマンドにエイリアスをつける

terminal

git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch

変更を元に戻すコマンド

ワークツリーでファイルへの変更を取り消す

ブランチを切り替えるコマンドとよく似ているので混同しないように注意

terminal
git checkout -- ファイル名
git checkout -- ディレクトリ名

# 全変更を取り消す魔法の言葉
git checkout -- . 

ステージした変更を取り消す

git addした後にやっぱなし!をしたい時
ワークツリーには影響なし

terminal
git reset HEAD ファイル名
git reset HEAD ディレクトリ名

# ステージの全変更を消す
git reset HEAD 

直前のコミットをやりなおす方法

ただし、リモートリポジトリにPushしたコミットはやり直し厳禁

terminal
git commit --amend

Githubとあれこれするタイプのコマンド

リモート表示

terminal
git remote

# 対応するURLを表示
git remote -v

リモートリポジトリを新規追加

terminal
git remote add リモート名 リモートURL

git remote add tutiral htpps://~~~~~

リモートから情報を取得(フェッチ)

git fetch はリモートリポジトリからローカルリポジトリへ下ろすだけ
ワークツリー

terminal
git fetch リモート名
git fetch origin 

リモートから情報を取得してマージする (プル)

terminal
git pull リモート名 ブランチ名

# 上記コマンドは下記と同じ
git fetch origin master
git merge origin/master

リモートの詳細

terminal
git remote show リモート名
git remote show origin

リモートの変更

terminal
git remote rename 旧リモート名 新リモート名

リモートの削除

terminal
git remote rm リモート名

続き

まだまだリベースとかもやっていきま〜す!!↓
https://qiita.com/tochisuke221/items/ef3fc32ba904d23ed258

5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?