Version control the source code is one of the very important software development process control to ensure systematic deployment. In fact, it is the first essential requirement as indicated by The Twelvel-Factor App. Version control software helps integrator to integrate various pieces of code from different developers working in a team in a very manageable manner. In IT Wonders, we use git for its offline history/commit and its distributed nature of version control.

On and off there will be new member totally new to git join the team. So we usually go through the routine to brief them about the git basic and the practice we used in IT Wonders. Therefore this is a blog post for those really new to git and want to learn how to best use the git rebase/merge command. Git veteran can skip this post :)

Let's do some little hand on to kick start.

// start by cloning and editing
git clone [email protected]:itwondersdesign/test.git
cd test
echo "test 1" >> test.txt
git add test.txt
git commit -m "init"
echo "test 2" >> test.txt
git commit -m "add line 2"
git commit -am "add line 2"
echo "test 3" >> test.txt && git commit -am "add line 3"

// imagine a new developer create a new branch and do some changes there
git checkout -b the-new-branch 
echo "test 3-new" >> test.txt && git commit -am "add line 3-new"
echo "test 4-new" >> test.txt && git commit -am "add line 4-new"

// while the first developer continue working on the master branch
git checkout master
echo "test 4" >> test.txt && git commit -am "add line 4"
echo "test 5" >> test.txt && git commit -am "add line 5"
git push origin master

So if we check the git log now, we will see something like this

// --graph will show the track
// --decorate will show the branch name
// --all to show all branches 
$ git log --graph --decorate --all

* commit 56e8e5129d1b75dc18bf488fbd354f1e25a913f2 (HEAD -> the-new-branch)
|
|     add line 4-new
|
* commit 64148dec56585e15d3af42914ad443b2bf1969d9
|
|     add line 3-new
|
| * commit fb2e8c4747b332c28dece31cccb22b03cb4a0744 (origin/master, master)
| |
| |     add line 5
| |
| * commit bbaf737f12381d694acb486f23c729442a862823
|/
|       add line 4
|
* commit 1bd3323d80b7c2213760f65177ba0567b07ed075
|
|     add line 3
|
* commit f5bda18791f6b160ab4b39b7ebcd526776c9b6a8
|
|     add line 2
|
* commit f30ab1272db66f66f91bdc9c9de2534300df8eea
      init

Let's assume you are the developer who are creating and working on the local branch the-new-branch and ready to push the commit back to the server.

To ease the job of integrator, you can use rebase or merge. For this blog, we will be using rebase. Since both of you changes on the same line of the same file, it's your duty to resolve the conflict before pushing to the server. You have two choice to consider. Either perform a git merge or execute a git rebase. git rebase would be preferred choice as it will leave the git history very clean with a single log path.

git rebase origin/master

// and you will see following output message
First, rewinding head to replay your work on top of it...
Applying: add line 3-new
Using index info to reconstruct a base tree...
M   test.txt
Falling back to patching base and 3-way merge...
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
error: Failed to merge in the changes.
Patch failed at 0001 add line 3-new
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".

Let's resolve the conflict and commit it.

git add test.txt
git rebase --continue

// let see the git log after the git rebase, which become a straight path
$ git log --graph --decorate --all

* commit 56e8e5129d1b75dc18bf488fbd354f1e25a913f2 (HEAD -> the-new-branch)
|
|     add line 4-new
|
* commit 64148dec56585e15d3af42914ad443b2bf1969d9
|
|     add line 3-new
|
* commit fb2e8c4747b332c28dece31cccb22b03cb4a0744 (origin/master, master)
|
|     add line 5
|
* commit bbaf737f12381d694acb486f23c729442a862823
|
|       add line 4
|
* commit 1bd3323d80b7c2213760f65177ba0567b07ed075
|
|     add line 3
|
* commit f5bda18791f6b160ab4b39b7ebcd526776c9b6a8
|
|     add line 2
|
* commit f30ab1272db66f66f91bdc9c9de2534300df8eea
      init

You are ready to push your new branch to the server now.

git push origin the-new-branch

That's all for this post. We hope you have learned something new today. Thanks.

Posted in Version Control on Oct 24, 2017

0 Comment

Leave a Comment

Your Email address will not be published. Required fields are marked *

About IT Wonders:

IT Wonders is a web agency based in Johor Bahru (JB), Malaysia and Singapore. We provide responsive and custom websites unique to your needs. If you have any inquiries about your website, do not hesitate to contact us.

YOU MIGHT BE INTERESTED:
TOPICS