ホーム » git

git」カテゴリーアーカイブ

gitコマンドで変更内容確認

git diff

比較対象

作業ディレクトリのファイルとローカルリポジトリ

表示

変更内容が全て表示されるので決して見やすくない

 

git diff –name-only

比較対象

作業ディレクトリのファイルとローカルリポジトリ

表示

更新されたファイル名の一覧表示

 

git diff ファイルパス

比較対象

作業ディレクトリのファイルとローカルリポジトリ

表示

ファイルの比較

 

git tag を追加

タグを追加

[code]
git tag -a タグ名 -m "コメント"
[/code]

タグをリモートリポジトリに反映

[code]
git push origin –tag
[/code]

タグの一覧

[code]
git tag
[/code]

git commit時のメッセージをVisualCodeで入力

予めVisualCodeをインストール

C:\Users\ユーザー名\.gitconfigを開いて以下を追加

[code]
[core]
quotepath = off
editor = "code –wait"
[/code]

git 取り消し

直近のcommitを取り消し

[code]
git reset –soft HEAD^
[/code]

※softをhardにすると作業ディレクトリの内容もロールバックされる

指定したコミットまでに戻す

[code]
git reset –soft ハッシュ値
[/code]

ハッシュ値はgit logで確認

resetをリモートリポジトリに反映

[code]
git push -f origin master
[/code]

作業ディレクトリの内容をローカルリポジトリに戻す

[code]
git checkout .
[/code]

git commit 関連

commitの一覧表示

[code]
git log
[/code]

直近のcommitの内容表示

[code]
git log -1
[/code]

直近のcommitの対象ファイル一覧

[code]
git log -1 –name-only
[/code]

直近のcommitの取り消し 編集したファイルは変更しない

[code]
git reset –soft HEAD~
[/code]

[code]
HEAD~とHEAD^は同じ
[/code]

直近のcommitの取り消し 編集したファイルも元に戻す

[code]
git reset –hard HEAD~
[/code]

githubにリポジトリを作成、ローカルリポジトリからPUSHまで

既にgithubの使い方は説明しているがSSHを使用しない方法を記載

PC上にはgit for windowsがインストール済であることを前提とする

github上でリポジトリを作成

PC上にローカルリポジトリのフォルダ「myrepo」を作成

カレントを「myrepo」に移動しローカルリポジトリを初期化

[code]
C:\projects\test\git>mkdir myrepo
C:\projects\test\git>cd myrepo
C:\projects\test\git\myrepo>git init
Initialized empty Git repository in C:/projects/test/git/myrepo/.git/
C:\projects\test\git\myrepo>
[/code]

ファイルを作成して「add」、「commit」

[code]
C:\projects\test\git\myrepo>echo hello > readme.txt
C:\projects\test\git\myrepo>git add .
C:\projects\test\git\myrepo>git commit -m "comment"
[/code]

github上からリポジトリのURLをコピー

リモートリポジトリを設定し、push
※githubのアカウントを聞かれたらメールアドレスとパスワードを入力

[code]
C:\projects\test\git\myrepo>git remote add origin https://github.com/省略/myrep.git
C:\projects\test\git\myrepo>git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 216 bytes | 216.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/省略/myrep.git
* [new branch] master -> master

C:\projects\test\git\myrepo>
[/code]

git add を取り消し

[code]
git reset
[/code]

GIt ローカルリポジトリとリモートリポジトリの同期をとる

ローカルリポジトリとリモートリポジトリの同期をとる(=VSSでは最新バージョンの取得)
はローカルリポジトリ内で以下のコアンドを実行
[code]
git pull origin master
[/code]

指定したファイル、ディレクトリをGitの対象外とする

VsiualStudioで開発をし、ビルドを行うと多くのバイナリファイル、フォルダが作成される。

これらをGitの対象外とするにはリポジトリの直下に「.gitignore」ファイルを作成して、対象外とするファイルとディレクトリを書き込み。

[code]
bin/
obj/
[/code]

GitHubのリポジトリをローカルにClone

前回申し込んだGitHubのリポジトリをローカルにクローンする。

基本的にはgit for windowsをインストールしてgitlabのプロジェクトを操作に記載した内容でOK。
今回は既にGitLabのリポジトリへの接続環境が作成されているPC上にGitHubのリポジトリへの接続環境を構築する。
GITHUBもGITLABと同様にSSHキーを必要とするため以下に従って作成。(保存するファイルはデフォルトではなくgithub_kernel_rsaに変更)

[code]
C:\Git\usr\bin>ssh-keygen -t rsa -C ‘メールアドレス’
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/tarumi/.ssh/id_rsa): /c/Users/tarumi/.ssh/github_kernel_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/tarumi/.ssh/github_kernel_rsa.
Your public key has been saved in /c/Users/tarumi/.ssh/github_kernel_rsa.pub.
The key fingerprint is:
省略
C:\Git\usr\bin>
[/code]

なぜかファイル名が文字化けしていたので修正

GitHubのサイトにSSHキーを登録

GItHubのサイトの「your profile」を表示

「Edit Profile」ボタンを押す

「SSH and GPG keys」を選択し「New SSH Key」ボタンを押す

github_kernel_rsa.pubの内容を貼り付ける

C:\Git\etc\ssh\ssh_configを以下に従って編集
※この設定は複数のGITリポジトリ(GITLABとGITHUB)とアクセスするために設定する

[code]
Host gitlab
User git
Port 22
HostName gitlab.com
TCPKeepAlive yes
identitiesonly yes
identityFile /C/Users/tarumi/.ssh/id_rsa

Host github
User git
Port 22
HostName github.com
TCPKeepAlive yes
identitiesonly yes
identityFile /C/Users/tarumi/.ssh/github_kernel_rsa
[/code]

github上のリポジトリをローカルにクローン

今回はC:\githubhomeディレクトリ以下にローカルリポジトリを作成していく。

通常(ssh_configの設定を使用しない)はgithubのリポジトリサイトを表示し「clone or download」ボタンを押しsshのURLを表示し

以下のコマンドでgithub上のリポジトリをローカルにクローンする。

[code]
git clone git@github.com:組織名/test.git
[/code]

今回(ssh_configの設定を使用)は以下のコマンドでクローンする

[code]
git clone git@github:組織名/test.git
[/code]

上記で指定したgithubはssh_configのHostに指定した値

[code]
C:\githubhome>git clone git@github:組織名/test.git
Cloning into ‘test’…
Enter passphrase for key ‘/C/Users/tarumi/.ssh/github_kernel_rsa’:
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[/code]

リポジトリのフォルダに移動して以下のコマンドでgitへの接続ユーザーの設定を行う

[code]
C:\>cd githubhome
C:\githubhome>cd test
C:\githubhome\test>git config –local user.email ‘メールアドレス’
C:\githubhome\test>git config –local user.name ‘ユーザー名’
C:\githubhome\test>git config –local core.quotepath off
[/code]
引数に–localを付けたときはローカルリポジトリのフォルダ内のみで有効な設定として保存される。

設定した内容はローカルリポジトリ内の.gitフォルダに保存される