Kohei Nozaki's blog 

Using Branches


Posted on Sunday Mar 09, 2014 at 04:01PM in Technology


Create a branch

git branch [BRANCH_NAME]
  • It goes like this:
kyle-no-MacBook:hello kyle$ git branch
* master
kyle-no-MacBook:hello kyle$ git branch branch1
kyle-no-MacBook:hello kyle$ git branch
  branch1
* master
  • We are still in master branch.
  • To change the branch to use as working tree:
kyle-no-MacBook:hello kyle$ git checkout branch1
Switched to branch 'branch1'
kyle-no-MacBook:hello kyle$ git branch
* branch1
  master
kyle-no-MacBook:hello kyle$ 
  • There's a shortcut:
kyle-no-MacBook:hello kyle$ git checkout -b branch2
Switched to a new branch 'branch2'
kyle-no-MacBook:hello kyle$ git branch
  branch1
* branch2
  master
kyle-no-MacBook:hello kyle$ 

Make an commit at an branch

kyle-no-MacBook:hello kyle$ echo fuge >> README.md 
kyle-no-MacBook:hello kyle$ git status
# On branch branch1
# 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
#
no changes added to commit (use "git add" and/or "git commit -a")
kyle-no-MacBook:hello kyle$ git commit -a
[branch1 114ec77] fuge
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
fuge
kyle-no-MacBook:hello kyle$ 

Back to master branch

kyle-no-MacBook:hello kyle$ git checkout master
Switched to branch 'master'
kyle-no-MacBook:hello kyle$ git status
# On branch master
nothing to commit, working directory clean
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
kyle-no-MacBook:hello kyle$ 
  • No changes are made to master branch.

Merge to master branch

kyle-no-MacBook:hello kyle$ git checkout master
Switched to branch 'master'
kyle-no-MacBook:hello kyle$ git merge branch1
Updating 9eb01ce..114ec77
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
fuge
kyle-no-MacBook:hello kyle$
  • If you have another branch that created from the master branch, you'll need to merge for it too.
kyle-no-MacBook:hello kyle$ git checkout branch2
Switched to branch 'branch2'
kyle-no-MacBook:hello kyle$ ls -l
total 8
-rw-r--r--+ 1 kyle  staff  27  3  9 16:20 README.md
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
kyle-no-MacBook:hello kyle$ git merge master
Updating 9eb01ce..114ec77
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
fuge
kyle-no-MacBook:hello kyle$ 

Delete an branch

kyle-no-MacBook:hello kyle$ git branch -d branch1
Deleted branch branch1 (was 114ec77).
kyle-no-MacBook:hello kyle$ 

Resolve a conflict

  • Create 2 branches from the master.
kyle-no-MacBook:hello kyle$ git checkout -b branch1
Switched to a new branch 'branch1'
kyle-no-MacBook:hello kyle$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
kyle-no-MacBook:hello kyle$ git checkout -b branch2
Switched to a new branch 'branch2'
kyle-no-MacBook:hello kyle$ 
  • Commit to each branches that will makes conflict
kyle-no-MacBook:hello kyle$ echo branch1 >> README.md
kyle-no-MacBook:hello kyle$ git commit -a -m 'branch1'
[branch1 2a74c5b] branch1
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git checkout branch2
Switched to branch 'branch2'
kyle-no-MacBook:hello kyle$ echo branch2 >> README.md
kyle-no-MacBook:hello kyle$ git commit -a -m 'branch2'
[branch2 f44a522] branch2
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ 
  • Make an attempt to merge both branches
kyle-no-MacBook:hello kyle$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
kyle-no-MacBook:hello kyle$ git merge branch1
Updating 114ec77..2a74c5b
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git merge branch2
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
kyle-no-MacBook:hello kyle$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#   (use "git push" to publish your local commits)
#
# You have unmerged paths.
#   (fix conflicts and run "git commit")
#
# Unmerged paths:
#   (use "git add <file>..." to mark resolution)
#
#   both modified:      README.md
#
no changes added to commit (use "git add" and/or "git commit -a")
kyle-no-MacBook:hello kyle$ 
  • README.md is below now:
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
fuge
<<<<<<< HEAD
branch1
=======
branch2
>>>>>>> branch2
kyle-no-MacBook:hello kyle$ 
  • So we have to edit it to resolve conflict manually:
kyle-no-MacBook:hello kyle$ vi README.md
kyle-no-MacBook:hello kyle$ cat README.md 
hoge
hogehoge
hogehogehoge
fuge
branch1 & branch2
kyle-no-MacBook:hello kyle$
  • After you done edit, then you can add & commit.
  • Add tells git that you have done resolving.
kyle-no-MacBook:hello kyle$ git commit -a -m 'resolve conflict'
[master 04c3da3] resolve conflict
kyle-no-MacBook:hello kyle$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
kyle-no-MacBook:hello kyle$ git log
commit 04c3da3496f6acc3acce525df855e5173b93f9d7
Merge: 2a74c5b f44a522
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 16:32:35 2014 +0900

    resolve conflict

commit f44a5224c39a6df457eb37a6c77fe593de8b5643
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 16:28:32 2014 +0900

    branch2

commit 2a74c5b1eebf19717212b6a75e540ba88591bfdb
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 16:28:19 2014 +0900

    branch1

Check the latest commit of each branches

kyle-no-MacBook:hello kyle$ git branch -v
  branch1 2a74c5b branch1
  branch2 f44a522 branch2
* master  04c3da3 [ahead 4] resolve conflict
kyle-no-MacBook:hello kyle$ 
  • This shows branches that already merged
kyle-no-MacBook:hello kyle$ git branch --merged
  branch1
  branch2
* master
kyle-no-MacBook:hello kyle$ 
  • This shows branches that not merged yet
kyle-no-MacBook:hello kyle$ git branch --no-merged 
  branch2
  master
kyle-no-MacBook:hello kyle$ 



No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed