20190324のGitに関する記事は9件です。

【ツール】Gitで使う初歩的な用語の関係をまとめる

参考リンク

全体像

  • origin
  • clone
  • origin/master
  • branchcheckoutcheckout -b
  • push origin/master
  • merge master
  • fetch master
  • merge origin/master
  • pull origin/master 表.png

origin

リモートリポジトリのアドレスに対してにつけられている名称
自分の場合、origingit@github.com:Hiroki-IT/Symfony2_Nyumon.gitであった

$ git remote -v
origin  git@github.com:Hiroki-IT/Symfony2_Nyumon.git (fetch)
origin  git@github.com:Hiroki-IT/Symfony2_Nyumon.git (push)

clone

リモートリポジトリの状態をそのままローカルリポジトリにコピー

git clone git@github.com:Hiroki-IT/Symfony2_Nyumon.git #リモートリポジトリのアドレスを指定

origin/master

リモート追跡ブランチ(リモートリポジトリのmasterブランチの状態がコピーされたブランチ)に対してデフォルトでつけられている通称(※fetch masterの項目も参照)
追跡されているリモートリポジトリのmasterブランチは$ git branch -vvを用いて確認可能で、[origin/master]と表示されたブランチが追跡されているmasterブランチを表す

$ git branch -vv
  2019/Symfony2_Nyumon/feature/2 f17f68e Merge remote-tracking branch 'refs/remotes/origin/master'
* 2019/Symfony2_Nyumon/master    f17f68e Merge remote-tracking branch 'refs/remotes/origin/master'
  master                         f17f68e [origin/master] Merge remote-tracking branch 'refs/remotes/origin/master'

branchcheckoutcheckout -b

$ git branch [ブランチ名] #指定のブランチを作成

$ git checkout [ブランチ名] #指定のブランチに移動

$ git checkout -b [ブランチ名] #指定のブランチを作成&指定のブランチに移動

push origin/master

ローカルリポジトリにおけるmasterブランチの状態を、リモートリポジトリのmasterブランチに反映
引数のorigin/masterは省略可能

$ git push origin/master

$ git push

image.png

conflict
ローカルリポジトリにおけるmasterブランチからpushする前に、リモートリポジトリにおけるmasterブランチに新しい履歴が追加されているため、ブランチ間で整合性がとれなくなっている状態

$ git push origin/master
To github.com:Hiroki-IT/Symfony2_Nyumon.git
 ! [rejected]        origin/master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:Hiroki-IT/Symfony2_Nyumon.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

image.png

merge master

ローカルリポジトリにおけるトピックブランチの変更を、同リポジトリにおけるmasterブランチに反映

$ git checkout [ブランチ名] #トピックブランチに移動

$ git merge master 

image.png

fetch master

リモートリポジトリにおけるmasterブランチの変更を、ローカルリポジトリのorigin/masterブランチに反映

$ git fetch master

image.png

merge origin/master

ローカルリポジトリにおけるorigin/masterブランチの変更を、同リポジトリにおけるmasterブランチに反映

$ git fetch 

$ git merge origin/master 

image.png

pull origin/master

