LoginSignup
1
2

More than 5 years have passed since last update.

心配性のコマンドラインgit入門

Last updated at Posted at 2017-09-05

コマンドラインからgitを使ってみます。

プロジェクトの開始

githubで学習用のリポジトリを作成しておきました。「README.md」というファイルだけあるリポジトリです。
https://github.com/estaro/working

クローンします。プロジェクト開始時に一回だけ行う作業です。

$ git clone https://github.com/estaro/working.git working
$ cd working
$ ls
README.md

なにはともあれstatus!よく使います。迷ったらstatus。

$ git status
# On branch master
nothing to commit, working directory clean

ブランチの切り替え

gitではより気軽にbranchを使うみたいです。

「gitwork」というbranchを作成して切り替えます。
切り替えは「checkout」です。(subversionとは違うみたいです)

$ git branch
* master
$ git branch gitwork
$ git checkout gitwork
Switched to branch 'gitwork'
$ git branch
* gitwork
  master
$ git status
# On branch gitwork
nothing to commit, working directory clean

日々の作業

ファイルの変更したり、追加したりしますね。

$ tree --charset=C ./
./
|-- README.md
`-- opencv
    `-- helloworld
        `-- opencvtest.cpp
$ git status
# On branch gitwork
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   README.md
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       opencv/
no changes added to commit (use "git add" and/or "git commit -a")

README.mdを変更して、opencvというディレクトリを追加しています。
追加したディレクトリは「Untracked files」という扱いになっており、「git add」しろって言われています。

$ git add opencv/helloworld/opencvtest.cpp
$ git status
# On branch gitwork
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   opencv/helloworld/opencvtest.cpp
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   README.md
#

addしてみました。「new file」となりましたね。
次はcommitとなるわけですが、いちいち差分を確認したいです。そこでdiff。

$ git diff README.md
diff --git a/README.md b/README.md
index 2fb43ae..4f1c92c 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,5 @@
 # working
 for studying
+
+## テストで追加してみた
+わーわーわー

これでやっとcommitします。まずは1ファイルづつ!

$ git commit opencv/helloworld/opencvtest.cpp

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'centos@monaca.(none)')

失敗。「お前は誰だ」とのことみたいです。

$ git config --global user.email "***@***"
$ git config --global user.name "estaro"

適当に設定しました。今度こそcommit!!

$ git commit opencv/helloworld/opencvtest.cpp
[gitwork d83ce77] my first commit!!
 1 file changed, 20 insertions(+)
 create mode 100644 opencv/helloworld/opencvtest.cpp
 $ git commit README.md
[gitwork 393c2e2] changed!
 1 file changed, 3 insertions(+)
 $ git status
# On branch gitwork
nothing to commit, working directory clean

次はpushですが、これもやっぱり心配なので、まずは差分を確認します。
branchに -a オプションでリモートのブランチもわかります。それに対して、diff。

$ git branch -a
* gitwork
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
$ git diff --name-status remotes/origin/master
M       README.md
A       opencv/helloworld/opencvtest.cpp

「--name-status」オプションで内容までは確認していません。
pushするぞー。

$ git push .
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Everything up-to-date

失敗。pushのスタイルを決める必要があるようです。

$ git config --global push.default simple

simpleに決めました。このあたりは参考を参照してください。

$ git push -u origin gitwork
Username for 'https://github.com':
Password for 'https://estaro@github.com':
Counting objects: 10, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 910 bytes | 0 bytes/s, done.
Total 8 (delta 0), reused 0 (delta 0)
To https://github.com/estaro/working.git
 * [new branch]      gitwork -> gitwork
Branch gitwork set up to track remote branch gitwork from origin.

とりあえず同じbranch名でgithubに上げてみました。

$ git branch -a
* gitwork
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/gitwork
  remotes/origin/master
$ git diff --name-status remotes/origin/gitwork
$

リモートに「github」というbranchができましたね。
github側からも確認してみましょう。

image

branchと内容が反映されていますね。

設定ファイル

git configで設定した内容は.gitconfigファイルに記録されています。

$ cat ~/.gitconfig
[user]
        email = ***@****
        name = estaro
[push]
        default = simple

ひとまず終わりです。

参考

https://www.backlog.jp/git-guide/
http://qiita.com/sta/items/982ab68e8220a81d485c
https://www.pistolfly.com/weblog/2010/11/git-svn-status-showing-chang.html
http://qiita.com/awakia/items/6aaea1ffecba725be601

1
2
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
2