2012年12月16日日曜日

gitを使うならこれはやっておいた方がいいでしょうシリーズ

普段gitを利用しているのですが、git statusとかgit checkoutとかサブコマンドが長すぎて
イライラしてる人いるでしょう 笑... 自分です

最近、#A君がgitを使い始めたらしく会社でよくサブコマンドを聞かれるので
基本的なコマンドを登録するよう勧めておいた、とりあえず良く使うコマンドを登録しておく

aliasを前もって登録しておく

cuomo@karky7 ~ $ git config --global alias.co checkout
cuomo@karky7 ~ $ git config --global alias.ci commit
cuomo@karky7 ~ $ git config --global alias.st status
cuomo@karky7 ~ $ git config --global alias.br branch
cuomo@karky7 ~ $ git config --global alias.brr 'branch -r'
cuomo@karky7 ~ $ git config --global alias.mg 'merge --no-ff'
cuomo@karky7 ~ $ git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
cuomo@karky7 ~ $ git config --list
user.name=karky7
user.email=cantimerny.g@gmail.com
alias.co=checkout
alias.ci=commit
alias.st=status
alias.br=branch
alias.brr=branch -r
alias.hist=log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short
alias.mg=merge --no-ff
cuomo@karky7 ~ $

bareリポジトリ作成

cuomo@karky7 ~ $ git init --bare test.git
Initialized empty Git repository in /home/cuomo/test.git/

ローカルリポジトリ作成

File1.txt追加、commitまで実行する
cuomo@karky7 ~ $ mkdir test
cuomo@karky7 ~ $ cd test
cuomo@karky7 ~/test $ emacs File1.txt
cuomo@karky7 ~/test $ cat File1.txt
haskellが好きです
cuomo@karky7 ~/test $ git add .
fatal: Not a git repository (or any of the parent directories): .git
cuomo@karky7 ~/test $ git init
Initialized empty Git repository in /home/cuomo/test/.git/
cuomo@karky7 ~/test $ git add .
cuomo@karky7 ~/test $ git commit -m'初期コミット'
[master (root-commit) 804d4cf] 初期コミット
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 File1.txt

remoteを追加し、push

cuomo@karky7 ~/test $ git remote add origin ~/test.git
cuomo@karky7 ~/test $ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 254 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/cuomo/test.git
 * [new branch]      master -> master
cuomo@karky7 ~/test $

ブランチ作成

cuomo@karky7 ~/test $ git br br1
cuomo@karky7 ~/test $ git br
  br1
* master
cuomo@karky7 ~/test $


そこへcheckoutしちょっと編集
cuomo@karky7 ~/test $ git co br1
Switched to branch 'br1'
cuomo@karky7 ~/test $ git br
* br1
  master
cuomo@karky7 ~/test $
cuomo@karky7 ~/test $ emacs File2.txt
cuomo@karky7 ~/test $ cat File2.txt
PHPも好きですが
cuomo@karky7 ~/test $

ステータス確認からのcommit

cuomo@karky7 ~/test $ git st
# On branch br1
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       File2.txt
nothing added to commit but untracked files present (use "git add" to track)
cuomo@karky7 ~/test $ git add .
cuomo@karky7 ~/test $ git commit -m'File2追加'
[br1 456f61c] File2追加
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 File2.txt
cuomo@karky7 ~/test $


もう一度commitが欲しいので編集
cuomo@karky7 ~/test $ emacs File1.txt
cuomo@karky7 ~/test $ git diff
diff --git a/File1.txt b/File1.txt
index 727b7ec..27a8941 100644
--- a/File1.txt
+++ b/File1.txt
@@ -1 +1,2 @@
 haskellが好きです
+モナドとかきついです

cuomo@karky7 ~/test $ git add .
cuomo@karky7 ~/test $ git commit -m'モナドの説明を追加'
[br1 f0659c8] モナドの説明を追加
 1 files changed, 1 insertions(+), 0 deletions(-)
cuomo@karky7 ~/test $

br1ブランチをmasterへマージ

cuomo@karky7 ~/test $ git co master
Switched to branch 'master'
cuomo@karky7 ~/test $ git br
  br1
* master
cuomo@karky7 ~/test $ git mg br1
Merge made by the 'recursive' strategy.
 File1.txt |    1 +
 File2.txt |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 File2.txt
cuomo@karky7 ~/test $


マージ後ログの確認

cuomo@karky7 ~/test $ git hist
*   3f02fdf 2012-11-12 | Merge branch 'br1' (HEAD, master) [karky7]
|\ 
| * f0659c8 2012-11-12 | モナドの説明を追加 (br1) [karky7]
| * 456f61c 2012-11-12 | File2追加 [karky7]
|/ 
* 804d4cf 2012-11-12 | 初期コミット (origin/master) [karky7]

変更をリモートへpush

cuomo@karky7 ~/test $ git push
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 669 bytes, done.
Total 7 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (7/7), done.
To /home/cuomo/test.git
   804d4cf..3f02fdf  master -> master
cuomo@karky7 ~/test $

リモートブランチを作成

cuomo@karky7 ~/test $ git push origin br1
Total 0 (delta 0), reused 0 (delta 0)
To /home/cuomo/test.git
 * [new branch]      br1 -> br1
cuomo@karky7 ~/test $

リモートブランチを確認

cuomo@karky7 ~/test $ git brr
  origin/br1
  origin/master
cuomo@karky7 ~/test $

こんな感じでちょっと簡単になる、どうかな

0 件のコメント:

コメントを投稿