1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

僕なりの Git 備忘録

Last updated at Posted at 2024-06-06

fetch

リモートのコピーをローカルにダウンロードするコマンド、ローカルのリモート追跡ブランチにコピーしてくるだけでワーキングツリーには影響しない

  1. リモートのコピーちょうだいー
  2. ローカルとの差分を確認し、リモートリポジトリが変更や新規ブランチをリモート追跡ブランチにあげる
  3. リモート追跡ブランチが更新される

merge

$ git checkout aaa
$ git merge bbb

ローカルでは今いるブランチにほかのブランチを合体させる

$ git fetch
$ git merge origin/aaa

リモートリポジトリのブランチをマージする場合、先にリモートの状態をリモート追跡ブランチにコピーしてからマージする

rebase

$ git checkout master
$ git rebase aaa

マージと同じ、今いるブランチに他のブランチの変更を取り込むものだがちょっと違う。マージは合体で一つになった!という感じだが、rebaseは上にコミットを積むというイメージ。だから、今いるブランチの未来ができているという感じで、master headとaaa headは異なる。

pull

$ git pull
=
$ git fetch
$ git merge

一回でまとめてやってくれるから便利よね

$ git pull --rebase
=
$ git fetch
$ git rebase

まとめてやるののrebaseバージョン

--rebaseオプション付ける

メリット:マージコミットが残らないから履歴がきれいになる

reset

git reset --hard <ブランチ名> ... commitもaddも吹き飛ばす

git reset --soft <ブランチ名> ... commit吹き飛ばしてadd後(ステージングにある状態)まで戻す

(HEAD, master) ... HEADが名のないブランチを指しており、それとmasterっていう2つのブランチがあるよ→下の状況にするには git checkout master を叩く

(HEAD → master) ... HEADがmasterブランチを指している=HEADとmasterが同じ

全ファイル取り消し

$ git reset HEAD

特定ファイル取り消し

$ git reset HEAD file_name

branch

自分なりの命名方法

  • f/xxxxx 機能追加/変更
  • ref/xxxxx リファクタリング
  • fix/xxxxx バグ修正
  • hotfix/xxxxx masterから派生させる緊急性の高い修正

stash

メッセージを付ける

$ git stash save "message"

特定のファイルのみstashする

$ git stash push -m "メッセージ" ファイルパス

merge commit in local

リモートリポジトリがない時に履歴を残したい時に使う

$ git merge --no-ff <targetBranch>

commit

$ git commit -m "message"
$ git commit -m "message" -m "detail message"

empty commit

https://qiita.com/miriwo/items/dbf82ca73723026d96f2

$ git commit --allow-empty -m ""

modify commit message

1つ前

$ git commit --amend -m ""

2つ以上前

$ git rebase -i HEAD~3
# 変更したいcommitをe(edit)に
$ git commit --amend -m ""
$ git rebase --continue

最初のコミットメッセージが対処の場合

$ git ch <最初のコミットID>
$ git commit --amend
$ git rebase --onto HEAD <新しくできた最初のコミットID> master

checkout

作業ツリーの特定のファイルを戻す

$ git checkout HEAD <fileName>

作業ツリー全体を戻す

$ git checkout HEAD .

rm

$ git rm <dirName>
$ git rm -r <dirName>
$ git rm -r --cached <dirName>

diff

$ git diff <branchName> --name-only

remote

https://git-scm.com/book/ja/v2/Git-%E3%81%AE%E5%9F%BA%E6%9C%AC-%E3%83%AA%E3%83%A2%E3%83%BC%E3%83%88%E3%81%A7%E3%81%AE%E4%BD%9C%E6%A5%AD

$ git remote -v
$ git remote add origin <remoteUrl>
$ git remote set-url origin <newRemoteUrl>
$ git remote <shortname> <url>

SSH connect

# 鍵作成
$ ssh-keygen -t rsa -f github_rsa

# config作成
$ vim config
>>
Host github
  HostName  github.com
  User  git
  Port  22
  IdentityFile ~/.ssh/github_rsa

ServerAliveInterval 15
<<

# GitHubやGitLabに公開鍵を設置

# 接続確認
$ ssh -T github
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?