LoginSignup
8
0

More than 5 years have passed since last update.

Subversionしか使っていないSIerおじさんがgitに挑戦する

Last updated at Posted at 2018-12-24

自己紹介

SIerに勤務する会社員(38)です。
常駐している現場ではSubversionしか使っておらず、gitはほぼ使ったことがありません。
先日githubにわけもわからず1個だけのっけただけです。

現場ではプレイングマネージャー的なポジションです。
腕前はなかなか残念なものですが、ようやく学ぶのが面白くなってきた感じがあります(遅)。

この記事の主旨

そんなおっさんがgitを、短時間(子どもが昼寝から目覚めるまで)でできる範囲で学ぶものです。

学び方

優れた入門記事・サイトが多数あるのでしょうけれど、あえてマニュアル(man)、ヘルプ(--help)だけでやってみます。
なんでかというと、忘れてもすぐ参照できるから…
(ネットが自由に使えない現場もあるので)

環境構築

ググるといっぱい出てくるので割愛します!

マニュアルを読む

$ man git

GIT(1)                                                         Git Manual                                                        GIT(1)

NAME
       git - the stupid content tracker

SYNOPSIS
       git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--super-prefix=<path>]
           <command> [<args>]

DESCRIPTION
       Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level
       operations and full access to internals.
…以下略…

下手な翻訳は余計に混乱するので、英語を頑張って読むことにします。
macを使っているので、指3本タップで単語の意味を調べつつ…。
…にしても時間がかかりすぎると悟ったので、オプションじゃなくてコマンドが書いてありそうなところまで移動します。
(fとかbで移動できる程度にはvimを覚えた)

HIGH-LEVEL COMMANDS (PORCELAIN)
       We separate the porcelain commands into the main commands and some ancillary user utilities.

   Main porcelain commands
       git-add(1)
           Add file contents to the index.

       git-am(1)
           Apply a series of patches from a mailbox.

       git-archive(1)
           Create an archive of files from a named tree.

       git-bisect(1)
           Use binary search to find the commit that introduced a bug.

       git-branch(1)
           List, create, or delete branches.

       git-bundle(1)
           Move objects and refs by archive.

       git-checkout(1)
           Switch branches or restore working tree files.

なんか見たことある「add」とか「checkout」とかが出てきましたが、コマンドだけでも相当な数があることがわかりました。
これを全部ちゃんと覚えてから使おうっていう発想をすると、一生使えないままなんだろうなと悟ったので、マニュアルはそっとqで閉じておきます。
あとで必要になったら読めばいいっす。

ヘルプを読む

$ git --help

usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

おっ、まだ読める範囲のVolumeだし、使用場面ごとに主なコマンドが書いてあって親切!
ヘルプを中心に見ていこう。

start a working area (see also: git help tutorial)

ん!?どうやらチュートリアルがあるらしい。親切!
早速やってみます。ワクワク。

$ git help tutorial

GITTUTORIAL(7)                                                 Git Manual                                                GITTUTORIAL(7)

NAME
       gittutorial - A tutorial introduction to Git

SYNOPSIS
       git *

DESCRIPTION
       This tutorial explains how to import a new project into Git, make changes to it, and share changes with other developers.

       If you are instead primarily interested in using Git to fetch a project, for example, to test the latest version, you may prefer
       to start with the first two chapters of The Git User's Manual[1].

       First, note that you can get documentation for a command such as git log --graph with:

           $ man git-log
…以下略…

ん〜!…なかなかハードル高そうです。
でもとにかくやっていくのみ。

IMPORTING A NEW PROJECT
       Assume you have a tarball project.tar.gz with your initial work. You can place it under Git revision control as follows.

           $ tar xzf project.tar.gz
           $ cd project
           $ git init

       Git will reply

           Initialized empty Git repository in .git/

このあたりからは、具体的なコマンドが書いてあるおかげで、なんとなく意味がわかりました。
最初にgit initするかgit cloneするものらしい、というのはヘルプで見たので理解。
ではあとはとにかく実践していきます!

チュートリアルを実践

init

適当な場所にディレクトリを作って、そこに移動し、コマンド投入。

$ git init
Initialized empty Git repository in (作ったディレクトリのパス)/.git/

add

このへんはSubversionと同じっぽいので戸惑いなし。適当なファイルを作ってaddする。

$ vim tekitou
(…編集…)
$ cat tekitou 
aaaaaaaa
bbbbbbbb
cccccccc

$ git add .

commit

$ git commit
commitしようとしたら、なんか入力画面が出てきました。
コミットメッセージを入れるやつですね。

適当にコミットします。
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Committer: (おっさんの名前)
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
#       new file:   tekitou
#
~                                                                                                                                                     
$ git commit
[master (root-commit) 3762d4e] 適当にコミットします。
 Committer: (きもいおっさんのなまえ)
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 4 insertions(+)
 create mode 100644 tekitou

なんか色々怒られた気がしますが、とにかくcommitできました。

diff

名前からして、差分を取るものでしょう。
先程commitしたファイルを少し修正しつつ、新しいファイルを1つ追加したうえでdiffしてみます。

$ git diff
diff --git a/tekitou b/tekitou
index 43fbc54..f6e3dee 100644
--- a/tekitou
+++ b/tekitou
@@ -1,4 +1,4 @@
 aaaaaaaa
-bbbbbbbb
+bbbzzzbbbbb
 cccccccc

差分が出てきました。想定の範囲内。
ただ、新しく作ったファイルは出てきませんでしたので、addしてからdiffしてみます。

$ git add .
$ git diff

あれ、なんも出てこない。
チュートリアルを見ると--cachesオプションをつけるように書いてありました。

$ git diff --cached
diff --git a/newfile b/newfile
new file mode 100644
index 0000000..dece85d
--- /dev/null
+++ b/newfile
@@ -0,0 +1,4 @@
+111111111
+2222222222
+33333333333
+
diff --git a/tekitou b/tekitou
index 43fbc54..f6e3dee 100644
--- a/tekitou
+++ b/tekitou
@@ -1,4 +1,4 @@
 aaaaaaaa
-bbbbbbbb
+bbbzzzbbbbb
 cccccccc

status

修正したとか新規ファイルだとか、現在の管理状態がわかるっぽいですね。

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   newfile
    modified:   tekitou

まとめ

ここで子どもが起きたので時間切れっす!
branchesの考え方とか、push、pull等、全然gitならではのところにたどり着いていませんが、マニュアル、ヘルプ、チュートリアルが丁寧に用意されているので、こんな調子でやっていけばなんとかなるだろうという気がしました。

8
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
8
0