これだけ知っておけばgitには困らない
- これだけ知っておけばgitには困らない
- 想定環境
- git設定編
- 運用編
- Fork
- git clone
- upstreamの設定、確認(git remote add upstream $URL)
- ローカルブランチの作成、originへの紐づけ(git checkout -b develop, git push -u origin develop)
- すでにoriginにブランチが存在する場合は、以下のように設定することも可能
- commit, push (git commit -m "comment", git push)
- upstraemの更新を取得(=originを最新化)(git fetch -p upstream, git merge upstream/develop)
- コンフリクトの解消 (解消後にmerge commit)
- pull request作成(gh pr create)
- よく使うコマンド集
- ブランチ削除(local: git branch -D develop , remote: git push -d origin develop)
- ローカルブランチをupstreamの最新で更新
- commitの取り消し
- ブランチ存在確認
- gitignoreが反映されない場合(キャッシュの削除)
- 特定のファイルの変更を取り消す
- 変更したファイルの一覧(--name-only)
- フォーマットしてログ出力(でもログの確認は基本的にトータスとか使う方が良いと思う)
- aliasの設定
- 設定ファイルの場所
- 強制push(非推奨)
- 変更の一時保存、取り出し(git stash save "comment", git stash list, git stash apply)
- なんか変なことになったらとりあえずこれでバックアップをとる
- helpの確認
- commit objectの指定方法いろいろ
- そのうち追記予定(そこまで使用頻度は高くないけどたまに使うものたち)
これだけ知っておけばgitには困らない
普通に開発する分ならここに記載したコマンド群(オプション含め)で大抵のことは事足りると思います。
随時更新予定 更新日 2022/11/23
想定環境
OS: windows git version 2.37.3.windows.1 remote: github github-flowを使用(forkしてPull Requestする運用方法) github-cliを使用(ただし別に使わなくても良い) コマンドプロンプト(cmd.exe)での使用
git設定編
gitの設定がlocal,global,systemの3つの階層があることは重要なので理解しておいでください。
※私は基本的にlocalとglobalしかいじりません。
user, mail設定
コマンドでも可能ですが、私は設定ファイルを直接いじることが多いです。
1. %userprofile%\.gitconfig
を開く(ファイルがなければ作成する 文字コード: utf8
)
2. 以下の通り追加
[user] name = user_username email = user_email@mail.com
自動改行を無効か
デフォルトだとpullとかpushとかしたときにgitが勝手に改行コードをOSに合わせて変更するようになっているので、それを無効化します。
(つまりgitが勝手に改行コードを変更したりしなくなります)
[core] autoCRLF = false
エディタの設定
デフォルトだとviになっているので、使い慣れたエディタに変更する。
※私はサクラエディタを使います。
※exeファイルへの絶対パスを指定するか、パスを通している場合は以下のように実行ファイル名のみの指定も可能。
[core] editor = sakura
ssh鍵の作成、登録
- 公開鍵、秘密鍵の作成
$ssh-keygen -t ecdsa C:\develop\WHAT_IS\what_is_help>ssh-keygen -t ecdsa Generating public/private ecdsa key pair. Enter file in which to save the key (C:\Users\user_name/.ssh/id_ecdsa): #何も入力せずEnter Enter passphrase (empty for no passphrase): #何も入力せずEnter Enter same passphrase again: #何も入力せずEnter Your identification has been saved in C:\Users\user_name/.ssh/id_ecdsa. #こっちが秘密鍵 Your public key has been saved in C:\Users\user_name/.ssh/id_ecdsa.pub. #こっちが公開鍵 The key fingerprint is: SHA256:gdskjgijsidjsjie545as4d5g4asg user_name@HOSTNAME The key's randomart image is: +---[ECDSA 256]---+ |o+ o+. | |+ + o =+. | +----[SHA256]-----+
- githubに公開鍵を登録
github.comにログインし、settings
⇒SSH and GPG keys
⇒new SSH Key
title
: 任意の名前を登録(例えばPCのホスト名とか)
key type
: Authentication Key
Key
: 先ほど作成したキー(C:\Users\user_name/.ssh/id_ecdsa.pub)をテキストエディタで開き、中身をコピーして貼り付け
[Add SSH Key]押下
これでgithubにSSH接続が可能です(要するにpushとかできるようになる)
TortoiseGitの場合
- TortoiseGit\bin\puttygen.exeを実行
- Generate⇒枠内でマウスを適当に動かすと鍵が作成される。
- [Save public key]押下⇒C:\Users\user_name/.ssh/tortoise_puttyに保存
- [Save public key]押下⇒C:\Users\user_name/.ssh/tortoise_putty.ppkに保存
- githubに公開鍵を登録 ※登録手順は上記の「githubに公開鍵を登録」と同様 ※C:\Users\user_name/.ssh/tortoise_puttyの中身をgithubに登録
- Pagentに登録
cd TortoiseGit\bin pageant.exe "C:\Users\user_name\.ssh\for_tortoise_git.ppk"
これでTortoiseGitを使用してgithubにSSH接続が可能です(要するにTortoiseGitでpushとかできるようになる)
運用編
Fork
こちらを使っていきます。
https://github.com/monkey999por/TestRep
github上で操作フォークしたいリポジトリでforkボタン押下⇒[Create fork]
これで自分のリポジトリにフォークが作成されます。
git clone
$git clone git@github.com:monkey999por-sub/TestRep.git $cd TestRep
upstreamの設定、確認(git remote add upstream $URL)
$git remote add upstream https://github.com/monkey999por/TestRep.git #正しく設定されているか確認 #origin: forkしたリポジトリ #upstream: fork元のリポジトリ $git remote -v origin git@github.com:monkey999por-sub/TestRep.git (fetch) origin git@github.com:monkey999por-sub/TestRep.git (push) upstream https://github.com/monkey999por/TestRep.git (fetch) upstream https://github.com/monkey999por/TestRep.git (push) #リモートブランチの確認 origin/mainとupstream/mainがあればOK $git fetch -p upstream $git branch --remote origin/HEAD -> origin/main origin/main upstream/main
ローカルブランチの作成、originへの紐づけ(git checkout -b develop, git push -u origin develop)
$git checkout -b develop Switched to a new branch 'develop' $git push -u origin develop Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'develop' on GitHub by visiting: remote: https://github.com/monkey999por-sub/TestRep/pull/new/develop remote: To github.com:monkey999por-sub/TestRep.git * [new branch] develop -> develop branch 'develop' set up to track 'origin/develop'. #ちゃんと紐づいているか確認 $git branch -vv * develop 4f11259 [origin/develop] comment
すでにoriginにブランチが存在する場合は、以下のように設定することも可能
$git branch -u origin/develop develop branch 'develop' set up to track 'origin/develop'.
commit, push (git commit -m "comment", git push)
$git add . $git commit -m "originで追加" $git push
upstraemの更新を取得(=originを最新化)(git fetch -p upstream, git merge upstream/develop)
#upstreamの更新情報を取得 $git fetch -p upstream #自分のローカルブランチにupstreamを取り込み $git merge upstream/main Auto-merging file.txt CONFLICT (content): Merge conflict in file.txt Automatic merge failed; fix conflicts and then commit the result.
あらら、コンフリクトしちゃいました。↓
コンフリクトの解消 (解消後にmerge commit)
#状況確認 $git status On branch develop Your branch is up to date with 'origin/develop'. You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: file.txt #これがコンフリクトしてる
コンフリクトしているファイルを開き、修正する。
<<<<<<< HEAD originで追加したもの ======= upstreamで追加されていたもの >>>>>>> upstream/main
<<<<<<< HEAD
と=======
の間: HEADから追加しようとした行
=======
と>>>>>>> upstream/main
の間: upstream/mainで追加されていた行
修正方法としては<<<...
と===...
と>>>...
を削除して、中身をいい感じに結合なり書き換えなりします。今回はどちらも残すようにしました。
修正後 originで追加したもの upstreamで追加されていたもの
コミットする
$git add file.txt $git commit -m "merge commit" $git push $git l #git logにalias設定してます。詳細は後述 * [2022-11-20 6 minutes ago] monkey999por-sub e94ec15 (HEAD -> develop, origin/develop)merge commit |\ | * [2022-11-20 27 minutes ago] monkey999por 61bc096 (upstream/main)upstreamで追加 * | [2022-11-20 25 minutes ago] monkey999por-sub 0974599 originで追加
pull request作成(gh pr create)
$gh pr create ? Which should be the base repository (used for e.g. querying issues) for this directory? monkey999por/TestRep # upstreamに設定したリポジトリを選択 Creating pull request for monkey999por-sub:develop into main in monkey999por/TestRep ? Title first pull request ? Body <Received> ? What's next? Submit https://github.com/monkey999por/TestRep/pull/1
ここまでで基本的にやることは終わり
よく使うコマンド集
ブランチ削除(local: git branch -D develop , remote: git push -d origin develop)
ローカルブランチの削除
$git branch -d branch_name
リモートブランチの削除
$git push -d origin branch_name
ローカルブランチをupstreamの最新で更新
$git fetch -p upstream $git reset --hard upstream/branch_name
commitの取り消し
$git commit -m "dummy commit" #間違えてコミットしちゃった #取り消し $git reset --mixed head~
ブランチ存在確認
$git branch -a | find /i "branch_name"
gitignoreが反映されない場合(キャッシュの削除)
こちらの記事が参考になります
https://qiita.com/fuwamaki/items/3ed021163e50beab7154
特定のファイルの変更を取り消す
$git checkout filename.txt #ワイルドカード指定も可能 $git checkout *.txt
変更したファイルの一覧(--name-only)
$git diff --name-only
フォーマットしてログ出力(でもログの確認は基本的にトータスとか使う方が良いと思う)
$git log --graph --all --format='[%as %ah] %x09%C(cyan bold)%an%Creset%x09%C(yellow)%h%Creset %C(green reverse)%d%Creset%s' --branches
aliasの設定
%userprofile%\.gitconfig
にこんな感じで追記。
[alias] a = add b = branch c = checkout ch = cherry-pick d = diff rmh = reset --mixed head rhh = reset --hard head s = status f = fetch l = log --graph --all --format='[%as %ah] %x09%C(cyan bold)%an%Creset%x09%C(yellow)%h%Creset %C(green reverse)%d%Creset%s' --branches
設定ファイルの場所
local: プロジェクトフォルダの中の.git/config
global: %userprofile%.gitconfig
system: インストールディレクトリのetc/gitconfig
それぞれ以下で開くこともできる
$git config --edit --local $git config --edit --global $git config --edit --system
強制push(非推奨)
※ブランチの関係性が壊れる可能性があるのでよほどのことがない限り使わないように。
$git push -f
変更の一時保存、取り出し(git stash save "comment", git stash list, git stash apply)
#ファイルを新規に作ったりしている場合は最初にステージングに乗せておく必要あり $git add . $git stash save "comment" #stashの確認 $git stash list stash@{0}: On main: comment #stashの取り出し $git stash apply stash@{0} #stashの削除 $git stash drop stash@{0} Dropped stash@{0} (88ecdc38b520e4f0b67a78bf18d1623606465964)
なんか変なことになったらとりあえずこれでバックアップをとる
例えばupstream取り込みmergeしてコンフリクト置きまくったときとか。なれないと焦ると思うが、いったん以下のことだけしておけばバックアップはとれる。
#いったんすべてステージングに追加(新規追加したファイルとかもこれで対応可能) $git add . #stashに保存 $git stash save "comment" #保存されていることを確認する $git stash list #upstreamの最新を取得 $git fetch -p upstream main $git reset --hard upstream/main #stashを適用 $git stash apply stash@{n} ※n #ここでコンフリクトの解消する #あとは普通にコミットしてPRの作成 $git add . $git commit -m "merge commit" $git push $gh pr create
helpの確認
$git comand -h #例 $git status -h usage: git status [<options>] [--] <pathspec>... -v, --verbose be verbose -s, --short show status concisely -b, --branch show branch information --show-stash show stash information --ahead-behind compute full ahead/behind values --porcelain[=<version>] machine-readable output --long show status in long format (default) -z, --null terminate entries with NUL -u, --untracked-files[=<mode>] show untracked files, optional modes: all, normal, no. (Default: all) --ignored[=<mode>] show ignored files, optional modes: traditional, matching, no. (Default: traditional) --ignore-submodules[=<when>] ignore changes to submodules, optional when: all, dirty, untracked. (Default: all) --column[=<style>] list untracked files in columns --no-renames do not detect renames -M, --find-renames[=<n>] detect renames, optionally set similarity index
commit objectの指定方法いろいろ
head~ #headに対して1つ前のコミット head~~ #headに対して2つ前のコミット head~~~ #headに対して3つ前のコミット stash@{0} #stashに保存したもの。使うことはないだろうけどここに対してcheckoutも可能 6ed25aa #logとかで確認可能なcommit hash ------- branch #ローカルブランチ origin/branch #originのブランチ upstream/branch #upstreamのブランチ tag #タグ