LoginSignup
0
0

git 備忘録

Last updated at Posted at 2024-05-09

はじめに

普段はバリバリコードを書くお仕事ではないけれど、たまーに集中してコーディングする機会もあります。
そんな時Githubやgitも使う機会がありますが、あれどうやったっけとかなんだっけと忘れており毎度困る。
この記事はこれまでのメモ書きをQiitaにアップし、今後も困ったら追記/更新する予定のunstableな記事です。

操作の対象

add

ローカルディレクトリ=>ステージングエリア

commit

ステージングエリア=>ローカルリポジトリ

push

ローカルリポジトリ=>リモートリポジトリ

fetch

リモートリポジトリ=>ローカルリポジトリ

merge

2つリポジトリの結合

実践

ローカルユーザーでリモートリポジトリ"practice"は作成済のものとする(パブリック権限)

リモートリポジトリからローカルにリポジトリをクローン
git clone https://github.com/USER/practice

configに関して

configには3種の設定ファイルがある

全てのconfigの項目表示
git config -l
システム全体(全ユーザーの全リポジトリ)

/etc/gitconfig
自分の環境中身なかった。消した?

git config --system  -l
使用中のユーザーの全リポジトリ
git config  --global -l
~/.gitconfig
user.name=USER
user.email=USER@ORG
リポジトリ別
git config --local -l
~/work/practice/.git/config

core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/USER/practice.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/main

ローカルディレクトリとローカルリポジトリのファイル操作

ローカルディレクトリでファイル作成

新規ファイル作成

touch git_practice
ローカルリポジトリに対象を指定してステージング追加
git add git_practice

ディレクトリ内すべて追加

git add .
ステージングエリアの更新内容をローカルリポジトリに登録
git commit -m "任意のcomment"
ローカルディレクトリとステージングエリアからファイル削除
git rm gomi
ステージングエリアをローカルリポジトリの状態に戻す
git reset ( --hard )HEAD gomi
ローカルディレクトリをステージングエリアの状態に戻す
git checkout -- gomi
ワークツリーの状態を見る

Gitの管理下に置かれた実際に作業をしているディレクトリのことをワークツリーと呼ぶ。

git status

作業ディレクトリの状態とステージング エリアの状態を表示するコマンド

コミットログを見る
git logs

ブランチ:メインとは別の流れで作業を続ける機能

main(master)ブランチ:デフォルトブランチ、commitで更新されていく

HEADブランチ:現在作業中のブランチ(結果としてmasterと同じ場所となることもある)、commitで更新されていく

ブランチの確認

すべて?のブランチ

git branch -a 
リモートのブランチ一覧
git branch -r
ブランチの作成

developという名のブランチを作成

git branch develop
作業中のブランチを切り替え
git checkout develop
git switch devekop
main ブランチでファイル作成
#ブランチ切り替え
git checkout main 
#新規ファイル作成
echo "hoge" > main.file 
#ステージング
git add .
#ローカルリポジトリに追加
git commit -m "add main.file"
develop ブランチでファイル作成
git checkout develop
echo "fuga" > develop.file 
git add .
git commit -m "add develop.file"
mainブランチにdevelopブランチをマージ
git checkout main
git merge develop

参考資料

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