Octopress Blog

自分のためのまとめブログ

Gitメモ

メモ書き

コンセプトは、曖昧に覚えていることをとりあえず書き出し、修正しながら身につける。

リモートリポジトリ

  • gitサーバ側に作成されるリポジトリ。クライアント側から作成することはできない。
  • このリポジトリが無いとpushできない

master

  • masterのブランチ
  • git commit -m "hoge" のように、commitを初めてした時に自動で作成される
  • ブランチは git branch で確認可能

origin

  • URLのエイリアスみたいなもの
  • git remote add origin https://github.com/ftakao2007/test.git で後ろのURLが登録される
  • リポジトリごとに異なる
  • 以下herokuの例
1
2
3
4
5
fukui-no-MacBook-Air:h ftakao2007$ git remote -v
heroku    git@heroku.com:quiet-tor-2376.git (fetch)
heroku    git@heroku.com:quiet-tor-2376.git (push)
origin    git://github.com/heroku/ruby-sample.git (fetch)
origin    git://github.com/heroku/ruby-sample.git (push)

とりあえず手っ取り早くgitを使いたい

初期設定

まっさらなremoteリポジトリの場合

1
2
3
4
5
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/ftakao2007/test.git
git push -u origin master

いろいろ使われているremoteリポジトリをローカルに持ってきて使う場合

1
2
git clone https://github.com/ftakao2007/test.git
cd test

編集

リモートにプッシュ

1
2
3
4
5
6
vi munin_construct.py
git add munin_construct.py
git status
git commit -m 'Commit test'
git pull --rebase origin
git push origin

ブランチ

作成、確認、切り替え、コミット、プッシュ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fukui-no-MacBook-Air:test2 ftakao2007$ git branch
* master

fukui-no-MacBook-Air:test2 ftakao2007$ git branch test-branch

fukui-no-MacBook-Air:test2 ftakao2007$ git branch
* master
  test-branch

fukui-no-MacBook-Air:test2 ftakao2007$ git checkout test-branch
Switched to branch 'test-branch'

fukui-no-MacBook-Air:test2 ftakao2007$ git branch
  master
* test-branch

vi hoge.txt
git status
git add hoge.txt
git status
git commit -m "hoge"
git push -u origin test-branch
git checkout master

削除

ローカルのブランチの削除

1
2
3
4
5
6
7
8
9
fukui-no-MacBook-Air:test2 ftakao2007$ git branch
* master
  test-branch

fukui-no-MacBook-Air:test2 ftakao2007$ git branch -D test-branch
Deleted branch test-branch (was 19b1e03).

fukui-no-MacBook-Air:test2 ftakao2007$ git branch
* master

リモートのブランチ削除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fukui-no-MacBook-Air:test2 ftakao2007$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/test-branch

fukui-no-MacBook-Air:test2 ftakao2007$ git push origin :test-branch     ※コロンをつける
Username:
Password:
To https://github.com/ftakao2007/test2.git
 - [deleted]         test-branch

fukui-no-MacBook-Air:test2 ftakao2007$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

コミットの取り消し

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fukui-no-MacBook-Air:test2 ftakao2007$ git log
commit f56ca7da10d119fd91fb5f7d1c4799d94f525a91
Author: ftakao2007 <ftakao2007@gmail.com>
Date:   Wed Apr 10 22:41:52 2013 +0900

    a

commit 3c63dc839b68e5753da4ab69330e465b31dd17c2
Author: ftakao2007 <ftakao2007@gmail.com>
Date:   Wed Apr 10 22:37:36 2013 +0900

    hoge

fukui-no-MacBook-Air:test2 ftakao2007$ git reset --soft HEAD^

fukui-no-MacBook-Air:test2 ftakao2007$ git log
commit 3c63dc839b68e5753da4ab69330e465b31dd17c2
Author: ftakao2007 <ftakao2007@gmail.com>
Date:   Wed Apr 10 22:37:36 2013 +0900

    hoge

git reset --hard HEAD^ にすると、コミットの取り消しと同時にファイルの内容もコミット前に戻る。

originのurlを変更する

git remote set-url origin <新しいリポジトリURL>

新しくなったリモートのファイルをローカルのファイルに持ってくる

1
2
git fetch
git merge origin/master

今後確認すること

  • リビジョンてどうなってるの?ぱっとググった感じリビジョンて概念を使わない?世代を超えたファイルのdiffってできないのかな。
  • Webでdiffしたら文字化けしてる?

README.md

gituhub に自分のプロジェクトのリポジトリを作ってみる。

リンク