リモートリポジトリにおけるmasterブランチの変更を、ローカルリポジトリのorigin/masterブランチに反映(fetch)させ、その後masterブランチに反映(merge

$ git pull origin/master

image.png

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【ツール】最近、Gitで使った用語の関係をまとめる

参考リンク

全体像

今回まとめた用語
origin
clone
origin/master
branchcheckoutcheckout -b
push origin/master
merge master
fetch mastermerge origin/master
pull origin/master
関係図.png

origin

リモートリポジトリのアドレスに対してにつけられている別称
自分の場合、origingit@github.com:Hiroki-IT/Symfony2_Nyumon.gitであった

$ git remote -v
origin  git@github.com:Hiroki-IT/Symfony2_Nyumon.git (fetch)
origin  git@github.com:Hiroki-IT/Symfony2_Nyumon.git (push)

clone

リモートリポジトリの状態をそのままローカルリポジトリにコピー

git clone git@github.com:Hiroki-IT/Symfony2_Nyumon.git #リモートリポジトリのアドレスを指定

origin/master

リモート追跡ブランチ(リモートリポジトリのmasterブランチの状態がコピーされたブランチ)に対してデフォルトでつけられている別称(※fetch masterの項目も参照)
追跡されているリモートリポジトリのmasterブランチは$ git branch -vvを用いて確認可能で、[origin/master]と表示されたブランチが追跡されているmasterブランチを表す

$ git branch -vv
  2019/Symfony2_Nyumon/feature/2 f17f68e Merge remote-tracking branch 'refs/remotes/origin/master'
* 2019/Symfony2_Nyumon/master    f17f68e Merge remote-tracking branch 'refs/remotes/origin/master'
  master                         f17f68e [origin/master] Merge remote-tracking branch 'refs/remotes/origin/master'

branchcheckoutcheckout -b

$ git branch [ブランチ名] #指定のブランチを作成

$ git checkout [ブランチ名] #指定のブランチに移動

$ git checkout -b [ブランチ名] #指定のブランチを作成&指定のブランチに移動

push origin/master

ローカルリポジトリにおけるmasterブランチの状態を、リモートリポジトリのmasterブランチに反映
引数のorigin/masterは省略可能

$ git push origin/master

$ git push

image.png

conflict
ローカルリポジトリにおけるmasterブランチからpushする前に、リモートリポジトリにおけるmasterブランチに新しい履歴が追加されているため、ブランチ間で整合性がとれなくなっている状態

$ git push origin/master
To github.com:Hiroki-IT/Symfony2_Nyumon.git
 ! [rejected]        origin/master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:Hiroki-IT/Symfony2_Nyumon.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

image.png

merge master

ローカルリポジトリにおけるトピックブランチの変更を、同リポジトリにおけるmasterブランチに反映

$ git checkout [ブランチ名] #トピックブランチに移動

$ git merge master 

image.png

fetch mastermerge origin/master

fetch master
リモートリポジトリにおけるmasterブランチの変更を、ローカルリポジトリのorigin/masterブランチに反映

$ git fetch master

image.png

merge origin/master
ローカルリポジトリにおけるorigin/masterブランチの変更を、同リポジトリにおけるmasterブランチに反映

$ git fetch master

$ git merge origin/master 

image.png

pull origin/master

リモートリポジトリにおけるmasterブランチの変更を、ローカルリポジトリのorigin/masterブランチに反映(fetch)させ、その後masterブランチに反映(merge

$ git pull origin/master

image.png

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【ツール】最近、Gitで使った基本的コマンドと用語の関係をまとめる

参考リンク

全体像

今回まとめた用語
origin
clone
origin/master
branchcheckoutcheckout -b
push origin/master
merge master
fetch mastermerge origin/master
pull origin/master
関係図.png

origin

リモートリポジトリのアドレスに対してにつけられている別称
自分の場合、origingit@github.com:Hiroki-IT/Symfony2_Nyumon.gitであった

$ git remote -v
origin  git@github.com:Hiroki-IT/Symfony2_Nyumon.git (fetch)
origin  git@github.com:Hiroki-IT/Symfony2_Nyumon.git (push)

clone

リモートリポジトリの状態をそのままローカルリポジトリにコピー

git clone git@github.com:Hiroki-IT/Symfony2_Nyumon.git #リモートリポジトリのアドレスを指定

origin/master

リモート追跡ブランチ(リモートリポジトリのmasterブランチの状態がコピーされたブランチ)に対してデフォルトでつけられている別称(※fetch masterの項目も参照)
追跡されているリモートリポジトリのmasterブランチは$ git branch -vvを用いて確認可能で、[origin/master]と表示されたブランチが追跡されているmasterブランチを表す

$ git branch -vv
  2019/Symfony2_Nyumon/feature/2 f17f68e Merge remote-tracking branch 'refs/remotes/origin/master'
* 2019/Symfony2_Nyumon/master    f17f68e Merge remote-tracking branch 'refs/remotes/origin/master'
  master                         f17f68e [origin/master] Merge remote-tracking branch 'refs/remotes/origin/master'

branchcheckoutcheckout -b

$ git branch [ブランチ名] #指定のブランチを作成

$ git checkout [ブランチ名] #指定のブランチに移動

$ git checkout -b [ブランチ名] #指定のブランチを作成&指定のブランチに移動

push origin/master

ローカルリポジトリにおけるmasterブランチの状態を、リモートリポジトリのmasterブランチに反映
引数のorigin/masterは省略可能

$ git push origin/master

$ git push

image.png

conflict
ローカルリポジトリにおけるmasterブランチからpushする前に、リモートリポジトリにおけるmasterブランチに新しい履歴が追加されているため、ブランチ間で整合性がとれなくなっている状態

$ git push origin/master
To github.com:Hiroki-IT/Symfony2_Nyumon.git
 ! [rejected]        origin/master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:Hiroki-IT/Symfony2_Nyumon.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

image.png

merge master

ローカルリポジトリにおけるトピックブランチの変更を、同リポジトリにおけるmasterブランチに反映

$ git checkout [ブランチ名] #トピックブランチに移動

$ git merge master 

image.png

fetch mastermerge origin/master

fetch master
リモートリポジトリにおけるmasterブランチの変更を、ローカルリポジトリのorigin/masterブランチに反映

$ git fetch master

image.png

merge origin/master
ローカルリポジトリにおけるorigin/masterブランチの変更を、同リポジトリにおけるmasterブランチに反映

$ git fetch master

$ git merge origin/master 

image.png

pull origin/master

リモートリポジトリにおけるmasterブランチの変更を、ローカルリポジトリのorigin/masterブランチに反映(fetch)させ、その後masterブランチに反映(merge

$ git pull origin/master

まずfetchが行われ、
image.png

次にmergeが行われる
image.png

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【ツール】最近、Gitで使った基本的操作と用語の関係をまとめる

引用リンク

全体像

今回まとめた用語
origin
clone
origin/master
branchcheckoutcheckout -b
push origin master
merge master
fetch mastermerge origin master
pull origin master
◇ ローカルリポジトリのディレクトリの変更
関係図.png

origin

リモートリポジトリのアドレスに対してにつけられている別称
自分の場合、origingit@github.com:Hiroki-IT/Symfony2_Nyumon.gitであった

$ git remote -v
origin  git@github.com:Hiroki-IT/Symfony2_Nyumon.git (fetch)
origin  git@github.com:Hiroki-IT/Symfony2_Nyumon.git (push)

clone

リモートリポジトリの状態をそのままローカルリポジトリにコピー

git clone git@github.com:Hiroki-IT/Symfony2_Nyumon.git #リモートリポジトリのアドレスを指定

origin/master

リモート追跡ブランチ(リモートリポジトリのmasterブランチの状態がコピーされたブランチ)に対してデフォルトでつけられている別称y
ローカルリポジトリの全てのブランチの表示する$ git branch --allから、リモート追跡ブランチ(remotes/[xxx]/[xxx])を確認可能

$ git branch --all
  2019/Symfony2_Nyumon/master
  remotes/origin/master

branchcheckoutcheckout -b

$ git branch [ブランチ名] #指定のブランチを作成

$ git checkout [ブランチ名] #指定のブランチに移動

$ git checkout -b [ブランチ名] #指定のブランチを作成&指定のブランチに移動

push

push origin master

ローカルリポジトリにおけるmasterブランチの状態を、リモートリポジトリのmasterブランチに反映
引数のorigin masterは省略可能

$ git push origin master

$ git push

image.png

conflict

ローカルリポジトリにおけるmasterブランチからpushする前に、リモートリポジトリにおけるmasterブランチに新しい履歴が追加されているため、ブランチ間で整合性がとれなくなっている状態

$ git push origin master
To github.com:Hiroki-IT/Symfony2_Nyumon.git
 ! [rejected]        origin/master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:Hiroki-IT/Symfony2_Nyumon.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

image.png

fetch

fetch master

リモートリポジトリにおけるmasterブランチの変更を、ローカルリポジトリのorigin/masterブランチに反映

$ git fetch master

image.png
もし他人が2019/symfony2_Nyumon/feature/2というブランチをリモートリポジトリで作成した場合、$ git fetchした後に$ git branch --allで全てのブランチを確認すると、新しいブランチを追跡し始めたことを確認可能

$ git fetch master
$ git branch --all
  2019/Symfony2_Nyumon/master
  remotes/origin/master
  remotes/origin/2019/Symfony2_Nyumon/feature/2

merge

merge master

ローカルリポジトリにおけるトピックブランチの変更を、同リポジトリにおけるmasterブランチに反映

$ git checkout [ブランチ名] #トピックブランチに移動

$ git merge master 

image.png

merge origin master

ローカルリポジトリにおけるorigin/masterブランチの変更を、同リポジトリにおけるmasterブランチに反映

$ git fetch master

$ git merge origin master 

image.png

pull

pull origin master

リモートリポジトリにおけるmasterブランチの変更を、ローカルリポジトリのorigin/masterブランチに反映(fetch)させ、その後masterブランチに反映(merge

$ git pull origin master

まずfetchが行われ、
image.png

次にmergeが行われる
image.png

pull --set-upstream origin master

ブランチに初めてpushする時、pushの引数を

$ git push --set-upstream origin 2019/Symfony2_Nyumon/master

とする。
リモートブランチに履歴がないため、最初のpushを最上流の履歴としてとして設定する必要があるらしい。(勉強不足)

ローカルリポジトリのディレクトリの変更

ローカルリポジトリを、.gitを含んだまま別のディレクトリに移動するだけ
GitHubに影響はない

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

[Git] Githubのパスワードを変えてからpushできなくなったときの対処法

状況

Githubのパスワードを更新したら,pushできなくなってしまいました.

~/repository> git push origin HEAD
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/user-name/repository-name.git'

解決法

-u もしくは --set-upstream オプションで,ユーザ名とパスワードを入れ直すことができます.

$ git push -u origin HEAD
もしくは
$ git push --set-upstream origin HEAD
Username for 'https://github.com': 
Password for 'https://test@example.com@github.com':

参考:git push

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Ubuntu 18.04 で git の interactive.singleKey を有効にする

探すの面倒だったので忘れる前にメモ

困ったこと

地味に重要なこの設定 :

$ git config --global interactive.singlekey true

これだけでは Ubuntu では動かない。かなしい。

結論

libterm-readkey-perl をインストールしよう

$ sudo apt-get install libterm-readkey-perl

調査中のメモ

といっても man を読むだけ

$ man git-config
...
interactive.singleKey
    In interactive commands, allow the user to provide one-letter input with a single key (i.e., without hitting enter). Currently this is used by the --patch mode of
    git-add(1), git-checkout(1), git-commit(1), git-reset(1), and git-stash(1). Note that this setting is silently ignored if portable keystroke input is not
    available; requires the Perl module Term::ReadKey.
...

なにやら Perl module Term::ReadKey なるモジュールが必要らしい。

ということで物は試し

$ sudo apt-get install libterm-readkey-perl
Reading package lists... Done
Building dependency tree
Reading state information... Done
...
...
Unpacking libterm-readkey-perl (2.37-1build1) ...
Setting up libterm-readkey-perl (2.37-1build1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

適当なリポジトリのディレクトリで意図通りに動作することを確認

$ git add -p

おわり。
いかがでしたでしょうか

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

VSCodeでの`GitLens: Open Changes (with difftool)`実行時のエラーへの対処

Issue

GitLens: Open Changes (with difftool) 実行時に以下のエラーが出て、diff画面が開けない。

Unable to open changes in diff tool. No Git diff tool is configured

0_Screen Shot 2019-03-24 at 10.24.24.png

View Git Docsのリンクは以下。
https://git-scm.com/docs/git-config#git-config-difftool

Soltuion

以下を ~/.gitconfig に追加

[diff]
    tool = default-difftool
[difftool "default-difftool"]
    cmd = code --wait --diff $LOCAL $REMOTE

ref) https://code.visualstudio.com/docs/editor/versioncontrol#_vs-code-as-git-diff-tool

Result

GitLens: Open Changes (with difftool) を実行
1_Screen Shot 2019-03-24 at 10.21.21.png

↓ diff画面が正しく表示される
2_Screen Shot 2019-03-24 at 10.22.06.png

PS

git-difftool について以下が参考になった。

https://girigiribauer.com/archives/20161227/

[diff]
  tool = vimdiff
[difftool]
  prompt = false

これらの設定は git-diff 用ではなく git-difftool 用です。 それぞれお間違えなく。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

clocでgit管理下にあるファイルだけを対象とする

clocコマンドを単に実行するとvendor/bundlenode_modulesなどgit管理下に無いファイルも集計してしまいます。

$ ls
.DS_Store        .dependabot/     .rubocop.yml     Procfile         bin/             db/              public/
.bundle/         .editorconfig    Gemfile          README.md        config/          lib/             test/
.byebug_history  .git/            Gemfile.lock     Rakefile         config.ru        log/             tmp/
.circleci/       .gitignore       Guardfile        app/             coverage/        package.json     vendor/

$ cloc .
   13862 text files.
   10857 unique files.
    4951 files ignored.

github.com/AlDanial/cloc v 1.80  T=34.00 s (265.3 files/s, 51360.0 lines/s)
-----------------------------------------------------------------------------------
Language                         files          blank        comment           code
-----------------------------------------------------------------------------------
Ruby                              6813         122342         178929         629290
JavaScript                          97          30244          22803         213124
C                                  342          12727          12804          60348
Markdown                           323          20352              0          51256
...
-----------------------------------------------------------------------------------
SUM:                              9021         228601         250093        1267480
-----------------------------------------------------------------------------------

clocではvcsオプションがあり、git配下にあるファイルのみを対象とすることができます。

$ cloc --vcs=git
     169 text files.
     163 unique files.
      36 files ignored.

github.com/AlDanial/cloc v 1.80  T=0.35 s (439.0 files/s, 10178.5 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Ruby                            89            310            388           1590
Haml                            38              3              2            315
Sass                             8             39             21            250
YAML                            10             52            105            220
HTML                             3             15              3            182
Markdown                         1             15              0             28
JSON                             1              0              0              5
JavaScript                       3              3             26              4
CSS                              1              0             15              0
CoffeeScript                     1              0              3              0
-------------------------------------------------------------------------------
SUM:                           155            437            563           2594
-------------------------------------------------------------------------------

clocのオプションを設定するよりはgitのサブコマンドをエイリアスで定義する方が楽チンです。

# ~/.gitconfig
[alias]
  cloc = !cloc $(git ls-files)

$ cloc --vcs=gitと同じ結果が出る。

$ git cloc
     169 text files.
     163 unique files.
      36 files ignored.

github.com/AlDanial/cloc v 1.80  T=0.10 s (1558.7 files/s, 36142.7 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Ruby                            89            310            388           1590
Haml                            38              3              2            315
...
-------------------------------------------------------------------------------
SUM:                           155            437            563           2594
-------------------------------------------------------------------------------
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

MacでGitツールSourceTreeによるローカルリポジトリの作成

はじめに

Gitの概念を学ぶために、svnではTortoiseSVNのようにツールを用いて簡単に動作確認するものがないか探したていたところ、@nnahitoさんの記事でSourceTreeというツールがあることを知りました。
今回はローカルリポジトリの作成を試してみたので、SourceTreeを用いてローカルリポジトリの作成方法をまとめます。

gitを学習するために以下の記事も参考にさせていただきました。
https://naichilab.blogspot.com/2014/01/gitsourcetreegit.html?m=1

環境

macOS Mojave(バージョン10.14.1)
Sourcetree(バージョン3.1.1_213)

ローカルリポジトリ作成

1. SourceTreeを起動して新規をクリック。

スクリーンショット 2019-03-21 22.51.39.png

2. ローカルリポジトリを作成をクリック

スクリーンショット 2019-03-21 23.31.28.png

3. 保存先のパスを指定して作成をクリック

スクリーンショット 2019-03-24 6.25.32.png

4. .gitフォルダが作成されたら完了です。

スクリーンショット 2019-03-24 6.32.40.png

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む