LoginSignup
0
1

More than 1 year has passed since last update.

Git覚え書き

Last updated at Posted at 2021-03-03

Gitチュートリアル

準備

apt-get update
apt-get install -y git
apt-get install -y vim

設定

git config --global user.name "Shoichiro Kanno"       # 名前(必須)
git config --global user.email "s-kanno@example.com"  # メールアドレス(必須)
git config --global color.ui true                     # カラー表示(任意)
git config --global core.editor vim                   # デフォルトで使用するエディタをVIMに設定
git config -l                                         # 設定内容確認
git config --global --unset {key}                     # 設定項目の削除
git config --global credential.helper 'store --file ~/.git_credentials' # 認証情報を記憶

# 2021年8月13日以降、GitHubのリポジトリにアクセスするには、ユーザ名とパスワードではなくアクセストークンが必要になった。
git clone https://{アクセストークン}@github.com/{ユーザ名}/{リポジトリ名}.git

基本的な使い方

git init             # 初期化
git add hogehoge.txt # 特定のファイル・変更を追加
git add .            # すべてのファイル・変更を追加
git rm hogehoge.txt  # ファイル削除
git mv hogehoge.txt foofoo.txt # ファイル移動
git commit           # コミット
git commit --amend   # 直前のコミットの変更を変更する
git log              # 履歴表示
git log --oneline    # 履歴表示(1行表示)
git log -p           # 履歴表示(変更箇所付き)
git log --stat       # 変更履歴(変更ファイル名付き)
git status           # 状態を表示
git checkout -- hogehoge.txt # commitされている状態に戻す
git diff             # 変更箇所を表示
git diff --cached    # ステージングエリア(コミット前)の変更箇所

vi .gitignore        # 無視するファイルを設定

git reset --hard HEAD       # コミットされている状態に戻す(ファイルの変更、追加も打ち消す)
git reset --hard HEAD^      # HEADから1つ前のコミットに戻す(すべて打ち消す)
git reset --soft HEAD^      # HEADから1つ前のコミットに戻す(ワーキングツリー・インデックスはそのまま)
git reset --mixed HEAD^     # HEADから1つ前のコミットに戻す(ワーキングツリーはそのまま)
git reset --hard {id}       # 特定のコミットに戻る
git reset --hard ORIG_HEAD  # 持っどったのを戻る

git reflog                  # HEADが辿ってきた履歴

git branch                    # ブランチの一覧
git branch {branch_name}      # ブランチの作成
git branch -d {branch_name}   # ブランチの削除
git checkout {branch_name}    # ブランチの切り替え
git checkout -b {branch_name} # ブランチを作成、同時に切り替え
git merge {branch_name}       # ブランチのマージ
git merge --squash {branch_name} # マージするブランチのコミットを1コミットにまとめる ※別途コミットすること

git tag {tag_name}      # 直近のコミットにタグを付ける
git tag {tag_name} {id} # 特定のコミットにタグを付ける
git tag -d {tag_name}   # タグの削除
git show {tag_name}     # タグのコミットを表示

git clone {url} {dir_name} # クローン
git push                   # プッシュ
git pull                   # プル
git push --set-upstream origin {branch_name} # ブランチを作って初回のプッシュ

git stash -u                      # 変更内容を退避(-uを付けるとaddをしていないファイルも退避する)
git stash -u save "stash message" # メッセージを付けて変更内容を退避
git stash list                    # stashの一覧を表示
git stash apply stash@{0} --index # stashした状態に戻す(--indexを付けないとaddを行った情報がなくなる)
git stash drop stash@{0}          # stashを削除

# branch1へbranch2を取り込む。その歳、branch1の最新のコミットからbranch2のコミットを追加する。
git rebase {branch1_name} {branch2_name}

git cherry-pick {id} # 特定のコミットを取り込む

git revert [id] # 特定のコミットを消す

小ネタ

# git logで文字化けする場合の対応
vim ~/.bashrc
export LESSCHARSET=utf-8
source ~/.bashrc
0
1
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
1