Kohei Nozaki's blog 

Entries tagged [git]

Delete and move


Posted on Sunday Mar 09, 2014 at 09:20AM in Technology


Assume this:

kyle-no-MacBook:gitprac5 kyle$ ls -l
total 8
-rw-r--r--+ 1 kyle  staff  5  3  9 09:27 hoge.txt
kyle-no-MacBook:gitprac5 kyle$ cat hoge.txt 
hoge
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
nothing to commit, working directory clean
kyle-no-MacBook:gitprac5 kyle$ 

Delete

Delete from working tree

kyle-no-MacBook:gitprac5 kyle$ rm hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    hoge.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
kyle-no-MacBook:gitprac5 kyle$ 

Add deletion to index

kyle-no-MacBook:gitprac5 kyle$ git rm hoge.txt
rm 'hoge.txt'
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    hoge.txt
#
kyle-no-MacBook:gitprac5 kyle$

Commit

kyle-no-MacBook:gitprac5 kyle$ git commit -m 'delete'
[master 8793936] delete
 1 file changed, 1 deletion(-)
 delete mode 100644 hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
nothing to commit, working directory clean
kyle-no-MacBook:gitprac5 kyle$ ls -l
kyle-no-MacBook:gitprac5 kyle$ 

Move / Rename

kyle-no-MacBook:gitprac5 kyle$ git mv hoge.txt hige.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   renamed:    hoge.txt -> hige.txt
#
kyle-no-MacBook:gitprac5 kyle$ 
  • Another identical operation:
kyle-no-MacBook:gitprac5 kyle$ mv hoge.txt hige.txt
kyle-no-MacBook:gitprac5 kyle$ git rm hoge.txt
rm 'hoge.txt'
kyle-no-MacBook:gitprac5 kyle$ git add hige.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   renamed:    hoge.txt -> hige.txt
#
kyle-no-MacBook:gitprac5 kyle$ 


Showing diffs


Posted on Sunday Mar 09, 2014 at 09:06AM in Technology


Assume this:

kyle-no-MacBook:gitprac5 kyle$ ls -l
total 8
-rw-r--r--+ 1 kyle  staff  5  3  9 09:04 hoge.txt
kyle-no-MacBook:gitprac5 kyle$ cat hoge.txt
hoge
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
nothing to commit, working directory clean
kyle-no-MacBook:gitprac5 kyle$ 

Show unstaged changes

kyle-no-MacBook:gitprac5 kyle$ echo hogehoge >> hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git diff
diff --git a/hoge.txt b/hoge.txt
index 2262de0..1904c09 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1 +1,2 @@
 hoge
+hogehoge
kyle-no-MacBook:gitprac5 kyle$ 

Show staged changes

kyle-no-MacBook:gitprac5 kyle$ git add hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git diff --cached
diff --git a/hoge.txt b/hoge.txt
index 2262de0..1904c09 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1 +1,2 @@
 hoge
+hogehoge
kyle-no-MacBook:gitprac5 kyle$ 
  • There are no unstaged changes now so result of git diff is empty
kyle-no-MacBook:gitprac5 kyle$ git diff
kyle-no-MacBook:gitprac5 kyle$ 

Show both of unstaged and staged changes

Edit hoge.txt again

kyle-no-MacBook:gitprac5 kyle$ echo hogehogehoge >> hoge.txt

Show unstaged changes

kyle-no-MacBook:gitprac5 kyle$ git diff
diff --git a/hoge.txt b/hoge.txt
index 1904c09..9d1d67e 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1,2 +1,3 @@
 hoge
 hogehoge
+hogehogehoge

Show staged changes

kyle-no-MacBook:gitprac5 kyle$ git diff --cached
diff --git a/hoge.txt b/hoge.txt
index 2262de0..1904c09 100644
--- a/hoge.txt
+++ b/hoge.txt
@@ -1 +1,2 @@
 hoge
+hogehoge
  • This means that an another copy is created when we execute git add.

Show diff between 2 branches

kyle-no-MacBook:hello kyle$ git diff b4 origin/b4
diff --git a/README.md b/README.md
index b8630fa..9c70835 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1 @@
 b4-local1
-CANCEL
kyle-no-MacBook:hello kyle$ 


Statuses of resources


