liguofeng29’s blog

個人勉強用ブログだっす。

git - branch,tag

参考

リモートリポジトリ

pull : fetch + merge

fetch

  • リモートの最新履歴取得
  • FETCH_HEAD

push : リモートリポジトリへ変更を反映

ブランチ操作

初期処理

$ echo "リポジトリ初期化"
$ mkdir repo
$ cd repo
$ git init
  
$ echo "ファイル作成&コミット"
$ echo "first commit" > myfile.txt
$ git add myfile
$ git status
On branch master
  
Initial commit
  
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
  
        new file:   myfile.txt
         
$ git commit -m "first commit"
[master (root-commit) 2add8a7] first commit
  
$ git status
On branch master
nothing to commit, working directory clean

ブランチ作成&切り替え

$ echo "ブランチ作成"
$ git branch issue1
  
$ echo "ブランチ確認"
$ git branch
  issue1
* master
  
$ git checkout issue1
Switched to branch 'issue1'
  
$ git branch
* issue1
  master
  
echo "-bでブランチ作成&切り替え"
echo "git checkout -b branch_name"

$ echo "ブランチを変更してコミット"
$ echo "second commit" >> myfile.txt
$ git status
On branch issue1
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:   myfile.txt
  
no changes added to commit (use "git add" and/or "git commit -a")

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

        modified:   myfile.txt

$ git commit -m "second commit"
[issue1 e91d210] second commit

ブランチマージ

$ echo "masterにissue1をマージする"
$ git checkout master
Switched to branch 'master'
  
$ echo "マージ前の内容である"
$ less myfile.txt
  
$ git merge issue1
Updating 2add8a7..e91d210
Fast-forward
 myfile.txt | 1 +
 1 file changed, 1 insertion(+)
  
$ echo "マージ誤の内容である"
$ less myfile.txt

$ echo "マージはfast^forward(早送り)"

ブランチ削除

$ git branch
  issue1
* master
  
$ echo "ブランチ削除は-d branch_name"
$ git branch -d issue1
Deleted branch issue1 (was e91d210).
  
$ git branch
* master

複数ブランチ作業&競合解消

$ echo "複数ブランチ作成"
$ git branch issue2
$ git branch issue3
$ git branch
  issue2
  issue3
* master
  
$ echo "issue2ブランチでファイル修正&コミット"
$ git checkout issue2
Switched to branch 'issue2'
$ echo "add line by issue2" >> myfile.txt
$ git add myfile.txt
$ git commit -m "add line by issue2"
  
$ echo "issue3ブランチでファイル修正&コミット"
$ git checkout issue3
$ echo "add line by issue3" >> myfile.txt
$ git add myfile.txt
$ git commit -m "add line by issue3"

$ echo "masterブランチへissue2, issue3をマージする"
$ git checkout master
Switched to branch 'master'
$ git merge issue2
Updating e91d210..3a5d7a2
Fast-forward
 myfile.txt | 1 +
 1 file changed, 1 insertion(+)
  
$ git merge issue3
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.
  
$ echo "競合を手動で解消する"
$ vi myfile.txt
$ git add myfile.txt
$ git commit -m "merge issue3"
[master 0b82438] merge issue3

コミット履歴イメージ:
http://www.backlog.jp/git-guide/img/post/stepup/capture_stepup2_7_2.png

rebaseでマージする

$ echo "masterへのissue3マージを取り消す (issue2をマージした状態)"
$ git reset --hard HEAD~
HEAD is now at 3a5d7a2 add line by issue2
  
$ git checkout issue3
Switched to branch 'issue3'

$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: add line by issue3
Using index info to reconstruct a base tree...
M       myfile.txt
Falling back to patching base and 3-way merge...
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
error: Failed to merge in the changes.
Patch failed at 0001 add line by issue3
The copy of the patch that failed is found in: .git/rebase-apply/patch
  
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
  
$ echo "競合を解消する"
$ vi myfile.txt
  
$ git add myfile.txt
$ git rebase --continue
  
$ echo "masterへissue3をマージする(fast-forword)"
$ git checkout master
Switched to branch 'master'
$ git merge issue3
Updating 3a5d7a2..9038b2e
Fast-forward
 myfile.txt | 1 +
 1 file changed, 1 insertion(+)

コミット履歴イメージ:
http://www.backlog.jp/git-guide/img/post/stepup/capture_stepup2_8_2.png

タグ : タグとは、コミットを参照しやすくするために、わかりやすい名前を付けるものです。

  • 軽量タグ
  • 注釈タグ
$ echo "リポジトリ初期化&コミット"
$ git init
$ echo "first line" >> myfile.txt
$ git add myfile.txt
$ git commit -m "first commit"
  
$ echo "軽量タグ作成"
$ git tag first-tag
$ git tag
  
$ echo "タグ情報を含めlog"
$ git log --decorate
  
$ echo "注釈タグ作成"
$ git tag -am "注釈付きタグ追加" commet-tag
$ git tag -n