gitスタートガイド

2020-05-11 2020-05-12

gitの使い始めに必要な全てを伝授します.

インストール

nix-shell -p git

git guigit send-emailが欲しいならgitFullをインストールする.

nixpkgs/pkgs/applications/version-management/git-and-tools/default.nix

  # The full-featured Git.
  gitFull = gitBase.override {
    svnSupport = true;
    guiSupport = true;
    sendEmailSupport = !stdenv.isDarwin;
    withLibsecret = !stdenv.isDarwin;
  };

使い方

https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup

gitconfig

$ cat ~/.gitconfig
[user]
	name = gtgteq
	email = gtgteq.operator@gmail.com
[color]
	ui = false
[core]
	autocrlf = input
	safecrlf = true
[push]
	default = upstream
[pull]
	rebase = true
[branch]
	autosetuprebase = always
[alias]
	hist = log --pretty=format:\"%h %ad %s%d [%an]\" --graph --date=short
	precommit = diff --cached --diff-algorithm=minimal -w

[color] ui = falseについて

ターミナルが256色に対応しない場合の設定.

CRLFについて

gitはテキストファイルについて,改行コードをLFとCRLFのどちらかと仮定している. autocrlfは特にWindowsでLFに揃えたファイルのみコミットするように設定する. Linuxだとinputに設定すると良い.

https://help.github.com/ja/github/using-git/configuring-git-to-handle-line-endings

safecrlfはLFとCRLFが混在する場合にコミット不可とする設定.

https://git-scm.com/docs/git-config#Documentation/git-config.txt-coresafecrlf

rebaseとmergeのどちらにすべきか

rebaseにしておきましょう.mergeはプロ向けの機能です.でもデフォルトはmergeなんですよね. グローバルなgitconfig(~/.gitconfig)で[pull] rebase = trueを指定する理由です.

pull時にmergeのままだとマージコミットが作成されます.つまり履歴がダイヤモンド型になります. rebaseはpullしたコミットにローカルのコミットを上乗せします.

https://git-scm.com/book/en/v2/Git-Branching-Rebasing

日常的に使うコマンド

gitconfigに2文字くらいのaliasを作っても良いし,シェルのエイリアスとして設定するのも良い.

初回設定.(URL)で指定した場所にバックアップされる. この場所を他人が書き換えられるようにすると,作業を分担することができる.

$ git init
$ git add (file)
$ git commit -m "Initial commit"
$ git remote set-url origin (URL)
$ git push -u origin master

2回目以降.上記の設定が完了しているならば, pullの時点でローカルにコミットしていたとしてもmergeの代わりにrebaseされるはず.

$ git pull
$ git add (file)
$ git commit -m "commit message"
$ git push

既存のリポジトリを自分の手元に持って来る.

$ git clone (URL)

やや上級者向けのコマンド

知ってるとお得.

$ git add -p (file)
$ git commit --amend
$ git rebase -i HEAD\^\^\^
$ git clone --depth=1
$ git checkout -b (new branch name)
$ git init --bare
$ git bisect