Posted on Sunday Mar 09, 2014 at 08:28AM in Technology


Statuses

  1. untracked
  2. unmodified
  3. modified
  4. staged

untracked

  • Newly created file what not connected to the any part of git.
  • We use git add to connect these file to git.

unmodified

  • Files connected to git but no changes were made yet.

modified

  • Files connected to git and modified.
  • When we do commit with this kind of resources only, no changes will be made.

staged

  • Files connected to git and modified, and declared to commit next time.

Examples of an newly created file

Create a repository

kyle-no-MacBook:gitprac5 kyle$ git init
Initialized empty Git repository in /Users/kyle/tmp/gitprac5/.git/
kyle-no-MacBook:gitprac5 kyle$ 

Create a file

kyle-no-MacBook:gitprac5 kyle$ echo hoge > hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   hoge.txt
nothing added to commit but untracked files present (use "git add" to track)
kyle-no-MacBook:gitprac5 kyle$ 
  • Now hoge.txt is untracked.

Add to Index

kyle-no-MacBook:gitprac5 kyle$ git add hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   hoge.txt
#
kyle-no-MacBook:gitprac5 kyle$ 
  • Now README is staged (?).

Commit

kyle-no-MacBook:gitprac5 kyle$ git commit -m 'initial'
[master (root-commit) ffbe516] initial
 1 file changed, 1 insertion(+)
 create mode 100644 hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
nothing to commit, working directory clean
kyle-no-MacBook:gitprac5 kyle$ 
  • Now README is unmodified.

Cancel git add to new file

  • We can cancel it like this:
kyle-no-MacBook:gitprac5 kyle$ echo README! > README
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   README
nothing added to commit but untracked files present (use "git add" to track)
kyle-no-MacBook:gitprac5 kyle$ git add README
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   README
#
kyle-no-MacBook:gitprac5 kyle$ git reset HEAD README
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   README
nothing added to commit but untracked files present (use "git add" to track)
kyle-no-MacBook:gitprac5 kyle$ 
  • Also this deletes all of untracked files:
kyle-no-MacBook:gitprac5 kyle$ git clean -f
Removing README
kyle-no-MacBook:gitprac5 kyle$ ls -l
total 8
-rw-r--r--+ 1 kyle  staff  5  3  9 08:40 hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
nothing to commit, working directory clean
kyle-no-MacBook:gitprac5 kyle$ 

Examples of existing file that already tracked by git

kyle-no-MacBook:gitprac5 kyle$ echo hogehoge >> hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# 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:   hoge.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
kyle-no-MacBook:gitprac5 kyle$ 
  • Now hoge.txt modified but not staged.
kyle-no-MacBook:gitprac5 kyle$ git add hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   hoge.txt
#
kyle-no-MacBook:gitprac5 kyle$ 
  • Now hoge.txt staged.
  • Let's take one more modify.
kyle-no-MacBook:gitprac5 kyle$ echo hogehogehoge >> hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   hoge.txt
#
# 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:   hoge.txt
#
kyle-no-MacBook:gitprac5 kyle$ 
  • This means last change (hogehogehoge) will not send by next commit.
  • Next commit will send the change (hogehoge) only.
  • So when you want to commit (hogehogehoge) next time, you need to execute git add once again.
kyle-no-MacBook:gitprac5 kyle$ git add hoge.txt
kyle-no-MacBook:gitprac5 kyle$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   hoge.txt
#
kyle-no-MacBook:gitprac5 kyle$ 

References

  1. How to sync local git repo with origin/master eliminating all changes - Stack Overflow
  2. git reset についてもまとめてみる - murankの日記


GitHubで遊ぶ


Posted on Tuesday Feb 11, 2014 at 11:16AM in Technology


環境

  • git version 1.8.3.4 (Apple Git-47)
  • OS X 10.9.1

前提条件

GitHubのアカウント登録とssh公開鍵登録は終わっているものとする。

参考文献を見ると専用の鍵ペア作ったりしてたけど面倒なので適当なid_rsa.pubを放り込んどいた。

Repositoryを作る

[2]に書いてあるけど

  1. ホーム画面でNew Repositoryを押す

  2. Repository nameとDescriptionを適当に埋めてCreate repositoryを押す

  3. つくられた

