# Git 基本操作

Git は開発に不可欠的な存在であるが、覚えられません!
なので、メモって忘れてもいいように 🤡

# git user email 変更

# git に登録しているユーザーメールアドレス
git config user.email

# git ユーザーメールアドレス変更
git config --local user.email hapi@gmail.com

# Git ユーザ名とパスワードの入力を省略

  • ssh なしで Git のユーザー名パスワードを省略する方法
vi .git/config

ユーザー名入力を省略

...略...

[remote "origin"]
url = https://username@github.com/username/project.git

...略...

※@を URL エスケープして %40 と書く

パスワードの入力を省略

git config credential.helper store

git pull
git pull
Already up-to-date.

# Git ディレクトリ・ファイルの大文字識別するための設定

デフォルト設定が大文字小文字の変更無視するようになっているが、以下で変更

// 確認
git config core.ignorecase

// 設定
git config core.ignorecase false

# Git checkout

Switch branches or restore working tree files
Git checkout の2つの機能

  • ブランチ切り替え
  • 削除したファイルの戻し

Options

-q
--quiet
フィードバックメッセージを抑える

-f
--force
ブランチ切り替え作業でヘッダーとインデックスが違う場合に、強制ブランチ切り替え

When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes.
When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.

-m
--merge
ブランチ切り替えて差分もマージ

When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.

git checkout [-q] [-f] [-m] [<branch>]
git checkout [-q] [-f] [-m] --detach [<branch>]
git checkout [-q] [-f] [-m] [--detach] <commit>
git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>…​
git checkout [<tree-ish>] [--] <pathspec>…​
git checkout (-p|--patch) [<tree-ish>] [--] [<paths>…​]

-b
新しいブランチ作って、そして移動する

Create a new branch named <new_branch> and start it at <start_point>

-B
新しいブランチ作って、そして移動する(基本-b と一緒ですが、新しく作られるブランチのスタートポイントすでに存在するかどうか判断)

if it already exists, then reset it to <start_point>. This is equivalent to running "git branch" with "-f";

git checkout -b|-B <new_branch> [start point] 以下と同等

git branch -f <branch> [<start point>]
git checkout <branch>

# 特定のファイルを戻す

git checkout [commit_id] <file_path>

commit_id の指定がない場合、index の head になる

# git remote

Git リモート URL 変更 git remote set-url

現在のリモート確認

git remote -v

# 結果
origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
origin  git@github.com:USERNAME/REPOSITORY.git (push)

リモート変更

ssh から https の URL(https://github.com/USERNAME/REPOSITORY.git)に変更

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

変更後結果確認

git remote -v

# 結果
origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
origin  https://github.com/USERNAME/REPOSITORY.git (push)

# Github 用に Windows ssh key 作った時のメモ

github につなぐための windows ssh key 作成

ssh-keygen

ssh-keygen -t rsa

Enter 複数

Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\test/.ssh/id_rsa):
Created directory 'C:\Users\test/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\test/.ssh/id_rsa.
Your public key has been saved in C:\Users\test/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:djAebkNoqR21ClPaKTJsBW5ZcpMyWu+nUcwMmYdbH5M test@DESKTOP-0O68LCO
The key's randomart image is:
+---[RSA 2048]----+
|  . . += .       |
|   o+=*+o E      |
|   o=+*O*. o     |
| ..o BoB==.      |
|  = *.+.S .      |
| . o +o+.o       |
|      .+         |
|      .          |
|                 |
+----[SHA256]-----+

Config(option)

IdentityFile 設定必要があれば変更

.ssh/config

Host github github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa
  User git

# git pull で強制的ローカル環境上書き

ローカル環境でいろいろ編集やらコミットやらあったけど、結局いらないよってことあるけど、その時にリモートサーバーの git を pull してローカル環境上書きしてしまえ~
の時に使う

# リモートの最新を取ってくる
$ git fetch origin master

# ローカルmasterをリモートmasterに強制的にリセット
$ git reset --hard origin/master
2019-06-01
  • git