Gitまとめ

いつも呪文のように唱えていたgitのコマンドだけど、ようやく学ぶことができたのでメモ。

言葉の定義

  • レポジトリ
    Gitがファイルの履歴を保存している場所
  • インデックス
    リポジトリに保存されている情報とワークツリー(作業している場所)との差(変更箇所)を記録する場所
  • ワークツリー
    リポジトリの内容をファイルとして展開する場所

コマンド一覧

コマンド 内容
git init レポジトリを作成する
git add コミット対象として登録する
-u:変更したファイル全てをインデックに登録
-A:全てのファイルをインデックスに登録
git commit インデックスの履歴をレポジトリに登録
git commit --amend 直前のコミット漏れしたファイルを後で追加
git commit --amend -m 直前コミットのコメント内容を``に変更
git revert ``というコミットの取り消し
git rebase -i HEAD~~ 過去のコミットをまとめたり、修正したりする。
左記はHEADから2つ前まで。コミットをまとめるときは、
エディタが開いたら2つ目のpicksquashに変更。
コミットを修正するときはpickeditに変更する
詳細
git rebase ``ブランチに現在いるブランチで行った全コミットを適応
git rebase baseブランチに`ブランチで行った全コミットを適応<br>(git checkout && git rebase `と同じ意味)
git rm --cached -r . git init直後の、git addを全て取り消し
git rm --cached -r git init直後の、git addfnameというファイルのみ取り消し
git reset --mixed HEAD 2回目以降にてgit addした内容を取り消し
git reset --mixed HEAD 2回目以降にてgit addしたfnameというファイルのみ取り消し
git stash 変更内容の退避。インデックス以下の変更全てが退避される。
git stash save "コメント" コメントを付けて変更内容を退避。
git stash apply 退避した内容をステージング前の状態として取り出す。
取り出してもstashリストから削除しない
--indexをつけるとステージング後の状態として取り出す
git stash pop 基本applyと同じだが、取り出したらstashリストから削除する
git stash clear stashリストを全て削除
git stash show 退避した内容の詳細を見る
git status ワークツリーの状態を表示する
git diff ワークツリーとインデックスの差分を表示する
git diff --cached インデックスとレポジトリの差分を表示する
git diff HEAD ワークツリーとレポジトリ差分を比較する
git log 履歴を表示する
git log --graph ブランチやマージの歴史を、ログ出力とともにアスキーグラフで表示する
git branch ブランチの一覧を表示する
-rをつけるとリモート追跡ブランチを表示
git branch `という新しいブランチを作る<br>-d`をつけるとブランチを削除する
git branch -m ブランチ名変更(へ)
git checkout ``ブランチに切り替える
git checkout -b ``ブランチを新しく作って、ブランチを切り替える
git checkout -b / ブランチが、リポジトリの``ブランチを追跡するように設定
git merge ``ブランチを現在のブランチにマージ。
git push
:
変更履歴を送る
git push --delete リポジトリのブランチを削除
git clone レポジトリを複製する
git remote add リモートレポジトリに別名をつける
git remote -v レポジトリの別名を一覧表示する
git remote rename リモートレポジトリの別名を変更したい
git fetch 他レポジトリの変更履歴を取込む
変更反映はmerge
git pull 別名の``というブランチを
現在のブランチに取り込む
git tag タグ一覧を表示する
-nをつけてコメント表示
git tag 現在のHEADが差しているコミットに``という軽量タグをつける
git tag -a 注釈付きタグをつける。-amでタグ名とコメント追加を一緒に行う
git tag -a 昔のコミットにタグを付ける。
check sumgit log --pretty=onelineで確認できる
git tag -d タグの削除
git push に``というタグ名でpush

Git ローカルレポジトリへのcommit、リモートレポジトリへのpush

上に書いたコマンド一覧だけだと、後で見たときに『結局どういうふうに使うんだっけ?😅』となりそうなので、大まかな流れと実際に試した例を載せておく。

  1. git initで空レポジトリを作成
  2. git initしたディレクトリで、必要なファイル作成やコピーを行う
  3. git addで2.で更新したファイル群をワークツリーからインデックスへ追加
  4. git commitでインデックスの内容をローカルレポジトリへ追加
  5. git remoteでリモートレポジトリに別名をつけてgit push時の入力手間軽減
  6. git pushでローカルレポジトリの内容をリモートレポジトリへ反映
$ git init
Initialized empty Git repository in /home/goruchan/git/hoge/.git/
$ git branch -m main   &lt;--デフォルトブランチ名をmainに設定
$ git branch           &lt;--レポジトリが空なのでbranchは出てこない
$ ls -la
total 12
drwxr-xr-x 3 goruchan goruchan 4096 Dec 27 22:11 .
drwxr-xr-x 3 goruchan goruchan 4096 Dec 27 22:10 ..
drwxr-xr-x 7 goruchan goruchan 4096 Dec 27 22:13 .git&lt;--レポジトリ
$ touch readme.md
$ git add readme.md     &lt;---リポジトリに履歴にとして追加
$ git status
On branch main

No commits yet

$ git branch
$ git commit -m &quot;add readme.md&quot;
[main (root-commit) 0e4504d] add readme.md
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 readme.md
$ git branch
* main
$ git branch develop   &lt;--developブランチ作成
$ git checkout develop  &lt;--developブランチへ切り替え
Switched to branch &#039;develop&#039;
$ touch hoge.md
$ git add -A
$ git commit -m &#039;add hoge.md&#039;
[develop 4260a4d] add hoge.md
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hoge.md
$ git checkout main    &lt;--mainブランチへ切り替え
Switched to branch &#039;main&#039;
$ git branch
  develop
* main
$ git merge develop    &lt;--developブランチをmainブランチへ取り込む
Updating 4a14ed8..4260a4d
Fast-forward
 hoge.md | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hoge.md
$ ls
hoge.md  readme.md
$ git branch -d develop &lt;--developブランチの削除
Deleted branch develop (was 4260a4d).
$ git remote add origin git@github.com:goruchanchan/gitPractice.git  &lt;--originをリポジトリパスとして設定(この場合、Github上のレポジトリ)
$ git push -u origin main   main
Branch 'main' set up to track remote branch 'main' from 'origin'.

あと、大雑把なワークフローのイメージをまとめてみた。全体的な流れを大雑把に理解して、あとは今後のプラクティスで定着させていこう😀 gitwork.png

fetchについてもう少し細かく書くと、リモートレポジトリからのfetchでやっているのは、リモートレポジトリの最新状況をローカル内に引っ張ってくるところまでで、現在作業しているレポジトリに対してのマージは実施しない。参考

fetch.png

また、コミットの取り消しについては、こちらの内容が参考になった。 reset.png

競合時の解決方法

  1. git fetch
  2. git merge
  3. 競合の修正
  4. 後はいつもの手順(addcommitpush)
$ git push origin tutorial3:tutorial3
error: failed to push some refs to 'github.com:goruchanchan/gitPractice.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git fetch 
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
Unpacking objects: 100% (2/2), 299 bytes | 299.00 KiB/s, done.
From github.com:goruchanchan/gitPractice
   f59c3b9..6ba99fe  tutorial3  -&gt; origin/tutorial3
$ git merge origin/tutorial3 
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
$ vim test.txt 
$ git add .
$ git commit
[tutorial3 c65231e] Merge remote-tracking branch 'origin/tutorial3' into tutorial3
$ git push 
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 629 bytes | 629.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:goruchanchan/gitPractice.git
   6ba99fe..c65231e  tutorial3 -&gt; tutorial3

競合時のイベントシーケンスはここの内容がとてもわかりやすいので、記載しておく。 sequence.png

同じところにチームで機能開発している場合のワークフローもあったので、参考として載せておく。workflow.png

Git用のエリアスの設定

大きく2つのやり方でGit用のエイリアスを設定できる。

  • コマンドで追加

    $ git config --global alias.br branch
    $ git config --global alias.ci commit
    
  • .gitconfigファイルに直接記入

コマンド追加を実行したときには、結局.gitconfigに追加してくれているだけっぽいので、直接そのファイルに書き込んでしまっても設定できる。[alias]部分にエイリアスを設定してあげれば良い。ちなみにbrstは上のコマンドで追加したままのもの。

  # This is Git's per-user configuration file.
  [user]
  # Please adapt and uncomment the following lines:
    name = goruchan 
    email = xxx@xxx.xxx.xxx
  [color]
    ui = auto
  [core]
    editor = vim
  [alias]
    br = branch
    st = status
    last = log -1 HEAD