CLIからいじってみる

cloneしてみる

kyle-no-MacBook:gitprac2 kyle$ git clone git@github.com:lbtc-xxx/hello.git
Cloning into 'hello'...
Warning: Permanently added the RSA host key for IP address '192.30.252.129' to the list of known hosts.
warning: You appear to have cloned an empty repository.
Checking connectivity... done
kyle-no-MacBook:gitprac2 kyle$ ls -ld hello
drwxr-xr-x+ 3 kyle  staff  102  2 11 11:28 hello
kyle-no-MacBook:gitprac2 kyle$ ls -lR hello
total 80
-rwxr-xr-x+ 1 kyle  staff   452  2 11 11:28 applypatch-msg.sample
-rwxr-xr-x+ 1 kyle  staff   896  2 11 11:28 commit-msg.sample
-rwxr-xr-x+ 1 kyle  staff   189  2 11 11:28 post-update.sample
-rwxr-xr-x+ 1 kyle  staff   398  2 11 11:28 pre-applypatch.sample
-rwxr-xr-x+ 1 kyle  staff  1704  2 11 11:28 pre-commit.sample
-rwxr-xr-x+ 1 kyle  staff  1348  2 11 11:28 pre-push.sample
-rwxr-xr-x+ 1 kyle  staff  4951  2 11 11:28 pre-rebase.sample
-rwxr-xr-x+ 1 kyle  staff  1239  2 11 11:28 prepare-commit-msg.sample
-rwxr-xr-x+ 1 kyle  staff  3611  2 11 11:28 update.sample

hello/.git/info:
total 8
-rw-r--r--+ 1 kyle  staff  240  2 11 11:28 exclude

hello/.git/objects:
total 0
drwxr-xr-x+ 2 kyle  staff  68  2 11 11:28 info
drwxr-xr-x+ 2 kyle  staff  68  2 11 11:28 pack

hello/.git/objects/info:

hello/.git/objects/pack:

hello/.git/refs:
total 0
drwxr-xr-x+ 2 kyle  staff  68  2 11 11:28 heads
drwxr-xr-x+ 2 kyle  staff  68  2 11 11:28 tags

hello/.git/refs/heads:

hello/.git/refs/tags:
kyle-no-MacBook:gitprac2 kyle$ %%%

よくわからないけど何かgitのディレクトリ構造っぽいものができている。

commitしてみる

kyle-no-MacBook:gitprac2 kyle$ cd hello
kyle-no-MacBook:hello kyle$ echo hoge > README.md
kyle-no-MacBook:hello kyle$ git add README.md
kyle-no-MacBook:hello kyle$ git commit -m "first commit"
[master (root-commit) 30ec784] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
kyle-no-MacBook:hello kyle$ 

この時点ではGitHub側の画面をリロードしても資源が上がったりはしていない。たぶんpushってのをやらないとだめなんだろう。

pushしてみる

kyle-no-MacBook:hello kyle$ 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)

Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to 'git@github.com:lbtc-xxx/hello.git'
kyle-no-MacBook:hello kyle$ 

よくわからないけどおこられた。[4]を見ると何やらパラメータが必要っぽいのでよくわからないけど追加してみる

kyle-no-MacBook:hello kyle$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
 * [new branch]      master -> master
kyle-no-MacBook:hello kyle$ 

GitHubのgitのコマンド例が書かれたページをリロードしてみると

何かがアップロードされた。うむ

編集してadd→commit→pushしてみる

kyle-no-MacBook:hello kyle$ echo hogehoge >> README.md 
kyle-no-MacBook:hello kyle$ git add README.md 
kyle-no-MacBook:hello kyle$ git commit -m "second commit"
[master 79e339f] second commit
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 249 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:lbtc-xxx/hello.git
   30ec784..79e339f  master -> master
kyle-no-MacBook:hello kyle$ 

うむ。

参考文献

  1. EclipseでMavenプロジェクトを作ってGithubで管理 - Qiita
  2. ナウなヤングのためのgithub入門講座 -基本機能からdotfiles管理まで- - tumblr
  3. GitHub+Eclipseを使ったプロジェクトのはじめ方 - Archit!!
  4. git - Why can't I push to this bare repository? - Stack Overflow
  5. GitHub for Mac